思路:
背包dp
为了使最后的罚时最少,先按做题所需时间排序
运算符重载方便背包转移。
代码:
#include<bits/stdc++.h> using namespace std; #define fi first #define se second #define LL long long #define pb push_back #define pii pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) const int N = 1e3 + 10; pii t[55]; struct node { int x, y, z; bool operator < (const node & t) const { if(x == t.x) { if(y == t.y) { if(z == t.z) { return true; } else return z > t.z; } else return y < t.y; } else return x < t.x; } }dp[N]; int main() { int cs, T, n; scanf("%d", &cs); while(cs--) { scanf("%d%d", &T, &n); for (int i = 1; i <= n; i++) scanf("%d", &t[i].fi); for (int i = 1; i <= n; i++) scanf("%d", &t[i].se); sort(t+1, t+1+n); for (int j = 0; j <= T; j++) dp[j].x = dp[j].y = dp[j].z = 0; for (int i = 1; i <= n; i++) { for (int j = T; j >= t[i].fi; j--) { dp[j] = max(dp[j],node{dp[j-t[i].fi].x+t[i].se, dp[j-t[i].fi].y+1, dp[j-t[i].fi].z+j}); } } node ans = {0, 0, 0}; for (int j = 0; j <= T; j++) { ans = max(ans, dp[j]); } printf("%d %d %d ", ans.x, ans.y, ans.z); } return 0; }