poj 1456 Supermarket 贪心 并查集优化
2016-07-25 22:12
507 查看
按价值排序后由大到小加进结果,贪心的放在最后的时间里如果某个时间已经占满往前放这时要用并查集维护这天前面没有占满的最后一天
#include<cstdio> #include<algorithm> #include<cstring> #include<cstdlib> #include<queue> #define maxn 10015 using namespace std; int n; struct node{ int mo,ti; }no[maxn]; int fa[maxn]; int getFa(int pre) { if(pre == fa[pre])return pre; fa[pre] = getFa(fa[pre]); return fa[pre]; } bool cmp(node no1,node no2) { return no1.mo>no2.mo; } int main() { while(scanf("%d",&n)!=EOF) { int time = -1; for(int i=1;i<=n;i++){ scanf("%d %d",&no[i].mo,&no[i].ti); time = max(time,no[i].ti);} sort(no+1,no+n+1,cmp); for(int i=0;i<=time;i++)fa[i] = i; int sum = 0; for(int i=1;i<=n;i++) { int pre = getFa(no[i].ti); if(pre) { fa[pre] = pre-1; sum+=no[i].mo; } } printf("%d\n",sum); } return 0; }
相关文章推荐
- 在WordPress主题中使用Font Awesome
- smarty变量调节器
- 设备树
- FFmpeg中的滤镜(五):视频滤镜 -- ass
- **MYSQL** 系列五
- Linux运维注意事项
- 上海庆科EMW3162 AT指令固件 实现串口透传
- 数据结构实验之栈七:出栈序列判定
- Docker数据管理:Named volume
- android-Notification
- fatal: Not a git repository 的解决办法
- POJ 3321 Apple Tree
- 框架基础之Hibernate简介
- java泛型
- 打算开始在自己学习的时候顺便写写博客了,至少不懂的时候可以回来看看了
- 谈谈垃圾回收机制及内存管理
- 宝石比较重量
- APP开发实战116-集成SO文件
- new self() 与new static()的区别
- linux下安卓刷机脚本