結果
| 問題 | No.2696 Sign Creation | 
| コンテスト | |
| ユーザー |  emthrm | 
| 提出日時 | 2024-03-22 21:56:43 | 
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,638 ms / 2,500 ms | 
| コード長 | 3,327 bytes | 
| コンパイル時間 | 3,980 ms | 
| コンパイル使用メモリ | 270,432 KB | 
| 実行使用メモリ | 144,876 KB | 
| 最終ジャッジ日時 | 2024-12-20 11:57:18 | 
| 合計ジャッジ時間 | 10,505 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 38 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;
struct UndoableUnionFind {
  explicit UndoableUnionFind(const int n) : data(n, -1) {}
  int root(const int ver) const {
    return data[ver] < 0 ? ver : root(data[ver]);
  }
  bool unite(int u, int v) {
    u = root(u);
    history.emplace_back(u, data[u]);
    v = root(v);
    history.emplace_back(v, data[v]);
    if (u == v) return false;
    if (data[u] > data[v]) std::swap(u, v);
    data[u] += data[v];
    data[v] = u;
    return true;
  }
  bool is_same(const int u, const int v) const { return root(u) == root(v); }
  int size(const int ver) const { return -data[root(ver)]; }
  void undo() {
    for (int i = 0; i < 2; ++i) {
      data[history.back().first] = history.back().second;
      history.pop_back();
    }
  }
  void snapshot() { history.clear(); }
  void rollback() {
    while (!history.empty()) undo();
  }
 private:
  std::vector<int> data;
  std::vector<std::pair<int, int>> history;
};
int main() {
  int h, w, n, d; cin >> h >> w >> n >> d;
  map<pair<int, int>, int> stars;
  REP(i, n) {
    int x, y; cin >> x >> y; --x, --y;
    stars[{x, y}] = i;
  }
  UndoableUnionFind union_find(n + 1);
  set<pair<int, int>> cand;
  for (const auto [xy, id] : stars) {
    const auto [x, y] = xy;
    FOR(dy, -d, d + 1) FOR(dx, -d, d + 1) {
      const int nx = x + dx, ny = y + dy;
      if (abs(dx) + abs(dy) > d || nx < 0 || nx >= h || ny < 0 || ny >= w) continue;
      if (const auto it = stars.find({nx, ny}); it != stars.end()) union_find.unite(id, it->second);
      cand.emplace(nx, ny);
    }
  }
  for (const auto [x, y] : stars | views::keys) cand.erase({x, y});
  int cur = 0;
  REP(i, n + 1) {
    if (union_find.root(i) == i && union_find.size(i) >= 2) ++cur;
  }
  union_find.snapshot();
  int mn = INF, mx = 0;
  for (const auto& [rx, ry] : cand) {
    int tmp = cur;
    FOR(dy, -d, d + 1) FOR(dx, -d, d + 1) {
      const int nx = rx + dx, ny = ry + dy;
      if (abs(dx) + abs(dy) > d || nx < 0 || nx >= h || ny < 0 || ny >= w) continue;
      if (const auto it = stars.find({nx, ny}); it != stars.end() && union_find.unite(n, it->second)) {
        --tmp;
        union_find.undo();
        if (union_find.size(n) == 1) ++tmp;
        if (union_find.size(it->second) == 1) ++tmp;
        assert(union_find.unite(n, it->second));
      }
    }
    chmin(mn, tmp);
    chmax(mx, tmp);
    union_find.rollback();
  }
  if (n + cand.size() < h * w) {
    chmin(mn, cur);
    chmax(mx, cur);
  }
  cout << mn << ' ' << mx << '\n';
  return 0;
}
            
            
            
        