-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
110 lines (96 loc) · 2.68 KB
/
main.cpp
File metadata and controls
110 lines (96 loc) · 2.68 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
class Result {
public:
int avail_;
std::vector<int> result_;
};
class Order {
public:
void fillFromFile(std::string file_name) {
std::string line;
std::ifstream file_input(file_name);
if (file_input.is_open()) {
std::getline(file_input, line);
std::stringstream first_ss(line);
std::string m_token, n_token;
std::getline(first_ss, m_token, ' ');
std::getline(first_ss, n_token, ' ');
m_ = std::stoi(m_token);
n_ = std::stoi(n_token);
while (std::getline(file_input, line)) {
std::stringstream ss(line);
std::string token;
while (std::getline(ss, token, ' ')) {
slices_.emplace_back(std::stoi(token));
}
}
}
}
void greedy() {
int size = slices_.size() - 1;
int max = m_;
Result res;
res.avail_ = 0;
for (int i = size; i >= 0; --i) {
if ((max - slices_[i]) >= 0) {
max -= slices_[i];
res.result_.emplace_back(i);
res.avail_ += slices_[i];
}
}
results = res;
}
void testFast() { results = recurcive(slices_, m_); }
Result recurcive(std::vector<int> slices, int avail) {
Result res;
static std::map<std::vector<int>, Result> memo;
if (memo.find({(int)slices.size(), avail}) != memo.end()) {
res = memo[{(int)slices.size(), avail}];
} else if ((slices.size() == 0) || (avail == 0)) {
res.avail_ = 0;
res.result_ = {};
} else if (slices[0] > avail) {
slices.erase(slices.begin());
res = recurcive(slices, avail);
} else {
int nextItem = slices[0];
std::vector<int> copy1{slices.begin() + 1, slices.end()};
std::vector<int> copy2{slices.begin() + 1, slices.end()};
auto toTake = recurcive(copy1, avail - nextItem);
int withVal = toTake.avail_;
std::vector<int> withToTake = toTake.result_;
withVal += nextItem;
auto notToTake = recurcive(copy2, avail);
int withoutVal = notToTake.avail_;
std::vector<int> withoutToTake = notToTake.result_;
if (withVal > withoutVal) {
withToTake.emplace_back(nextItem);
res.avail_ = withVal;
res.result_ = withToTake;
} else {
res.avail_ = withoutVal;
res.result_ = withoutToTake;
}
memo[{(int)slices.size(), avail}] = res;
}
return res;
}
int m_;
int n_;
std::vector<int> slices_;
Result results;
};
int main(int argc, char** argv) {
Order o;
o.fillFromFile(argv[1]);
o.testFast();
std::cout << o.results.avail_ << std::endl;
for (auto x : o.results.result_) {
std::cout << x << std::endl;
}
return 0;
}