結果

問題 No.2696 Sign Creation
ユーザー 👑 emthrmemthrm
提出日時 2024-03-22 21:56:43
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,613 ms / 2,500 ms
コード長 3,327 bytes
コンパイル時間 3,515 ms
コンパイル使用メモリ 270,276 KB
実行使用メモリ 145,300 KB
最終ジャッジ日時 2024-03-27 17:57:48
合計ジャッジ時間 9,237 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 1 ms
6,676 KB
testcase_08 AC 16 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 9 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 732 ms
26,496 KB
testcase_14 AC 204 ms
14,332 KB
testcase_15 AC 184 ms
12,880 KB
testcase_16 AC 684 ms
25,168 KB
testcase_17 AC 3 ms
6,676 KB
testcase_18 AC 71 ms
6,676 KB
testcase_19 AC 263 ms
10,804 KB
testcase_20 AC 3 ms
6,676 KB
testcase_21 AC 128 ms
12,160 KB
testcase_22 AC 127 ms
6,676 KB
testcase_23 AC 180 ms
11,136 KB
testcase_24 AC 188 ms
11,068 KB
testcase_25 AC 356 ms
10,736 KB
testcase_26 AC 18 ms
6,676 KB
testcase_27 AC 149 ms
7,552 KB
testcase_28 AC 153 ms
8,448 KB
testcase_29 AC 5 ms
6,676 KB
testcase_30 AC 4 ms
6,676 KB
testcase_31 AC 1,613 ms
145,300 KB
testcase_32 AC 2 ms
6,676 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 2 ms
6,676 KB
testcase_35 AC 2 ms
6,676 KB
testcase_36 AC 2 ms
6,676 KB
testcase_37 AC 2 ms
6,676 KB
testcase_38 AC 1 ms
6,676 KB
testcase_39 AC 2 ms
6,676 KB
testcase_40 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0