http://acm.hdu.edu.cn/showproblem.php?pid=3466
题目大意是说n个物品每个物品的花费是p,但是如果你现在的钱少于q就买不了这个物品,每个物品的价值是v,求有钱M时的最大价值。
一看这个题,就觉得直接按p背包还是按q背包都不对,然后就没有然后了。。。
然后看了题解:是说按q-p贪心,其实是这样,每次取q-p最小的,那么每次留下的自然就是最多的金钱。
至于严格的证明。。。。。。。待研究
剩下的就是01背包了。。,。。
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define MAX(a,b) (a > b ? a : b)
#define MIN(a,b) (a < b ? a : b)
#define mem0(a) memset(a,0,sizeof(a)) typedef long long LL;
const double eps = 1e-;
const int MAXN = ;
const int MAXM = ; struct NODE
{
int p,q;
int val;
}item[MAXN];
int N, M, DP[MAXM]; int cmp(NODE a, NODE b)
{
return a.q-a.p < b.q-b.p;
} int main()
{
while(~scanf("%d %d", &N, &M))
{
mem0(DP);
for(int i=;i<N;i++)scanf("%d %d %d", &item[i].p, &item[i].q, &item[i].val);
sort(item, item+N, cmp);
for(int i=;i<N;i++)
{
for(int j=M;j>=item[i].q;j--)
{
DP[j] = max(DP[j], DP[j-item[i].p]+item[i].val);
}
}
printf("%d\n", DP[M]);
}
return ;
}