poj 1456 Supermarket
来源:互联网 发布:知君本无邪by尼罗 编辑:程序博客网 时间:2024/05/18 02:58
点击打开链接poj 1456
思路: 贪心+并查集
分析:
1 题目的意思是给定n个物品的利润和出售的最后时间,求最大的利润
2 比较明显的贪心问题,按照利润排序,假设当前是第i个物品,那么利润为pi出售的时间为di,那么假设di还没有物品销售那么肯定先销售第i个物品,否则找di~1这些时间里面是否有没有销售物品
3 如果按照2的思路做法最坏的情况是O(n^2),但是数据比较弱可以过。但是我们这边可以利用并查集来优化,根据2如果di已经被占用了那么我们要去找di~1的时间,这里利用并查集的思路。如果di被占用了之后,那么father[di] = di-1说明如果下次再来一个销售日期为di的话是要放到di-1天,那么对于第i个物品我们只要判断find(di)是不是大于0,如果是di可以放到find(di)这一天销售,否则跳过
代码:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int MAXN = 10010;struct Node{ int val; int day; bool operator<(const Node &s)const{ return val > s.val; }};Node node[MAXN];int father[MAXN];int find(int x){ if(father[x] != x) father[x] = find(father[x]); return father[x];}int solve(int n){ int ans = 0; for(int i = 1 ; i <= n ; i++){ int fa = find(node[i].day); if(fa > 0){ father[fa] = fa-1; ans += node[i].val; } } return ans; }int main(){ int n; while(scanf("%d" , &n) != EOF){ for(int i = 1 ; i < MAXN ; i++) father[i] = i; for(int i = 1 ; i <= n ; i++) scanf("%d%d" , &node[i].val , &node[i].day); sort(node+1 , node+n+1); printf("%d\n" , solve(n)); } return 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
- 谷歌祭出“机器智能”技术,SEO如今要这么玩才行
- 背包九讲学习--基础背包
- windows+cygwin+eclipse运行时jdk的配置问题
- android EditText和TextView出现中文、英文等string串的排版问题
- uva 10499
- poj 1456 Supermarket
- hdu 4276 树形dp背包
- 编程珠机 第二章 找出一个不在文件中一32位整数。
- android 自定义toast
- 图文解说redmine使用指南
- 记事本
- 飘逸的python - 发送qq邮件
- 小米的红米战略:告别苹果走向三星
- poj 2612 Mine Sweeper