#include "bits/stdc++.h"
using namespace std;
vector<vector<vector<vector<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;
}
vector<int> upda, updb;
if (A < a.size()) upda = {A};
if (B < b.size()) updb = {B};
if (dp[A][B][c][d] == (int) -2e9) {
for (auto i : upda) {
dp[A][B][c][d] = max(dp[A][B][c][d], -(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[A][B][c][d] = max(dp[A][B][c][d], -res - solve(A, B + 1, tc, td));
}
if (c) {
dp[A][B][c][d] = max(dp[A][B][c][d], abs(2 + solve(A, B, c - 1, d)));
}
if (d) {
dp[A][B][c][d] = max(dp[A][B][c][d], abs(4 + solve(A, B, c, d - 1)));
}
}
return dp[A][B][c][d];
}
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;
dp.resize((int) a.size() + 1, vector<vector<vector<int>>> ((int) b.size() + 1, vector<vector<int>> ((int) b.size() * 2 + 1,
vector<int> ((int) a.size() + 1, -(int) 2e9))));
cout << solve(0, 0, 0, 0);
}
Compilation message
game.cpp: In function 'int solve(int, int, int, int)':
game.cpp:14:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | if (A < a.size()) upda = {A};
| ~~^~~~~~~~~~
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 (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 |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
320 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
0 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 |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 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 |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 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 |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 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 |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
336 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 |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 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 |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
324 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
324 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
316 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
320 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 |
316 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
320 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
320 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
316 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
316 KB |
Output is correct |
61 |
Correct |
0 ms |
340 KB |
Output is correct |
62 |
Correct |
1 ms |
340 KB |
Output is correct |
63 |
Correct |
1 ms |
320 KB |
Output is correct |
64 |
Correct |
1 ms |
340 KB |
Output is correct |
65 |
Correct |
1 ms |
336 KB |
Output is correct |
66 |
Correct |
18 ms |
5600 KB |
Output is correct |
67 |
Correct |
35 ms |
12748 KB |
Output is correct |
68 |
Correct |
26 ms |
8916 KB |
Output is correct |
69 |
Correct |
19 ms |
5824 KB |
Output is correct |
70 |
Correct |
20 ms |
7232 KB |
Output is correct |
71 |
Correct |
26 ms |
7764 KB |
Output is correct |
72 |
Correct |
39 ms |
14304 KB |
Output is correct |
73 |
Correct |
78 ms |
97588 KB |
Output is correct |
74 |
Correct |
29 ms |
9824 KB |
Output is correct |
75 |
Correct |
32 ms |
9036 KB |
Output is correct |
76 |
Correct |
25 ms |
8928 KB |
Output is correct |
77 |
Correct |
30 ms |
17628 KB |
Output is correct |
78 |
Correct |
30 ms |
10592 KB |
Output is correct |
79 |
Correct |
32 ms |
9268 KB |
Output is correct |
80 |
Correct |
49 ms |
15956 KB |
Output is correct |
81 |
Correct |
28 ms |
6232 KB |
Output is correct |
82 |
Correct |
35 ms |
12004 KB |
Output is correct |
83 |
Correct |
39 ms |
10176 KB |
Output is correct |