分类:二分答案
题目链接: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;
}