[POJ1456]Supermarket(贪心 + 优先队列 || 并查集)
2017-06-15 19:06
381 查看
1.贪心 + 优先队列
按照时间排序从前往后
很简单不多说
——代码
#include <cstdio> #include <iostream> #include <algorithm> #define N 10001 int n, ans; int f ; struct node { int a, b; }p ; inline bool cmp(node x, node y) { return x.a > y.a; } inline int find(int x) { return x == f[x] ? x : f[x] = find(f[x]); } inline int read() { int x = 0, f = 1; char ch = getchar(); for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1; for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0'; return x * f; } int main() { int i, x; while(~scanf("%d", &n)) { ans = 0; for(i = 1; i < N; i++) f[i] = i; for(i = 1; i <= n; i++) p[i].a = read(), p[i].b = read(); std::sort(p + 1, p + n + 1, cmp); for(i = 1; i <= n; i++) { x = find(p[i].b); if(x) { f[x] = x - 1; ans += p[i].a; } } printf("%d\n", ans); } return 0; }View Code
相关文章推荐
- [POJ 1456]Supermarket[贪心][优先队列or并查集优化]
- pku 1456 Supermarket 贪心优先队列的应用 or 并查集
- POJ 1456 Supermarket (贪心 + 暴力 or 优先权队列 or 并查集)
- 【贪心+优先队列 / 并查集】【cofun1025】工作安排
- 51NOD 1163——最高的奖励(可用优先队列解决的贪心问题)
- LA 4254 Processor 处理器 【二分 贪心 优先队列】
- 暑假训练专题二 并查集的理解 优先队列
- Supermarket -- 贪心 + 并查集
- 22th【贪心&&优先队列】打地鼠游戏
- 【POJ1456】Supermarket(贪心)
- POJ—3253—Fence Repair—【贪心】【优先队列】
- 【2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest F】【贪心+ STL-优先队列】 Gourmet and Banquet 最大活动时间
- CF377B Preparing for the Contest [贪心+优先队列]
- hdu 1789 Doing Homework again【贪心 || 贪心+优先队列】
- Codeforces 553D Nudist Beach 优先队列 贪心
- poj Supermarket(贪心)(并查集)(优先队列)
- 当并查集遇上贪心【UVA1623,UVA11134,POJ1456】
- Supermarket poj 1456 (贪心,并查集)
- hdu 2850 Load Balancing (优先队列 + 贪心)
- hdu--1789--贪心||优先队列