UVALive - 2757 Supermarket 贪心+优先队列
2015-03-18 16:34
567 查看
题目大意:超市有n件物品,每件物品有响应的利润p和保质期d,每天只能卖出一件物品,必须得在改保质期前卖出才能赚到利润,问如何卖这些物品才能使利润达到最大
解题思路:按保质期的降序排序,然后从最大的保质期开始枚举,设置一个优先队列,利润大的排前面
因为时间t是从大到小开始枚举的,所以如果保质期的d >= t的话,就可以将其放在队列中了,因为接下来时间的减小的,所以t<=d这个条件是恒满足的,所以只需从优先队列中挑出p最大的就可以了
解题思路:按保质期的降序排序,然后从最大的保质期开始枚举,设置一个优先队列,利润大的排前面
因为时间t是从大到小开始枚举的,所以如果保质期的d >= t的话,就可以将其放在队列中了,因为接下来时间的减小的,所以t<=d这个条件是恒满足的,所以只需从优先队列中挑出p最大的就可以了
[code]#include<cstdio> #include<algorithm> #include<queue> using namespace std; #define maxn 10010 struct Pro{ int p, d; bool operator < (const Pro t) const { return p < t.p; } }P[maxn]; int n; int cmp(const Pro a, const Pro b) { return a.d > b.d; } int main() { while(scanf("%d",&n) == 1) { int MAX_t = 0; for(int i = 0; i < n; i++) { scanf("%d%d",&P[i].p, &P[i].d); MAX_t = max(MAX_t, P[i].d); } sort(P,P+n,cmp); priority_queue<Pro> q; int ans = 0, j = 0; for(int i = MAX_t; i > 0; i--) { while(j < n && P[j].d >= i) q.push(P[j++]); if(!q.empty()) { ans += q.top().p; q.pop(); } } printf("%d\n",ans); } return 0; }
相关文章推荐
- UVaLive 2757 Supermarket (贪心+优先队列)
- UVALive 2757 Supermarket(贪心)
- (advanced) UVA 优先队列+贪心 1316 Supermarket
- uvalive 2757(贪心)
- uvalive4254(贪心+优先队列)
- UVaLive 3902 Network (无根树转有根树,贪心)
- poj Supermarket(贪心)(并查集)(优先队列)
- UVa 1316 Supermarket 解题报告(贪心)
- UVALive - 3135 Argus——优先队列
- UVALive 6093 Emergency Room --优先队列实现的模拟
- UVA1153-Keep the Customer Satisfied(贪心+优先队列)
- uva 1153 - Keep the Customer Satisfied(贪心+优先队列)
- UVALive 2757
- UVALive 3135 Argus(优先队列)
- uvalive 2949 - Elevator Stopping Plan(贪心+二分)
- uvalive 3266 - Tian Ji -- The Horse Racing(贪心)
- uvalive 2519(贪心)
- uvalive3971(二分+贪心)
- poj Supermarket(贪心)(并查集)(优先队列)
- UVALive 4863 Balloons 贪心/费用流