[2016-03-18][POJ][1456][Supermarket]

  • 时间:2016-03-18 09:05:15 星期五

  • 题目编号:[2016-03-18][POJ][1456][Supermarket]

  • 题目大意:给定一个物品清单,求最大能卖出的商品价值

  • 分析:

    • 贪心,显然价值大的先卖,然后价值大的应该从最后一天开始卖,即如果最后一天能卖就最后一天,否则就往前一天(前面有物品已经占用那天了)
    • 求maxprofit 的时候,可以用dp递推,对于没见物品,更新一次期限内的信息
    • 也可以用并查集优化,fa[i] 更新维护第i天及其往前没有卖过物品的日期,这里用了压缩路径,第二次查找就会快些,如果没有压缩路径,那么和直接从日期开始往前逐天查找没有区别
  • 方法:贪心排序+并查集优化

  1. #define _WORK_
  2. #ifdef _WORK_
  3. #include <algorithm>
  4. #include <cstdio>
  5. using namespace std;
  6. typedef long long LL;
  7. #define FOR(x,y,z) for(int (x)=(y);(x)<(z);++(x))
  8. const int maxp = 10000 + 100;
  9. const int maxn = 10000 + 100;
  10. int fa[maxp];
  11. void ini(int n){
  12. FOR(i,0,n + 1) fa[i] = i;
  13. }
  14. int fnd(int x){
  15. return x == fa[x]?x:fa[x] = fnd(fa[x]);
  16. }
  17. void uni(int x,int y){
  18. x = fnd(x);y = fnd(y);
  19. if(x == y )return ;
  20. fa[x] = y;
  21. }
  22. struct Product{
  23. int val,dead;
  24. bool operator < (const Product & a){
  25. return a.val < val || (a.val == val && a.dead < dead);
  26. }
  27. }a[maxn];
  28. int main(){
  29. //freopen("in.txt","r",stdin);
  30. //freopen("out.txt","w",stdout);
  31. int n;
  32. while(~scanf("%d",&n)){
  33. int maxtime = 0;
  34. FOR(i,0,n){
  35. scanf("%d%d",&a[i].val,&a[i].dead);
  36. if(a[i].dead > maxtime) maxtime = a[i].dead;
  37. }
  38. ini(maxtime);
  39. sort(a,a+n);
  40. int ans = 0;
  41. FOR(i,0,n){
  42. int t = fnd(a[i].dead);
  43. if(t){
  44. fa[t] = t-1;
  45. ans += a[i].val;
  46. }
  47. }
  48. printf("%d\n",ans);
  49. }
  50. return 0;
  51. }
  52. #endif


来自为知笔记(Wiz)


posted on 2016-03-18 09:39  红洋  阅读(154)  评论(0)    收藏  举报

导航