JZOJ 3223. 【HBOI2013】Ede的新背包问题
JZOJ 3223. 【HBOI2013】Ede的新背包问题
3223. 【HBOI2013】Ede的新背包问题 (Standard IO)
1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #define N 2007 5 #define LL long long 6 using namespace std; 7 int n, q, w[N], v[N], c[N]; 8 LL f[N][N], g[N][N]; 9 10 int max(int a, int b) 11 { 12 if (a > b) return a; 13 return b; 14 } 15 16 void Init() 17 { 18 scanf("%d", &n); 19 for (int i = 1; i <= n; i++) 20 scanf("%d%d%d", &v[i], &w[i], &c[i]); 21 } 22 23 void Dp() 24 { 25 for (int i = 1; i <= n; i++) 26 for (int j = 1000; j >= 0; j--) 27 { 28 f[i][j] = f[i - 1][j]; 29 for (int k = 1; k <= c[i]; k++) 30 if (j >= k * v[i]) f[i][j] = max(f[i][j], f[i - 1][j - k * v[i]] + k * w[i]); 31 else break; 32 } 33 memset(g, 0, sizeof(g)); 34 for (int i = n; i >= 1; i--) 35 for (int j = 1000; j >= 0; j--) 36 { 37 g[i][j] = g[i + 1][j]; 38 for (int k = 1; k <= c[i]; k++) 39 if (j >= k * v[i]) g[i][j] = max(g[i][j], g[i + 1][j - k * v[i]] + k * w[i]); 40 else break; 41 } 42 } 43 44 int main() 45 { 46 Init(); 47 Dp(); 48 scanf("%d", &q); 49 for (; q--; ) 50 { 51 int m, ban; 52 scanf("%d%d", &ban, &m); 53 int ans = 0; 54 for (int i = 0; i <= m; i++) 55 ans = max(ans, f[ban][m - i] + g[ban + 2][i]); 56 printf("%d\n", ans); 57 } 58 }
View Code