This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <algorithm>
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)
#define x first
#define y second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef pair<int, int> pii;
const int infinity = 1e9;
vvb edges1, edges2;
vvi id1, id2;
int n, m;
inline bool IsEdge1(int i, int j, int bit) {
if (edges1[i][j]) return true;
if (id1[i][j] != -1) return !((bit >> id1[i][j]) & 1);
return false;
}
inline bool IsEdge2(int i, int j, int bit) {
if (edges2[i][j]) return true;
if (id2[i][j] != -1) return !((bit >> id2[i][j]) & 1);
return false;
}
inline bool Inside(int i, int j, int bit) {
if (i < 0 || i >= n || j < 0 || j >= m) return false;
return IsEdge1(i, j, bit) && IsEdge1(i + 1, j, bit) && IsEdge2(i, j, bit) && IsEdge2(i, j + 1, bit);
}
int main() {
cin >> n >> m;
edges1.resize(n + 1, vb(m)), edges2.resize(n, vb(m + 1));
vector<pair<pii, bool>> edges;
id1.resize(n + 1, vi(m, -1)), id2.resize(n, vi(m + 1, -1));
for (int i = 0; i <= n; i++) {
for (int j = 0; j < m; j++) {
char c;
cin >> c;
edges1[i][j] = c == '1';
if (c == '0') {
id1[i][j] = edges.size();
edges.push_back({{i, j}, false});
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j <= m; j++) {
char c;
cin >> c;
edges2[i][j] = c == '1';
if (c == '0') {
id2[i][j] = edges.size();
edges.push_back({{i, j}, true});
}
}
}
int sz = edges.size();
int pow = 1 << sz;
vi dp1(pow, -infinity), dp2(pow, infinity);
dp1[0] = dp2[0] = 0;
for (int i = 1; i < pow; i++) {
for (int j = 0; j < sz; j++) {
if (!((i >> j) & 1)) continue;
int squares = 0;
int sub = i - (1 << j);
if (edges[j].y) {
squares = Inside(edges[j].x.x, edges[j].x.y, sub) + Inside(edges[j].x.x, edges[j].x.y - 1, sub);
} else {
squares = Inside(edges[j].x.x, edges[j].x.y, sub) + Inside(edges[j].x.x - 1, edges[j].x.y, sub);
}
chkmax(dp1[i], (squares ? dp1[sub] : dp2[sub]) + squares);
chkmin(dp2[i], (squares ? dp2[sub] : dp1[sub]) - squares);
}
}
cout << dp1[pow - 1];
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |