# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1277395 | shirokito | Dango Maker (JOI18_dango_maker) | C++20 | 2100 ms | 249060 KiB |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 3000 + 24;
int n, m; char a[N][N]; int id[N][N];
vector<vector<int>> g;
int tdfs = 0; vector<int> match, vis;
bool ok(int i, int j, int dir) {
if (dir == 0) {
return (j + 2 <= m && a[i][j] == 'R' && a[i][j + 1] == 'G' && a[i][j + 2] == 'W');
}
else {
return (i + 2 <= n && a[i][j] == 'R' && a[i + 1][j] == 'G' && a[i + 2][j] == 'W');
}
}
bool kuhn(int u) {
if (vis[u] == tdfs) return 0;
vis[u] = tdfs;
for (int v: g[u]) {
if (match[v] == 0 || kuhn(match[v])) {
match[v] = u;
return 1;
}
}
return 0;
}
void solve() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
int szL = 0, szT = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (ok(i, j, 0)) ++szL;
if (ok(i, j, 1)) id[i][j] = ++szT;
}
}
g.assign(szL + 1, vector<int>());
match.resize(szT + 1, 0); vis.resize(szL + 1, 0);
szL = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (!ok(i, j, 0)) continue;
szL++;
if (ok(i, j, 1)) g[szL].push_back(id[i][j]);
if (i - 1 >= 1 && j + 1 <= m && ok(i - 1, j + 1, 1)) g[szL].push_back(id[i - 1][j + 1]);
if (i - 2 >= 1 && j + 2 <= m && ok(i - 2, j + 2, 1)) g[szL].push_back(id[i - 2][j + 2]);
}
}
vector<int> id; for (int i = 1; i <= szL; i++) id.push_back(i);
random_shuffle(id.begin(), id.end());
for (int i: id) {
random_shuffle(g[i].begin(), g[i].end());
}
for (int i: id) {
tdfs++;
kuhn(i);
}
int cnt_match = 0;
for (int i = 1; i <= szT; i++) {
if (match[i] != 0) cnt_match++;
}
cout << (szL + szT) - cnt_match << '\n';
}
int main() {
cin.tie(0) -> sync_with_stdio(0);
int T = 1; // cin >> T;
while (T--) {
solve();
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |