/* Tug of War
Autor: Piotr Smulewicz
*/
#include <iostream>
#include <numeric>
#include <vector>
#include <cstdlib>
using namespace std;
const int SIZE = 30001;
const int MAX_STRENGHT = 20;
const int BACK_PACK_SIZE = SIZE * MAX_STRENGHT + 1;
int persons[SIZE * 2][2];
int str[SIZE * 2];
bool assigned[SIZE * 2];
vector<int> spots[SIZE * 2];
int item[BACK_PACK_SIZE];
unsigned int opt[SIZE * 2];
bool back_pack[BACK_PACK_SIZE + 1];
int n, j, s, a, b, pe, mi, k, sum_all, sum, res;
int main() {
ios_base::sync_with_stdio(false);
res = BACK_PACK_SIZE;
cin >> n >> k;
for (size_t i = 0; i < n * 2u; i++) {
cin >> a >> b >> s;
str[i] = s;
persons[i][0] = a * 2;
persons[i][1] = b * 2 + 1;
spots[a * 2].push_back(i);
spots[b * 2 + 1].push_back(i);
}
for (size_t s_pe = 0; s_pe < 2u * n; s_pe++) {
pe = s_pe;
while (!assigned[pe]) {
for (j = 0; j < 2; j++) {
mi = persons[pe][j];
if (opt[mi] + 1 == spots[mi].size()) {
assigned[pe] = true;
sum += (1 - 2 * (mi % 2)) * str[pe];
mi = persons[pe][1 - j];
opt[mi]++;
break;
}
}
if (spots[mi].size() == opt[mi] + 1) {
unsigned int poz = 0;
while (assigned[spots[mi][poz]] && poz < spots[mi].size())
poz++;
if (poz < spots[mi].size())
pe = spots[mi][poz];
else
break;
} else
break;
}
}
item[abs(sum)]++;
sum_all += abs(sum);
for (int s_pe = 0; s_pe < 2 * n; s_pe++) {
sum = 0;
pe = s_pe;
int st = 0;
int mi2 = persons[pe][1];
if (assigned[pe])
continue;
while (!assigned[pe]) {
sum += (1 - 2 * st) * str[pe];
assigned[pe] = true;
mi = persons[pe][st];
size_t poz = 0;
while (poz < spots[mi].size() && assigned[spots[mi][poz]])
poz++;
if (poz < spots[mi].size())
pe = spots[mi][poz];
st = 1 - st;
}
if (mi != mi2) {
cout << "NO" << endl;
return 0;
}
if (sum != 0)
item[abs(sum)]++;
sum_all += abs(sum);
}
back_pack[0] = true;
for (size_t i = 0; i < BACK_PACK_SIZE*1u; i++) {
if (item[i] == 0u)
continue;
// cout<<"it"<<item[i]<<endl;
for (int j = BACK_PACK_SIZE; j >= 0; --j) {
if (back_pack[j]) {
for (int l = 1; l <= item[i] && back_pack[j + l * i] == false; l++)
back_pack[j + l * i] = true;
}
}
}
for (int j = BACK_PACK_SIZE; j >= 0; --j) {
if (back_pack[j])
res = min(res, abs(sum_all - 2 * j));
}
// cout<<"res "<<res<<" k: "<<k<<endl;
if (res <= k)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7512 KB |
Output is correct |
2 |
Correct |
6 ms |
7512 KB |
Output is correct |
3 |
Correct |
6 ms |
7512 KB |
Output is correct |
4 |
Correct |
6 ms |
7512 KB |
Output is correct |
5 |
Correct |
3 ms |
7512 KB |
Output is correct |
6 |
Correct |
3 ms |
7512 KB |
Output is correct |
7 |
Correct |
6 ms |
7512 KB |
Output is correct |
8 |
Correct |
6 ms |
7512 KB |
Output is correct |
9 |
Correct |
6 ms |
7512 KB |
Output is correct |
10 |
Correct |
6 ms |
7512 KB |
Output is correct |
11 |
Correct |
6 ms |
7512 KB |
Output is correct |
12 |
Correct |
6 ms |
7512 KB |
Output is correct |
13 |
Correct |
6 ms |
7512 KB |
Output is correct |
14 |
Correct |
6 ms |
7512 KB |
Output is correct |
15 |
Correct |
6 ms |
7512 KB |
Output is correct |
16 |
Correct |
3 ms |
7512 KB |
Output is correct |
17 |
Correct |
6 ms |
7512 KB |
Output is correct |
18 |
Correct |
6 ms |
7512 KB |
Output is correct |
19 |
Correct |
6 ms |
7512 KB |
Output is correct |
20 |
Correct |
3 ms |
7512 KB |
Output is correct |
21 |
Correct |
0 ms |
7512 KB |
Output is correct |
22 |
Correct |
3 ms |
7512 KB |
Output is correct |
23 |
Correct |
3 ms |
7512 KB |
Output is correct |
24 |
Correct |
6 ms |
7512 KB |
Output is correct |
25 |
Correct |
6 ms |
7512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7512 KB |
Output is correct |
2 |
Correct |
6 ms |
7512 KB |
Output is correct |
3 |
Correct |
6 ms |
7512 KB |
Output is correct |
4 |
Correct |
6 ms |
7512 KB |
Output is correct |
5 |
Correct |
3 ms |
7512 KB |
Output is correct |
6 |
Correct |
3 ms |
7512 KB |
Output is correct |
7 |
Correct |
6 ms |
7512 KB |
Output is correct |
8 |
Correct |
6 ms |
7512 KB |
Output is correct |
9 |
Correct |
6 ms |
7512 KB |
Output is correct |
10 |
Correct |
6 ms |
7512 KB |
Output is correct |
11 |
Correct |
6 ms |
7512 KB |
Output is correct |
12 |
Correct |
6 ms |
7512 KB |
Output is correct |
13 |
Correct |
6 ms |
7512 KB |
Output is correct |
14 |
Correct |
6 ms |
7512 KB |
Output is correct |
15 |
Correct |
6 ms |
7512 KB |
Output is correct |
16 |
Correct |
3 ms |
7512 KB |
Output is correct |
17 |
Correct |
6 ms |
7512 KB |
Output is correct |
18 |
Correct |
6 ms |
7512 KB |
Output is correct |
19 |
Correct |
6 ms |
7512 KB |
Output is correct |
20 |
Correct |
3 ms |
7512 KB |
Output is correct |
21 |
Correct |
0 ms |
7512 KB |
Output is correct |
22 |
Correct |
3 ms |
7512 KB |
Output is correct |
23 |
Correct |
3 ms |
7512 KB |
Output is correct |
24 |
Correct |
6 ms |
7512 KB |
Output is correct |
25 |
Correct |
6 ms |
7512 KB |
Output is correct |
26 |
Correct |
13 ms |
7644 KB |
Output is correct |
27 |
Correct |
159 ms |
7644 KB |
Output is correct |
28 |
Correct |
16 ms |
7644 KB |
Output is correct |
29 |
Correct |
149 ms |
7644 KB |
Output is correct |
30 |
Correct |
9 ms |
7644 KB |
Output is correct |
31 |
Correct |
159 ms |
7644 KB |
Output is correct |
32 |
Correct |
13 ms |
7644 KB |
Output is correct |
33 |
Correct |
153 ms |
7644 KB |
Output is correct |
34 |
Correct |
9 ms |
7644 KB |
Output is correct |
35 |
Correct |
149 ms |
7644 KB |
Output is correct |
36 |
Correct |
16 ms |
7644 KB |
Output is correct |
37 |
Correct |
159 ms |
7644 KB |
Output is correct |
38 |
Correct |
13 ms |
7644 KB |
Output is correct |
39 |
Correct |
149 ms |
7644 KB |
Output is correct |
40 |
Correct |
16 ms |
7644 KB |
Output is correct |
41 |
Correct |
156 ms |
7644 KB |
Output is correct |
42 |
Correct |
13 ms |
7644 KB |
Output is correct |
43 |
Correct |
149 ms |
7644 KB |
Output is correct |
44 |
Correct |
23 ms |
7644 KB |
Output is correct |
45 |
Correct |
156 ms |
7644 KB |
Output is correct |
46 |
Correct |
13 ms |
7644 KB |
Output is correct |
47 |
Correct |
3 ms |
7644 KB |
Output is correct |
48 |
Correct |
19 ms |
7644 KB |
Output is correct |
49 |
Correct |
16 ms |
7644 KB |
Output is correct |
50 |
Correct |
16 ms |
7644 KB |
Output is correct |
51 |
Correct |
16 ms |
7644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
8040 KB |
Output is correct |
2 |
Correct |
9 ms |
8040 KB |
Output is correct |
3 |
Correct |
6 ms |
8040 KB |
Output is correct |
4 |
Correct |
9 ms |
8040 KB |
Output is correct |
5 |
Correct |
6 ms |
8040 KB |
Output is correct |
6 |
Correct |
9 ms |
8040 KB |
Output is correct |
7 |
Correct |
6 ms |
8040 KB |
Output is correct |
8 |
Correct |
9 ms |
8040 KB |
Output is correct |
9 |
Correct |
13 ms |
8040 KB |
Output is correct |
10 |
Correct |
6 ms |
8040 KB |
Output is correct |
11 |
Correct |
13 ms |
8040 KB |
Output is correct |
12 |
Correct |
9 ms |
8040 KB |
Output is correct |
13 |
Correct |
13 ms |
8040 KB |
Output is correct |
14 |
Correct |
9 ms |
8040 KB |
Output is correct |
15 |
Correct |
9 ms |
8040 KB |
Output is correct |
16 |
Correct |
9 ms |
8040 KB |
Output is correct |
17 |
Correct |
9 ms |
8040 KB |
Output is correct |
18 |
Correct |
13 ms |
8040 KB |
Output is correct |
19 |
Correct |
9 ms |
8040 KB |
Output is correct |
20 |
Correct |
6 ms |
8040 KB |
Output is correct |
21 |
Correct |
9 ms |
8040 KB |
Output is correct |
22 |
Correct |
13 ms |
8172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7512 KB |
Output is correct |
2 |
Correct |
6 ms |
7512 KB |
Output is correct |
3 |
Correct |
6 ms |
7512 KB |
Output is correct |
4 |
Correct |
6 ms |
7512 KB |
Output is correct |
5 |
Correct |
3 ms |
7512 KB |
Output is correct |
6 |
Correct |
3 ms |
7512 KB |
Output is correct |
7 |
Correct |
6 ms |
7512 KB |
Output is correct |
8 |
Correct |
6 ms |
7512 KB |
Output is correct |
9 |
Correct |
6 ms |
7512 KB |
Output is correct |
10 |
Correct |
6 ms |
7512 KB |
Output is correct |
11 |
Correct |
6 ms |
7512 KB |
Output is correct |
12 |
Correct |
6 ms |
7512 KB |
Output is correct |
13 |
Correct |
6 ms |
7512 KB |
Output is correct |
14 |
Correct |
6 ms |
7512 KB |
Output is correct |
15 |
Correct |
6 ms |
7512 KB |
Output is correct |
16 |
Correct |
3 ms |
7512 KB |
Output is correct |
17 |
Correct |
6 ms |
7512 KB |
Output is correct |
18 |
Correct |
6 ms |
7512 KB |
Output is correct |
19 |
Correct |
6 ms |
7512 KB |
Output is correct |
20 |
Correct |
3 ms |
7512 KB |
Output is correct |
21 |
Correct |
0 ms |
7512 KB |
Output is correct |
22 |
Correct |
3 ms |
7512 KB |
Output is correct |
23 |
Correct |
3 ms |
7512 KB |
Output is correct |
24 |
Correct |
6 ms |
7512 KB |
Output is correct |
25 |
Correct |
6 ms |
7512 KB |
Output is correct |
26 |
Correct |
13 ms |
7644 KB |
Output is correct |
27 |
Correct |
159 ms |
7644 KB |
Output is correct |
28 |
Correct |
16 ms |
7644 KB |
Output is correct |
29 |
Correct |
149 ms |
7644 KB |
Output is correct |
30 |
Correct |
9 ms |
7644 KB |
Output is correct |
31 |
Correct |
159 ms |
7644 KB |
Output is correct |
32 |
Correct |
13 ms |
7644 KB |
Output is correct |
33 |
Correct |
153 ms |
7644 KB |
Output is correct |
34 |
Correct |
9 ms |
7644 KB |
Output is correct |
35 |
Correct |
149 ms |
7644 KB |
Output is correct |
36 |
Correct |
16 ms |
7644 KB |
Output is correct |
37 |
Correct |
159 ms |
7644 KB |
Output is correct |
38 |
Correct |
13 ms |
7644 KB |
Output is correct |
39 |
Correct |
149 ms |
7644 KB |
Output is correct |
40 |
Correct |
16 ms |
7644 KB |
Output is correct |
41 |
Correct |
156 ms |
7644 KB |
Output is correct |
42 |
Correct |
13 ms |
7644 KB |
Output is correct |
43 |
Correct |
149 ms |
7644 KB |
Output is correct |
44 |
Correct |
23 ms |
7644 KB |
Output is correct |
45 |
Correct |
156 ms |
7644 KB |
Output is correct |
46 |
Correct |
13 ms |
7644 KB |
Output is correct |
47 |
Correct |
3 ms |
7644 KB |
Output is correct |
48 |
Correct |
19 ms |
7644 KB |
Output is correct |
49 |
Correct |
16 ms |
7644 KB |
Output is correct |
50 |
Correct |
16 ms |
7644 KB |
Output is correct |
51 |
Correct |
16 ms |
7644 KB |
Output is correct |
52 |
Correct |
13 ms |
8040 KB |
Output is correct |
53 |
Correct |
9 ms |
8040 KB |
Output is correct |
54 |
Correct |
6 ms |
8040 KB |
Output is correct |
55 |
Correct |
9 ms |
8040 KB |
Output is correct |
56 |
Correct |
6 ms |
8040 KB |
Output is correct |
57 |
Correct |
9 ms |
8040 KB |
Output is correct |
58 |
Correct |
6 ms |
8040 KB |
Output is correct |
59 |
Correct |
9 ms |
8040 KB |
Output is correct |
60 |
Correct |
13 ms |
8040 KB |
Output is correct |
61 |
Correct |
6 ms |
8040 KB |
Output is correct |
62 |
Correct |
13 ms |
8040 KB |
Output is correct |
63 |
Correct |
9 ms |
8040 KB |
Output is correct |
64 |
Correct |
13 ms |
8040 KB |
Output is correct |
65 |
Correct |
9 ms |
8040 KB |
Output is correct |
66 |
Correct |
9 ms |
8040 KB |
Output is correct |
67 |
Correct |
9 ms |
8040 KB |
Output is correct |
68 |
Correct |
9 ms |
8040 KB |
Output is correct |
69 |
Correct |
13 ms |
8040 KB |
Output is correct |
70 |
Correct |
9 ms |
8040 KB |
Output is correct |
71 |
Correct |
6 ms |
8040 KB |
Output is correct |
72 |
Correct |
9 ms |
8040 KB |
Output is correct |
73 |
Correct |
13 ms |
8172 KB |
Output is correct |
74 |
Correct |
53 ms |
9360 KB |
Output is correct |
75 |
Correct |
716 ms |
9360 KB |
Output is correct |
76 |
Correct |
49 ms |
9360 KB |
Output is correct |
77 |
Correct |
719 ms |
9360 KB |
Output is correct |
78 |
Correct |
46 ms |
9360 KB |
Output is correct |
79 |
Correct |
59 ms |
9360 KB |
Output is correct |
80 |
Correct |
716 ms |
9360 KB |
Output is correct |
81 |
Correct |
59 ms |
9360 KB |
Output is correct |
82 |
Correct |
33 ms |
9360 KB |
Output is correct |
83 |
Correct |
49 ms |
9360 KB |
Output is correct |
84 |
Correct |
723 ms |
9360 KB |
Output is correct |
85 |
Correct |
66 ms |
9360 KB |
Output is correct |
86 |
Correct |
736 ms |
9360 KB |
Output is correct |
87 |
Correct |
46 ms |
9360 KB |
Output is correct |
88 |
Correct |
716 ms |
9360 KB |
Output is correct |
89 |
Correct |
46 ms |
9360 KB |
Output is correct |
90 |
Correct |
719 ms |
9360 KB |
Output is correct |
91 |
Correct |
63 ms |
9360 KB |
Output is correct |
92 |
Correct |
719 ms |
9360 KB |
Output is correct |
93 |
Correct |
53 ms |
9360 KB |
Output is correct |
94 |
Correct |
29 ms |
9228 KB |
Output is correct |
95 |
Correct |
56 ms |
9360 KB |
Output is correct |
96 |
Correct |
53 ms |
9360 KB |
Output is correct |
97 |
Correct |
49 ms |
9360 KB |
Output is correct |
98 |
Correct |
43 ms |
9360 KB |
Output is correct |