POJ 1456 Supermarket
来源:互联网 发布:mac fn键有什么用 编辑:程序博客网 时间:2024/05/18 01:06
贪心,按时间排个序,再一个个地添加
有空位就直接放进去,没空位就覆盖价值更小的
#include <cstdio>#include <set>#include <algorithm>using namespace std;struct P{int v, d;}p[10005];bool operator < (P a, P b) {return a.d < b.d;}multiset<int> S;multiset<int>::iterator it;int main(){ int n; while(~scanf("%d", &n)) { S.clear(); for(int i = 1; i <= n; i++) scanf("%d%d", &p[i].v, &p[i].d); sort(p + 1, p + n + 1); for(int i = 1; i <= n; i++) { if(S.size() < p[i].d) S.insert(p[i].v); else { if(*S.begin() >= p[i].v) continue; S.erase(S.begin()); S.insert(p[i].v); } } int sum = 0; for(it = S.begin(); it != S.end(); it++) sum += *it; printf("%d\n", sum); } return 0;}
也可以按价值排序,再一个个地添加
从截止时间往前找空位
用并查集优化,用set也可以
根结点是没有用过的点,子结点是用过了的点
#include <cstdio>#include <algorithm>using namespace std;struct P{int v, d;}p[10005];bool operator < (P a, P b) {return a.v > b.v;}int fa[10005];int find(int n){ if(fa[n] == 0) return 0; if(fa[n] != n) fa[n] = find(fa[n]); return fa[n];}int main(){ int n; while(~scanf("%d", &n)) { int Max = 0; for(int i = 1; i <= n; i++) { scanf("%d%d", &p[i].v, &p[i].d); if(Max < p[i].d) Max = p[i].d; } for(int i = 0; i <= Max; i++) fa[i] = i; sort(p + 1, p + n + 1); int sum = 0; for(int i = 1; i <= n; i++) { int k = find(p[i].d); if(k == 0) continue; fa[k] = fa[k - 1]; sum += p[i].v; } printf("%d\n", sum); } return 0;}
0 0
- poj 1456 supermarket
- POJ-1456 Supermarket【贪心】
- poj 1456 Supermarket
- Supermarket poj 1456
- POJ 1456 Supermarket
- POJ 1456 Supermarket
- POJ-1456-Supermarket
- poj 1456 Supermarket
- poj 1456 Supermarket
- POJ - 1456 Supermarket
- POJ 1456 Supermarket
- POJ 1456 Supermarket
- POJ 1456 Supermarket
- poj 1456 Supermarket
- 【POJ 1456】Supermarket
- poj 1456 Supermarket
- POJ 1456 Supermarket
- poj 1456 Supermarket
- p7zip的解压和压缩
- poj3368 Frequent values
- powerdesigner逆向导出oracle数据库结构显示备注
- Executors 和 ExecutorService
- cocos2d-x 3.x addEventListenerWithSceneGraphPriority第二个参数的作用
- POJ 1456 Supermarket
- c++11特性之std::thread--进阶
- 海量数据处理
- Codeforces Round #328 (Div. 2) A. PawnChess
- Codeforces Round #328 (Div. 2) B. The Monster and the Squirrel
- 二叉树的基本操作实现
- 五,一个可以左右滑动的关卡选择界面
- 为什么选择 Java 8 ?
- 设计模式问答系列(3)----抽象工厂模式