完全背包
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int maxn = 10000 + 10; int dp[maxn]; const int INF = 0x7FFFFFFF; int main() { int A, B, C, H, X, i, T; scanf("%d", &T); while (T--) { scanf("%d%d%d", &A, &B, &C); scanf("%d%d", &H, &X); for (i = 0; i <= 10000 + 8; i++) dp[i] = INF; dp[0] = 0; for (i = 1; i <= H + 8; i++) dp[i] = min(dp[i], dp[i - 1] + A); for (i = 2; i <= H + 8; i++) dp[i] = min(dp[i], dp[i - 2] + B); for (i = 5; i <= H + 8; i++) dp[i] = min(dp[i], dp[i - 5] + C); int ans = INF; for (i = H; i <= H + 8; i++) if (ans > dp[i]) ans = dp[i]; printf("%d\n", ans); memset(dp, 0, sizeof(dp)); for (i = A; i <= X; i++) dp[i] = max(dp[i], dp[i - A] + 1); for (i = B; i <= X; i++) dp[i] = max(dp[i], dp[i - B] + 2); for (i = C; i <= X; i++) dp[i] = max(dp[i], dp[i - C] + 5); printf("%d\n", dp[X]); } return 0; }
时间: 2024-10-12 16:16:07