HDU3466Proud Merchants(贪心&背包)

本文探讨了一种解决背包问题的独特策略,通过按q-p值进行贪心选择,实现最大化价值的目标。详细介绍了问题背景、算法实现及关键步骤。

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 }

 

转载于:https://www.cnblogs.com/gj-Acit/p/3452033.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值