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;
}
#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;
}
相关文章推荐
- UVA 1153 - Keep the Customer Satisfied(贪心)
- UVa 1153 - Keep the Customer Satisfied(贪心)
- Uva 11134 问题分解,贪心策略区间选点问题
- UVA-1153 Keep the Customer Satisfied (贪心)
- UVA 1153 Keep the Customer Satisfied(贪心+优先队列)
- UVa 1153 Keep the Customer Satisfied 【贪心 优先队列】
- UVa 1153:Keep the Customer Satisifed(贪心)
- UVA 1153 Keep the Customer Satisfied(贪心2.0)
- Uva11054 等价转换 贪心策略:供需分配问题
- UVa 1153 Keep the Customer Satisfied (贪心+优先队列)
- UVA - 1153 Keep the Customer Satisfied(贪心)
- UVA1153-Keep the Customer Satisfied(贪心+优先队列)
- UVa 1153 Keep the Customer Satisfied 解题报告(贪心)
- UVA 1153 Keep the Customer Satisfied 顾客是上帝 经典贪心 (SWE 2005)
- UVA 1153 Keep the Customer Satisfied 顾客是上帝(贪心)
- UVa 1153 - Keep the Customer Satisfied (贪心 + 优先队列)
- UVALive 7483 贪心策略
- UVa 1153 Keep the Customer Satisfied (贪心+优先队列)
- UVA - 311 Pockets(贪心策略)
- uva 1153 - Keep the Customer Satisfied(贪心+优先队列)