传送门:点击打开链接
题意:类似cf的赛制,每道题目有A,B,C三个值,A表示初始分数,B表示每分钟题的分数会减少B,C表示做这道题需要C分钟,数据保证分数不会变为负数。现在给出比赛时长,问安排做题的顺序,求最大得分。
思路:这道题是一道非常经典的题。首先很容易想到一件事,给出的题哪个先做没有先后顺序,那么在动态规划的时候,必然有前后顺序,所以要么就是状态压缩,要么就是之前就把这些题目排序了,使得后面是有序的。状压不用考虑了因为数据太大,所以我们考虑要如何排序。
现在有A1,B1,C1和A2,B2,C2这两道题,如果先做1再做2的得分是A1-B1*C1+A2-B2*(C1+C2),如果先做2在做1的得分是A2-B2*C2+A1-B1*(C1+C2),令先做1再做2的得分更高些,那么有A1-B1*C1+A2-B2*(C1+C2) >= A2-B2*C2+A1-B1*(C1+C2),解得B1*C2>=B2*C1
所以,只要B1*C2>=B2*C1,那么先做题1再做题2的分数就会更高。
所以,我们只需要根据这个来排序,再做dp,就能得到答案了.
#include<map>
#include<set>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define fuck printf("fuck")
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w+",stdout)
using namespace std;
typedef long long LL;
const int MX = 3000 + 5;
int T, n, m;
int dp[MX];
struct Data {
int a, b, c;
Data() {}
Data(int _a, int _b, int _c) {
a = _a; b = _b; c = _c;
}
bool operator<(const Data &P) const {
return (LL)b * P.c > (LL)P.b * c;
}
} D[MX];
int main() {
//FIN;
scanf("%d", &T);
while(T--) {
memset(dp, 0, sizeof(dp));
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++) {
scanf("%d%d%d", &D[i].a, &D[i].b, &D[i].c);
}
sort(D + 1, D + 1 + n);
for(int i = 1; i <= n; i++) {
for(int j = m; j >= D[i].c; j--) {
dp[j] = max(dp[j], dp[j - D[i].c] + D[i].a - D[i].b * j);
}
}
int ans = 0;
for(int i = 0; i <= m; i++) {
ans = max(ans, dp[i]);
}
printf("%d\n", ans);
}
return 0;
}