diff options
author | crupest <crupest@outlook.com> | 2021-02-24 15:39:50 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-02-24 15:39:50 +0800 |
commit | aea8b723789575aaa57b809369e183e22b4c1cf9 (patch) | |
tree | a1321197d96ed8861235e1ed665eef922df6c5c0 /works | |
parent | af32dd10a12e007f92fad878322893e227f6c690 (diff) | |
download | crupest-aea8b723789575aaa57b809369e183e22b4c1cf9.tar.gz crupest-aea8b723789575aaa57b809369e183e22b4c1cf9.tar.bz2 crupest-aea8b723789575aaa57b809369e183e22b4c1cf9.zip |
import(solutions): Add acwing problem 7.
Diffstat (limited to 'works')
-rw-r--r-- | works/solutions/acwing/5.cpp | 4 | ||||
-rw-r--r-- | works/solutions/acwing/7.cpp | 54 |
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;
+}
|