标签:次方 潜水 https 预算 等价 href printf 动态 分组背包
void solve() {
for(int i = 1; i <= N; i++) {
for(int j = 0; j <= V; j++) {
dp[i][j] = dp[i - 1][j];
if(v[i] <= j) dp[i][j] = max(dp[i][j], dp[i - 1][j - v[i]] + w[i]);
}
}
printf("%d\n", dp[N][V]);
}
void solve() {
for(int i = 1; i <= N; i++)
for(int j = V; j >= v[i]; j--)
dp[j] = max(dp[j], dp[j - v[i]] + w[i]);
printf("%d\n", dp[V]);
}
void solve() {
for(int i = 1; i <= n; i++) // 枚举所有用到物品
for(int j = 0; j <= V; j++) // 枚举所有体积
for(int k = 0; k * v[i] <= j; k++) // 枚举每件物品用到的次数
dp[i][j] = max(dp[i][j], dp[i - 1][j - k * v[i]] + w[i] * k);
cout << dp[n][V];
}
\(dp(i, j) = dp(i - 1, j - v_i \times k) + w_i \times k\)
\(dp(i, j)\)展开式:
\(dp(i, j) = max(dp(i - 1, j), dp(i - 1, j - v_i) + w_i, dp(i - 1,j - 2 \times v_i + 2 \times w_i,...)\)
\(dp(i,j - v_i) = max(dp(i - 1,j - v), dp(i - 1,j - 2 \times v_i + w_i, ...)\)
由以上两式可得:
\(dp(i, j) = max(dp(i - 1, j), dp(i, j - v_i) + w_i)\)
void solve() {
for(int i = 1; i <= n; i++) {
for(int j = 0; j <= V; j++) {
dp[i][j] = dp[i - 1][j];
if(j >= v[i]) dp[i][j] = max(dp[i][j], dp[i][j - v[i]] + w[i]);
}
}
cout << dp[n][V];
}
void solve() {
for(int i = 1; i <= n; i++) {
for(int j = v[i]; j <= V; j++)
dp[j] = max(dp[j], dp[j - v[i]] + w[i]);
}
cout << dp[V];
}
s[i]
个void solve() {
for(int i = 1; i <= n; i++)
for(int j = 0; j <= V; j++)
for(int k = 0; k <= s[i] && k * v[i] <= j; k++)
dp[i][j] = max(dp[i][j], dp[i - 1][j - k * v[i]] + w[i] * k);
cout << dp[n][V];
}
void solve() {
int cnt = 0;
for(int i = 0; i < n; i++) {
int a, b, c; scanf("%d%d%d", &a, &b, &c);
int k = 1;
while(k <= c) {
cnt++;
v[cnt] = k * a;
w[cnt] = k * b;
c -= k;
k *= 2;
}
if(c) { // 2^k + c == v
cnt++;
v[cnt] = a * c;
w[cnt] = b * c;
}
}
for(int i = 1; i <= cnt; i++)
for(int j = m; j >= v[i]; j--)
dp[j] = max(dp[j], dp[j - v[i]] + w[i]);
printf("%d\n", dp[m]);
}
void solve() {
for(int i = 1; i <= n; i++) {
scanf("%d", &s[i]);
for(int j = 0; j < s[i]; j++)
scanf("%d%d", &v[i][j], &w[i][j]);
}
for(int i = 1; i <= n; i++)
for(int j = m; j >= 0; j--)
for(int k = 0; k < s[i]; k++)
if(v[i][k] <= j)
dp[j] = max(dp[j], dp[j - v[i][k]] + w[i][k]);
printf("%d\n", dp[m]);
}
int x, y; scanf("%d%d", &x, &y);
int n; scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d%d%d", &o2[i], &n2[i], &v[i]);
memset(dp, 0x3f, sizeof dp);
dp[0][0] = 0;
for(int i = 1; i <= n; i++) {
for(int j = x; j >= 0; j--) {
for(int k = y; k >= 0; k--) {
// 当j - o2[i] < 0 时,表示氧气体积至少为j - o2[i],此时该状态是合法的,但该状态数为0,氮气同理
dp[j][k] = min(dp[j][k], dp[max(0, j - o2[i])][max(0, k - n2[i])] + v[i]);
}
}
}
printf("%d", dp[x][y]);
标签:次方 潜水 https 预算 等价 href printf 动态 分组背包
原文地址:https://www.cnblogs.com/Hot-machine/p/13283398.html