第一章-問題求解策略-LA4254-Processor

分類:二分答案
題目鏈接:LA4254-Processor
這個題目如果在某一秒上一個任務已經執行完了,是可以繼續執行下一個任務的,這個check函數相對來說比較難想

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
struct node
{
    int l,r,w;
    friend bool operator < (node aa,node bb)
    {
        return aa.r>bb.r;
    }
}a[maxn];
int T,n,l,r,mid,mmax,lt,rt;
bool cmp(const node &a,const node &b)
{
    return a.l<b.l;
}
bool check(int mid)
{
    priority_queue<node> q;
    int t=a[0].l,i=0;
    while(t<rt)
    {
        while(i<n&&a[i].l<=t)
        {
            q.push(a[i++]);
        }
        int v=mid;
        while(v&&q.size())
        {
            node temp=q.top();
            q.pop();
            int tt=min(temp.w,v);
            v-=tt;
            temp.w-=tt;
            if(temp.w)
            {
                q.push(temp);
            }
        }
        t++;
        if(!q.empty()&&q.top().r<=t)
        {
            return false;
        }
    }
    return i==n&&q.empty();
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        mmax=rt=0;
        lt=0x7fffffff;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].w);
            lt=min(lt,a[i].l);
            rt=max(rt,a[i].r);
            mmax+=a[i].w;
        }
        sort(a,a+n,cmp);
        l=0,r=mmax+10;
        while(l<r)
        {
            mid=(l+r)>>1;
            if(check(mid))
            {
                r=mid;
            }
            else
            {
                l=mid+1;
            }
        }
        printf("%d\n",r);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章