您的位置:首页 > 其它

uvalive 4254(二分+模拟)

2016-03-08 17:00 274 查看
分析:

大方向是二分答案,对每一秒进行模拟,判断是否能够处理完在这个时刻可以处理的任务,可以用优先队列储存当前可以执行的任务。当队列中元素不为空且里面有的元素的结束时间小于当前秒时,不能处理完;当队列中元素为空且判断完所有元素时说明可以以该速度执行。

ac代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;

struct task
{
int r,d,w;
bool operator < (const task& a)const{
return d>a.d;}
}a[10005];

bool cmp(task a,task b)
{
return a.r<b.r;
}
bool ok(int x,int n);

priority_queue<task> q;

int main()
{
//freopen("test.txt","r",stdin);
int cas;
scanf("%d",&cas);
while(cas--)
{
int n;
scanf("%d",&n);
int mmax=0;
for(int i=0;i<n;i++)
{
scanf("%d%d%d",&a[i].r,&a[i].d,&a[i].w);
mmax+=a[i].w;
}
sort(a,a+n,cmp);
int lb=0,ub=mmax;
while(ub-lb>1)
{
int mid=(ub+lb)/2;
if(ok(mid,n))
ub=mid;
else
lb=mid;
}
printf("%d\n",ub);
}
}
bool ok(int x,int n)
{
while(!q.empty())
q.pop();
int i=0,j=0;
while(1)
{
while(a[i].r<=j&&i<n)
q.push(a[i++]);
int now=x;
while(now!=0&&(!q.empty()))
{
task tmp=q.top();
q.pop();
int m=min(now,tmp.w);
now-=m;
tmp.w-=m;
if(tmp.w)
q.push(tmp);
}
j++;
if(!q.empty()&&q.top().d<=j)
return false;
if(q.empty()&&i==n)
return true;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: