結果

問題 No.2696 Sign Creation
ユーザー haihamabossu
提出日時 2024-04-16 23:45:27
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 74 ms / 2,500 ms
コード長 1,903 bytes
コンパイル時間 2,717 ms
コンパイル使用メモリ 212,684 KB
最終ジャッジ日時 2025-02-21 02:45:28
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/dsu>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

void solve() {
  ll h, w, n, d;
  cin >> h >> w >> n >> d;
  vector grid(h, vector<ll>(w, -1));
  rep(ni, n) {
    ll x, y;
    cin >> x >> y, x--, y--;
    grid[x][y] = ni;
  }
  atcoder::dsu dsu(n);
  rep(x, h) rep(y, w) {
    ll ni = grid[x][y];
    if (ni == -1)
      continue;
    for (ll xi = x - d; xi <= x + d; xi++) {
      if (xi < 0 || h <= xi)
        continue;
      ll dx = abs(x - xi);
      for (ll yi = y - d + dx; yi <= y + d - dx; yi++) {
        if (yi < 0 || w <= yi)
          continue;
        ll nj = grid[xi][yi];
        if (nj == -1 || ni == nj)
          continue;
        dsu.merge(ni, nj);
      }
    }
  }
  ll orig = 0;
  for (const auto &g : dsu.groups()) {
    if (g.size() >= 2)
      orig++;
  }
  ll ans_min = orig, ans_max = orig;
  rep(x, h) rep(y, w) {
    if (grid[x][y] != -1)
      continue;
    set<ll> st;
    vector<ll> cnt(3);
    for (ll xi = x - d; xi <= x + d; xi++) {
      if (xi < 0 || h <= xi)
        continue;
      ll dx = abs(x - xi);
      for (ll yi = y - d + dx; yi <= y + d - dx; yi++) {
        if (yi < 0 || w <= yi)
          continue;
        ll nj = grid[xi][yi];
        if (nj == -1)
          continue;
        nj = dsu.leader(nj);
        if (st.count(nj) == 1)
          continue;
        st.insert(nj);
        cnt[min(dsu.size(nj), 2)] += 1;
      }
    }
    if (st.empty())
      continue;
    ll now = orig;
    if (cnt[1]) {
      now += 1;
      now -= cnt[2];
    } else {
      now -= cnt[2] - 1;
    }
    ans_min = min(ans_min, now);
    ans_max = max(ans_max, now);
  }
  cout << ans_min << ' ' << ans_max << '\n';
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  int T = 1;
  for (int t = 0; t < T; t++) {
    solve();
  }
  return 0;
}
0