#include "bits/stdc++.h"
using namespace std;
map<vector<int>, int> dp;
vector<int> a, b;
int solve(int A, int B, int c, int d) {
if (A == (int) a.size() && B == (int) b.size() && c == 0 && d == 0) {
return 0;
}
auto all = {A, B, c, d};
vector<int> upda, updb;
if (A < a.size()) upda = {A};
if (B < b.size()) updb = {B};
if (dp.find(all) == dp.end()) {
dp[all] = (int) -1e9;
for (auto i : upda) {
dp[all] = max(dp[all], -(a[i] - 4) - solve(A + 1, B, c, d + 1));
}
for (auto i : updb) {
auto tc = c, td = d;
int L = b[i] / 2, R = b[i] - L, res = 0;
if (L >= 2) {
tc++;
res += L - 2;
} else if (L) {
res++;
}
if (R >= 2) {
tc++;
res += R - 2;
} else if (R) {
res++;
}
dp[all] = max(dp[all], -res - solve(A, B + 1, tc, td));
}
if (c) {
dp[all] = max(dp[all], abs(2 + solve(A, B, c - 1, d)));
}
if (d) {
dp[all] = max(dp[all], abs(4 + solve(A, B, c, d - 1)));
}
}
return dp[all];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
auto get = [&] (int i, int j) {
return i * m + j;
};
auto check = [&] (int i, int j) {
return (i >= 0 && i < n && j >= 0 && j < m);
};
vector<vector<int>> g(n * m);
vector<int> cnt(n * m, 4);
auto add = [&] (int a, int b, int c, int d) {
if (check(a, b)) {
cnt[get(a, b)]--;
}
if (check(c, d)) {
cnt[get(c, d)]--;
}
if (check(a, b) && check(c, d)) {
g[get(a, b)].push_back(get(c, d));
g[get(c, d)].push_back(get(a, b));
}
};
for (int i = 0; i < n + 1; i++) {
for (int j = 0; j < m; j++) {
char c;
cin >> c;
if (c == '0') {
add(i - 1, j, i, j);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m + 1; j++) {
char c;
cin >> c;
if (c == '0') {
add(i, j - 1, i, j);
}
}
}
vector<int> used(n * m);
int c = 0, was;
function<void(int)> dfs = [&] (int v) {
used[v] = 1;
if ((int) g[v].size() == 1) {
was = 1;
}
c++;
for (auto u : g[v]) {
if (!used[u]) {
dfs(u);
}
}
};
vector<int> A, B, C, D;
for (int i = 0; i < n * m; i++) {
if (cnt[i] != 4 && !used[i]) {
c = 0, was = 0;
dfs(i);
if (was || c == 1) {
B.push_back(c);
} else {
A.push_back(c);
}
}
}
sort(A.begin(), A.end());
sort(B.begin(), B.end());
a = A, b = B;
cout << solve(0, 0, 0, 0);
}
Compilation message
game.cpp: In function 'int solve(int, int, int, int)':
game.cpp:15:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | if (A < a.size()) upda = {A};
| ~~^~~~~~~~~~
game.cpp:16:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | if (B < b.size()) updb = {B};
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
316 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
316 KB |
Output is correct |
18 |
Correct |
0 ms |
320 KB |
Output is correct |
19 |
Correct |
1 ms |
320 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
320 KB |
Output is correct |
23 |
Correct |
1 ms |
324 KB |
Output is correct |
24 |
Correct |
1 ms |
312 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
296 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
0 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
320 KB |
Output is correct |
20 |
Correct |
1 ms |
316 KB |
Output is correct |
21 |
Correct |
1 ms |
320 KB |
Output is correct |
22 |
Correct |
1 ms |
320 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
316 KB |
Output is correct |
42 |
Correct |
0 ms |
320 KB |
Output is correct |
43 |
Correct |
1 ms |
320 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
320 KB |
Output is correct |
47 |
Correct |
1 ms |
324 KB |
Output is correct |
48 |
Correct |
1 ms |
312 KB |
Output is correct |
49 |
Correct |
0 ms |
212 KB |
Output is correct |
50 |
Correct |
0 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
212 KB |
Output is correct |
52 |
Correct |
1 ms |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
320 KB |
Output is correct |
54 |
Correct |
1 ms |
212 KB |
Output is correct |
55 |
Correct |
1 ms |
212 KB |
Output is correct |
56 |
Correct |
1 ms |
212 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
316 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
1 ms |
212 KB |
Output is correct |
62 |
Correct |
1 ms |
340 KB |
Output is correct |
63 |
Correct |
1 ms |
296 KB |
Output is correct |
64 |
Correct |
1 ms |
212 KB |
Output is correct |
65 |
Correct |
1 ms |
316 KB |
Output is correct |
66 |
Correct |
218 ms |
8164 KB |
Output is correct |
67 |
Correct |
404 ms |
13900 KB |
Output is correct |
68 |
Correct |
287 ms |
10904 KB |
Output is correct |
69 |
Correct |
256 ms |
8136 KB |
Output is correct |
70 |
Correct |
196 ms |
7500 KB |
Output is correct |
71 |
Correct |
295 ms |
11080 KB |
Output is correct |
72 |
Correct |
390 ms |
13112 KB |
Output is correct |
73 |
Correct |
384 ms |
15632 KB |
Output is correct |
74 |
Correct |
310 ms |
11732 KB |
Output is correct |
75 |
Correct |
423 ms |
14156 KB |
Output is correct |
76 |
Correct |
240 ms |
9456 KB |
Output is correct |
77 |
Correct |
277 ms |
10228 KB |
Output is correct |
78 |
Correct |
328 ms |
12556 KB |
Output is correct |
79 |
Correct |
330 ms |
12416 KB |
Output is correct |
80 |
Execution timed out |
581 ms |
19680 KB |
Time limit exceeded |
81 |
Halted |
0 ms |
0 KB |
- |