-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem0012.h
More file actions
58 lines (51 loc) · 1.64 KB
/
Problem0012.h
File metadata and controls
58 lines (51 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
// Created by Fengwei Zhang on 2021/7/11.
//
#ifndef ACWINGSOLUTION_PROBLEM0012_H
#define ACWINGSOLUTION_PROBLEM0012_H
#include <iostream>
#include <cstring>
using namespace std;
class Problem0012 {
// 求具体方案时,不能使用压缩空间后的DP数组
// f[i][j]:从第i个元素到最后一个元素总容量为j的最优解;这样可以反序求最优解,保证字典序最小:可以选择第1个物品时,一定要选上
// https://www.acwing.com/solution/content/2687/
private:
struct Item {
int v;
int w;
};
void knapsack(Item items[], const int n, const int m) {
int f[n + 2][m + 1];
memset(f, 0, sizeof f);
for (int i = n; i >= 1; --i) {
for (int j = m; j >= 0; --j) { // 体积从0开始循环,不能跳过[0, items[i].v)
f[i][j] = f[i + 1][j];
if (j < items[i].v) {
continue;
}
f[i][j] = max(f[i][j], f[i + 1][j - items[i].v] + items[i].w);
}
}
auto current_v = m;
for (int i = 1; i <= n; ++i) {
if (current_v < items[i].v || f[i][current_v] != f[i + 1][current_v - items[i].v] + items[i].w) {
continue;
}
printf("%d ", i);
current_v -= items[i].v;
}
printf("\n");
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
Item items[n + 1];
for (int i = 1; i <= n; ++i) {
scanf("%d%d", &items[i].v, &items[i].w);
}
knapsack(items, n, m);
return 0;
}
};
#endif //ACWINGSOLUTION_PROBLEM0012_H