結果
| 問題 |
No.2884 Pieces on Squares
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-09-03 18:30:55 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 136 ms / 2,000 ms |
| コード長 | 1,529 bytes |
| コンパイル時間 | 3,632 ms |
| コンパイル使用メモリ | 287,272 KB |
| 実行使用メモリ | 7,720 KB |
| 最終ジャッジ日時 | 2025-09-03 18:31:03 |
| 合計ジャッジ時間 | 7,026 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 45 |
ソースコード
#include <bits/stdc++.h>
#include <atcoder/dsu>
using namespace std;
using ll = long long;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int h, w, n;
cin >> h >> w >> n;
atcoder::dsu uf(h + w);
vector<int> a(n), b(n);
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
a[i]--, b[i]--;
uf.merge(a[i], b[i] + h);
}
int cur = 0;
vector<vector<int>> dp(2, vector<int>(w + 1, 1e9)), ep(2, vector<int>(w + 1, -1e9));
dp[cur][0] = ep[cur][0] = 0;
const auto G = uf.groups();
const int K = G.size();
for (int i = 0; i < K; i++) {
int nxt = 1 - cur;
for (int j = 0; j <= w; j++) dp[nxt][j] = dp[cur][j], ep[nxt][j] = ep[cur][j];
int r = 0, c = 0;
for (int x : G[i]) {
r += x < h;
c += x >= h;
}
for (int j = 0; j <= w; j++) {
if (dp[cur][j] != 1e9) {
dp[nxt][j + c] = min(dp[nxt][j + c], dp[cur][j] + r);
}
if (ep[cur][j] != -1e9) {
ep[nxt][j + c] = max(ep[nxt][j + c], ep[cur][j] + r);
}
}
cur = nxt;
}
ll ans = 0;
for (int j = 0; j <= w; j++) {
if (dp[cur][j] != 1e9) {
int i = dp[cur][j];
ans = max<ll>(ans, i * w + j * h - 2 * i * j);
}
if (ep[cur][j] != -1e9) {
int i = ep[cur][j];
ans = max<ll>(ans, i * w + j * h - 2 * i * j);
}
}
cout << ans << "\n";
}