zoukankan      html  css  js  c++  java
  • HDU3466Proud Merchants(贪心&背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=3466

    题目大意是说n个物品每个物品的花费是p,但是如果你现在的钱少于q就买不了这个物品,每个物品的价值是v,求有钱M时的最大价值。

    一看这个题,就觉得直接按p背包还是按q背包都不对,然后就没有然后了。。。

    然后看了题解:是说按q-p贪心,其实是这样,每次取q-p最小的,那么每次留下的自然就是最多的金钱。

    至于严格的证明。。。。。。。待研究

    剩下的就是01背包了。。,。。

     1 #include <map>
     2 #include <set>
     3 #include <stack>
     4 #include <queue>
     5 #include <cmath>
     6 #include <ctime>
     7 #include <vector>
     8 #include <cstdio>
     9 #include <cctype>
    10 #include <cstring>
    11 #include <cstdlib>
    12 #include <iostream>
    13 #include <algorithm>
    14 using namespace std;
    15 #define INF 0x3f3f3f3f
    16 #define MAX(a,b) (a > b ? a : b)
    17 #define MIN(a,b) (a < b ? a : b)
    18 #define mem0(a) memset(a,0,sizeof(a))
    19 
    20 typedef long long LL;
    21 const double eps = 1e-12;
    22 const int MAXN = 1005;
    23 const int MAXM = 5005;
    24 
    25 struct NODE
    26 {
    27     int p,q;
    28     int val;
    29 }item[MAXN];
    30 int N, M, DP[MAXM];
    31 
    32 int cmp(NODE a, NODE b)
    33 {
    34     return a.q-a.p < b.q-b.p;
    35 }
    36 
    37 int main()
    38 {
    39     while(~scanf("%d %d", &N, &M))
    40     {
    41         mem0(DP);
    42         for(int i=0;i<N;i++)scanf("%d %d %d", &item[i].p, &item[i].q, &item[i].val);
    43         sort(item, item+N, cmp);
    44         for(int i=0;i<N;i++)
    45         {
    46             for(int j=M;j>=item[i].q;j--)
    47             {
    48                 DP[j] = max(DP[j], DP[j-item[i].p]+item[i].val);
    49             }
    50         }
    51         printf("%d
    ", DP[M]);
    52     }
    53     return 0;
    54 }
  • 相关阅读:
    GO语言并发
    NEERC2017:L
    bzoj2823[AHOI2012]信号塔
    bzoj1336[Balkan2002]Alien最小圆覆盖
    bzoj1069[SCOI2007]最大土地面积
    ACM2017Tsukuba:H
    ACM2015沈阳:B-Bazinga
    bzoj2724[Violet 6]蒲公英
    [bzoj4066]简单题
    [bzoj2125]最短路
  • 原文地址:https://www.cnblogs.com/gj-Acit/p/3452033.html
Copyright © 2011-2022 走看看