UVaLive 2757 Supermarket (贪心+优先队列)
来源:互联网 发布:sql join 多个条件 编辑:程序博客网 时间:2024/06/17 19:26
题目链接:https://vjudge.net/problem/UVALive-2757
题目大意:有n个商品,商品i的利润为pi,销售截止日期为di(必须不晚于截止日期销售才有利润),销售每个商品需要1天时间。求最大利润。
思路:可以倒过来想,将商品按截止日期从晚到早排序,从大到小枚举日期d,每次将截止日期不晚于d的商品加入优先队列中,然后取出优先队列中的最大值即可。因为是从玩到早加入队列,所以先加入的元素一直到最后都是有效的。
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cctype>#include <cstdlib>#include <map>#include <iostream>#include <queue>using namespace std;const int maxn = 10000 + 10;struct Node{int p, d;}node[maxn];bool cmp(const Node &A, const Node &B){return A.d > B.d;}priority_queue<int> q;int main(){ int n; while(scanf("%d", &n) == 1) { while(q.size()) q.pop(); for(int i = 0; i < n; i++) scanf("%d%d", &node[i].p, &node[i].d); sort(node, node+n, cmp); int ans = 0, cur = 0; for(int day = node[0].d; day >= 1; day--) { while(cur < n && node[cur].d == day) q.push(node[cur++].p); if(q.size()) { ans += q.top(); q.pop(); } } printf("%d\n", ans); }return 0;}
阅读全文
0 0
- UVALive - 2757 Supermarket 贪心+优先队列
- UVaLive 2757 Supermarket (贪心+优先队列)
- UVALive 2757Supermarket(贪心)
- (advanced) UVA 优先队列+贪心 1316 Supermarket
- POJ 1456 Supermarket 贪心+优先队列
- poj1456 Supermarket【优先队列】
- poj 1456 Supermarket(贪心+并查集,贪心+优先队列)
- [POJ 1456]Supermarket[贪心][优先队列or并查集优化]
- poj Supermarket(贪心)(并查集)(优先队列)
- UvaLive 4254 Processor 优先队列
- UVALive - 3135 Argus 优先队列
- UVALive - 3135 Argus 优先队列
- UVALive - 3135 Argus【优先队列】
- poj3614-贪心、优先队列
- EOJ1855 贪心+优先队列
- uvalive4254(贪心+优先队列)
- HOJ1062 贪心+优先队列
- poj3465 贪心+优先队列
- 2017 10 03 NOIP2017模拟赛
- opengl es 透视投影矩阵推导
- 虚拟存储(存储器)鸟瞰前篇
- 讲讲排序(C++描述)
- 20171005WindowsPrj08_02Hook是什么
- UVaLive 2757 Supermarket (贪心+优先队列)
- 男生脱单后都有哪些变化?
- Java 线程池(ThreadPoolExecutor)原理分析与使用
- Service详细解析
- 深圳突传大消息:这类人要哭了!
- linux内存管理之初始化
- django多表过滤和搜索
- 深度学习之基础模型—InceptionV1(GoogLeNet)
- Spring的AOP和IOC的分析理解