summaryrefslogtreecommitdiff
path: root/PlaygroundCpp/Sources
diff options
context:
space:
mode:
Diffstat (limited to 'PlaygroundCpp/Sources')
-rw-r--r--PlaygroundCpp/Sources/Playground.cpp79
1 files changed, 29 insertions, 50 deletions
diff --git a/PlaygroundCpp/Sources/Playground.cpp b/PlaygroundCpp/Sources/Playground.cpp
index e402792..50297b2 100644
--- a/PlaygroundCpp/Sources/Playground.cpp
+++ b/PlaygroundCpp/Sources/Playground.cpp
@@ -1,67 +1,46 @@
#include <iostream>
-#include <algorithm>
#include <vector>
//#include <gtest/gtest.h>
using std::vector;
-int lcs3(vector<int> &a, vector<int> &b, vector<int> &c) {
- int l = c.size();
- int m = b.size();
- int n = a.size();
- int D[n + 1][m + 1][l + 1];
- for (int k = 0; k < l + 1; k++) {
- for (int j = 0; j < m + 1; j++) {
- for (int i = 0; i < n + 1; i++) {
- if (i == 0 || j == 0 || k == 0) {
- D[i][j][k] = 0;
- continue;
- }
- if (a[i - 1] == b[j - 1] && b[j - 1] == c[k - 1]) {
- D[i][j][k] = D[i - 1][j - 1][k - 1] + 1;
- } else {
- D[i][j][k] = std::max(D[i - 1][j][k],
- std::max(D[i][j - 1][k], D[i][j][k - 1]));
+int optimalWeight(int W, vector<int> &w2) {
+ int j = w2.size();
+// int value[W + 1][j + 1];
+ vector< vector<int> > value(W + 1, vector<int>(j + 1));
+
+ for (int i = 0; i < j + 1; i++) {
+ for (int w = 0; w < W + 1; w++) {
+ if (i == 0 || w == 0) {
+ value[w][i] = 0;
+ continue;
+ }
+ value[w][i] = value[w][i - 1];
+ if (w2[i - 1] <= w) {
+ int val = value[w - w2[i - 1]][i - 1] + w2[i - 1];
+ if (value[w][i] < val) {
+ value[w][i] = val;
}
}
}
+
}
- return D[n][m][l];
+ return value[W][j];
}
-//TEST(LCS3, t1) {
-// vector<int> a = { 1, 2, 3 };
-// vector<int> b = { 2, 1, 3 };
-// vector<int> c = { 1, 3, 5 };
-// ASSERT_EQ(2, lcs3(a, b, c));
-//}
-//
-//TEST(LCS3, t2) {
-// vector<int> a = { 8, 3, 2, 1, 7 };
-// vector<int> b = { 8, 2, 1, 3, 8, 10, 7 };
-// vector<int> c = { 6, 8, 3, 1, 4, 7 };
-// ASSERT_EQ(3, lcs3(a, b, c));
+//TEST(Knapsack, t1) {
+// vector<int> w = { 1, 4, 8 };
+// int W = 10;
+// ASSERT_EQ(9, optimalWeight(W, w));
//}
int main() {
- size_t an;
- std::cin >> an;
- vector<int> a(an);
- for (size_t i = 0; i < an; i++) {
- std::cin >> a[i];
- }
- size_t bn;
- std::cin >> bn;
- vector<int> b(bn);
- for (size_t i = 0; i < bn; i++) {
- std::cin >> b[i];
- }
- size_t cn;
- std::cin >> cn;
- vector<int> c(cn);
- for (size_t i = 0; i < cn; i++) {
- std::cin >> c[i];
- }
- std::cout << lcs3(a, b, c) << std::endl;
+ int n, W;
+ std::cin >> W >> n;
+ vector<int> w(n);
+ for (int i = 0; i < n; i++) {
+ std::cin >> w[i];
+ }
+ std::cout << optimalWeight(W, w) << '\n';
}