summaryrefslogtreecommitdiff
path: root/acwing
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-02-24 14:55:08 +0800
committercrupest <crupest@outlook.com>2021-02-24 14:55:08 +0800
commit9ac8e6f0fc6797c46396e40b794c2ffab8431d44 (patch)
tree9b6e9746e7dc762926bf661b963e7e69b46f5ac2 /acwing
parent48043930d0b591c5e4a0d63cb937e2f77ab4df35 (diff)
downloadsolutions-9ac8e6f0fc6797c46396e40b794c2ffab8431d44.tar.gz
solutions-9ac8e6f0fc6797c46396e40b794c2ffab8431d44.tar.bz2
solutions-9ac8e6f0fc6797c46396e40b794c2ffab8431d44.zip
Add acwing problem 3 (aka. complete pack problem).
Diffstat (limited to 'acwing')
-rw-r--r--acwing/3-2.cpp29
-rw-r--r--acwing/3.cpp29
2 files changed, 58 insertions, 0 deletions
diff --git a/acwing/3-2.cpp b/acwing/3-2.cpp
new file mode 100644
index 0000000..6565c5b
--- /dev/null
+++ b/acwing/3-2.cpp
@@ -0,0 +1,29 @@
+#include <algorithm>
+#include <iostream>
+
+int N, V;
+int v[1001];
+int w[1001];
+int states[1001];
+
+int main() {
+ std::cin >> N >> V;
+
+ for (int i = 1; i <= N; i++) {
+ std::cin >> v[i] >> w[i];
+ }
+
+ for (int i = 1; i <= N; i++) {
+ for (int j = 0; j <= V; j++) {
+ if (j >= v[i]) {
+ states[j] = std::max(states[j], states[j - v[i]] + w[i]);
+ } else {
+ states[j] = states[j];
+ }
+ }
+ }
+
+ std::cout << states[V];
+
+ return 0;
+}
diff --git a/acwing/3.cpp b/acwing/3.cpp
new file mode 100644
index 0000000..21bd8dc
--- /dev/null
+++ b/acwing/3.cpp
@@ -0,0 +1,29 @@
+#include <algorithm>
+#include <iostream>
+
+int N, V;
+int v[1001];
+int w[1001];
+int states[1001][1001];
+
+int main() {
+ std::cin >> N >> V;
+
+ for (int i = 1; i <= N; i++) {
+ std::cin >> v[i] >> w[i];
+ }
+
+ for (int i = 1; i <= N; i++) {
+ for (int j = 0; j <= V; j++) {
+ if (j >= v[i]) {
+ states[i][j] = std::max(states[i - 1][j], states[i][j - v[i]] + w[i]);
+ } else {
+ states[i][j] = states[i - 1][j];
+ }
+ }
+ }
+
+ std::cout << states[N][V];
+
+ return 0;
+}