UVA-624 CD

一道不难但是烦并需要注意细节的题目…
代码的第28行, 最后才想起来加上…

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
#include <iostream>
#include <vector>

using namespace std;

struct E {
int sum;
vector<int> path;
};

int main() {

int n, tracks_len, tracks[25];

while (cin >> n) {
vector<E> s[25];
E ans;
ans.sum = 0;

cin >> tracks_len;
for (int i = 0; i < tracks_len; ++i) {
cin >> tracks[i];
E e;
e.sum = tracks[i];
e.path.push_back(i);
s[i].push_back(e);

if (e.sum <= n && e.sum > ans.sum) {
ans = e;
}
}

for (int i = 0; i < tracks_len; ++i) {
for (int j = 0; j < i; ++j) {
for (auto k : s[j]) {
/* cout << k.path.size() << endl; */
/* if (find(k.path.begin(), k.path.end(), j) != k.path.end()) */
/* continue; */
k.sum += tracks[i];
k.path.push_back(i);
s[i].push_back(k);

if (k.sum <= n && k.sum > ans.sum) {
ans = k;
}
}
}
}

for (auto i : ans.path) {
cout << tracks[i] << " ";
}
cout << "sum:";
cout << ans.sum << endl;



}
return 0;
}
Author: comwrg
Link: https://comwrg.github.io/2019/03/23/UVA-624/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.