1 条题解

  • 0
    @ 2025-10-8 17:07:49
    #include<bits/stdc++.h>
    using namespace std;
    const int N=1e4+10;
    typedef pair<int, int> PII;
    int n, ans, cnt;
    queue<PII> wait;
    set<PII> run;//保存每个任务的内存 起始位置 和 终结位置 
    priority_queue<PII, vector<PII>, greater<PII> > heap;//保存在内存中每个任务 结束时间 和 起始位置 
     
    bool Insert(int t, int m, int p)
    {
        for (auto it=run.begin();it!=run.end();it++)
        {
            auto temp=it;
            temp++;
            if (temp!=run.end())
            {
                int stm=it->second+1;
    			int edm=stm+m-1; 
                if ( edm < temp->first )
                {
                    run.insert({stm, edm});
                    ans=max(ans,t+p);
                    heap.push({t+p, stm});
                    return true;
                }
            }
        }
        return false;
    }
     
    void finish(int t)
    {
        while (!heap.empty()&&heap.top().first<=t)
        {
            int f=heap.top().first;
            while (!heap.empty()&&heap.top().first==f)
            {
                auto temp=heap.top();
                heap.pop();
                auto it=run.lower_bound({temp.second, 0});//优先查找第一关键字:任务结束时间,set支持二分查找是本题不超时关键 
    			run.erase(it);
            }
            while (!wait.empty())
            {
                auto temp=wait.front();
                if (Insert(f, temp.first, temp.second))
                    wait.pop();
                else break;
            }
        }
    }
     
    int main()
    {
        int t, m, p;
        scanf("%d", &n);
        run.insert({-1, -1});run.insert({n, n});
        cnt=0,ans=0; 
        while (scanf("%d%d%d", &t, &m, &p)!=EOF&&(t||m||p))
        {
            finish(t);
            if (!Insert(t, m, p))
            {
                wait.push({m, p});
                cnt++;
            }
        }
        finish(2e9);//只有释放之前内存的所有任务,才能让队列中的任务进入内存,得到最终结束时间 
        printf("%d\n%d\n", ans, cnt);
        return 0;
    }
    
    • 1

    *【STL:set+priority_queue+queue】[NOI1999] 内存分配

    信息

    ID
    4782
    时间
    1000ms
    内存
    128MiB
    难度
    10
    标签
    递交数
    2
    已通过
    2
    上传者