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\n", DP[M]);
52     }
53     return 0;
54 }

 

posted @ 2013-12-01 00:54  再见~雨泉  阅读(254)  评论(0编辑  收藏  举报