放到二分和分治小测题里还以为是和跳石头差不多的二分题,没想到就是个dp……emmmmmm
就当复习dp了8 . 趴
(早知道多做做综合题了,还是这种题更能检测会不会,原理清不清楚啊。
#include<bits/stdc++.h> using namespace std; const int N = 1050; inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int n,vv,x; int v[N],w[N],dp[N]; int main() { n = read(); vv = read(); for(int i = 1; i <= n; i++){ cin >> v[i] >> w[i] >> x; if( x == -1 ){ //01背包 for(int j = vv; j >= v[i]; j--) dp[j] = max(dp[j], dp[j-v[i]] + w[i]); } else if( x == 0){ //完全背包 for(int j = v[i];j <= vv;j++) dp[j] = max(dp[j], dp[j-v[i]]+w[i]); } else if( x > 0){ //多重背包 for(int j = 1; j <= x; j++) for(int z = vv; z >= v[i]; z--) dp[z]=max(dp[z],dp[z-v[i]]+w[i]); } } printf("%d",dp[vv]); }