您的位置:首页 > 其它

uva-1153-策略贪心

2018-03-27 17:55 411 查看
这个题见过几次没想出来也一直没补、、

#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <list>
#include <cmath>
#define INF 0x3f3f3f3f

using namespace std;

const int maxn=800000+5;
int t,n,u,v;
struct P
{
int q,d;
bool operator<(const P&a) const
{
return d<a.d;//按照结束日期递增排序
}
} p[maxn];

priority_queue<int > pq;

int main()
{
scanf("%d",&t);
for(int l=1;l<=t;l++)
{
while(!pq.empty()) pq.pop();
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
scanf("%d%d",&u,&v);
p[i].q=u;
p[i].d=v;
}
sort(p+1,p+n+1);
int sum=0;
for(int i=1; i<=n; i++)
{
if(sum+p[i].q<=p[i].d)//如果能完成,直接放入优先队列表示选中
{
sum+=p[i].q;
pq.push(p[i].q);
}
else//如果当前任务无法完成,就将花费时间最多的任务取消
{
if(pq.top()>p[i].q && sum-pq.top()+p[i].q<=p[i].d)//如果当前任务花费时间小于取出来的任务的时间,并且如果能完成当前的任务,就确定替换掉
{
sum-=pq.top();
pq.pop();
sum+=p[i].q;
pq.push(p[i].q);
}
}
}
if(l!=1) printf("\n");
printf("%d\n",pq.size());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: