aboutsummaryrefslogtreecommitdiff
path: root/works/solutions
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-02-24 15:39:50 +0800
committercrupest <crupest@outlook.com>2021-02-24 15:39:50 +0800
commita7a2582fc75aa1b2d4e56a01e07174f4083f203d (patch)
tree480081e2a84d16aba4162ccc1178b8e4ce0cdaec /works/solutions
parent3a3f0c3b0a19306bc9dfd901516de1c9c9d6f3ef (diff)
downloadcrupest-a7a2582fc75aa1b2d4e56a01e07174f4083f203d.tar.gz
crupest-a7a2582fc75aa1b2d4e56a01e07174f4083f203d.tar.bz2
crupest-a7a2582fc75aa1b2d4e56a01e07174f4083f203d.zip
import(solutions): Add acwing problem 7.
Diffstat (limited to 'works/solutions')
-rw-r--r--works/solutions/acwing/5.cpp4
-rw-r--r--works/solutions/acwing/7.cpp54
2 files changed, 55 insertions, 3 deletions
diff --git a/works/solutions/acwing/5.cpp b/works/solutions/acwing/5.cpp
index e451a2d..a88fba6 100644
--- a/works/solutions/acwing/5.cpp
+++ b/works/solutions/acwing/5.cpp
@@ -39,9 +39,7 @@ int main() {
k *= 2;
}
- if (amount != 0) {
- ZeroOnePack(amount * v[i], amount * w[i]);
- }
+ ZeroOnePack(amount * v[i], amount * w[i]);
}
}
diff --git a/works/solutions/acwing/7.cpp b/works/solutions/acwing/7.cpp
new file mode 100644
index 0000000..fcc0fb1
--- /dev/null
+++ b/works/solutions/acwing/7.cpp
@@ -0,0 +1,54 @@
+#include <algorithm>
+#include <iostream>
+
+int N, V;
+int v[1001];
+int w[1001];
+int s[1001];
+int states[1001];
+
+void CompletePack(int v, int w) {
+ for (int j = v; j <= V; j++) {
+ states[j] = std::max(states[j], states[j - v] + w);
+ }
+}
+
+void ZeroOnePack(int v, int w) {
+ for (int j = V; j >= v; j--) {
+ states[j] = std::max(states[j], states[j - v] + w);
+ }
+}
+
+void MultiplePack(int v, int w, int s) {
+ int k = 1;
+
+ while (k < s) {
+ ZeroOnePack(k * v, k * w);
+ s -= k;
+ k *= 2;
+ }
+
+ ZeroOnePack(s * v, s * w);
+}
+
+int main() {
+ std::cin >> N >> V;
+
+ for (int i = 1; i <= N; i++) {
+ std::cin >> v[i] >> w[i] >> s[i];
+ }
+
+ for (int i = 1; i <= N; i++) {
+ if (s[i] < 0) {
+ ZeroOnePack(v[i], w[i]);
+ } else if (s[i] == 0) {
+ CompletePack(v[i], w[i]);
+ } else {
+ MultiplePack(v[i], w[i], s[i]);
+ }
+ }
+
+ std::cout << states[V];
+
+ return 0;
+}