题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=747
每种食材只有两种操作,即取与不取, 又有在T时间内, 所以很容易想到0-1背包, 但是我们发现即使选的食材一样, 但选的顺序不一样获得美味指数也不样, 所以要先贪心求一下选的先后顺序, 然后在DP, 就AC了。
贪心的公式就是主要是一个算式:
a1-b1c1+a2-(c1+c2)b2 > a2-b2c2+a1-(c1+c2)b1
化简
b2c1<b1c2
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int M = 100000 + 10;
const int N = 50 + 5;
#define LL long long
struct Info
{
int a, b, c;
} sa[N];
int cmp(Info x, Info y)
{
return x.c*y.b < y.c*x.b;
}
LL dp[M];
int main()
{
int T, n;
while(~scanf("%d%d", &T, &n))
{
for(int i = 1; i <= n; i++)
scanf("%d%d%d", &sa[i].a, &sa[i].b, &sa[i].c);
sort(sa+1, sa+n+1, cmp);
memset(dp, -0x3f, sizeof(dp));
dp[0] = 0;
for(int i = 1; i <= n; i++)
for(int j = T; j-sa[i].c >= 0; j--)
dp[j] = max(dp[j], dp[j-sa[i].c]+sa[i].a-j*sa[i].b);
LL Max = 0;
for(int i = 1; i <= T; i++)
Max = max(Max, dp[i]);
printf("%lld\n", Max);
}
return 0;
}