[luoguP2224] [HNOI2001]产品加工(背包DP)
f[i][j]表示第一个机器耗时j,第二个机器耗时f[i][j]
第一维可以滚掉
#include <cstdio> #include <cstring> #include <iostream> #define INF 1e9 #define min(x, y) ((x) < (y) ? (x) : (y)) #define max(x, y) ((x) > (y) ? (x) : (y)) using namespace std; int n, m, now = 0, last = 1, ans = 1e9; int f[2][30011]; inline int read() { int x = 0, f = 1; char ch = getchar(); for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1; for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0'; return x * f; } int main() { int i, j, x, y, z; n = read(); for(i = 1; i <= n; i++) { x = read(), y = read(), z = read(); m += max(x, max(y, z)); if(!x) x = INF; if(!y) y = INF; if(!z) z = INF; now ^= 1, last ^= 1; memset(f[now], 127 / 3, sizeof(f[now])); for(j = m; j >= 0; j--) { if(j >= x) f[now][j] = min(f[now][j], f[last][j - x]); f[now][j] = min(f[now][j], f[last][j] + y); if(j >= z) f[now][j] = min(f[now][j], f[last][j - z] + z); } } for(i = m; i >= 0; i--) ans = min(ans, max(i, f[now][i])); printf("%d\n", ans); return 0; }