結果

問題 No.2456 Stamp Art
ユーザー 👑 emthrmemthrm
提出日時 2023-09-01 21:56:58
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 968 ms / 5,000 ms
コード長 3,061 bytes
コンパイル時間 3,025 ms
コンパイル使用メモリ 252,724 KB
実行使用メモリ 38,924 KB
最終ジャッジ日時 2023-09-02 11:17:52
合計ジャッジ時間 17,427 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 782 ms
38,704 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 873 ms
38,680 KB
testcase_07 AC 842 ms
38,736 KB
testcase_08 AC 753 ms
38,704 KB
testcase_09 AC 851 ms
38,704 KB
testcase_10 AC 950 ms
38,692 KB
testcase_11 AC 806 ms
38,644 KB
testcase_12 AC 836 ms
38,924 KB
testcase_13 AC 965 ms
38,644 KB
testcase_14 AC 968 ms
38,688 KB
testcase_15 AC 787 ms
38,740 KB
testcase_16 AC 377 ms
21,012 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 192 ms
26,984 KB
testcase_20 AC 861 ms
38,708 KB
testcase_21 AC 781 ms
35,540 KB
testcase_22 AC 877 ms
38,708 KB
testcase_23 AC 11 ms
5,156 KB
testcase_24 AC 29 ms
6,260 KB
testcase_25 AC 1 ms
4,376 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;

template <typename T>
struct CumulativeSum2D {
  explicit CumulativeSum2D(const int h, const int w)
      : CumulativeSum2D(std::vector<std::vector<T>>(h, std::vector<T>(w, 0))) {}

  template <typename U>
  explicit CumulativeSum2D(const std::vector<std::vector<U>>& a)
      : is_built(false), h(a.size()), w(a.front().size()),
        data(h + 1, std::vector<T>(w + 1, 0)) {
    for (int i = 0; i < h; ++i) {
      std::copy(a[i].begin(), a[i].end(), std::next(data[i + 1].begin()));
    }
  }

  void add(const int y, const int x, const T val) {
    assert(!is_built);
    data[y + 1][x + 1] += val;
  }

  void build() {
    assert(!is_built);
    is_built = true;
    for (int i = 0; i < h; ++i) {
      std::partial_sum(data[i + 1].begin(), data[i + 1].end(),
                       data[i + 1].begin());
    }
    for (int j = 1; j <= w; ++j) {
      for (int i = 1; i < h; ++i) {
        data[i + 1][j] += data[i][j];
      }
    }
  }

  T query(const int y1, const int x1, const int y2, const int x2) const {
    assert(is_built);
    return y1 > y2 || x1 > x2 ? 0 : data[y2 + 1][x2 + 1] - data[y2 + 1][x1]
                                    - data[y1][x2 + 1] + data[y1][x1];
  }

 private:
  bool is_built;
  const int h, w;
  std::vector<std::vector<T>> data;
};

int main() {
  int h, w; cin >> h >> w;
  vector<string> s(h); REP(i, h) cin >> s[i];
  CumulativeSum2D<int> cs(h, w);
  REP(i, h) REP(j, w) {
    if (s[i][j] == '#') cs.add(i, j, 1);
  }
  cs.build();
  int lb = 1, ub = min(h, w) + 1;
  while (ub - lb > 1) {
    const int k = midpoint(lb, ub);
    vector grid(h, vector(w, 0));
    REP(i, h - k + 1) REP(j, w - k + 1) {
      if (cs.query(i, j, i + k - 1, j + k - 1) == k * k) {
        ++grid[i][j];
        if (i + k < h) --grid[i + k][j];
        if (j + k < w) --grid[i][j + k];
        if (i + k < h && j + k < w) ++grid[i + k][j + k];
      }
    }
    REP(i, h) FOR(j, 1, w) grid[i][j] += grid[i][j - 1];
    REP(j, w) FOR(i, 1, h) grid[i][j] += grid[i - 1][j];
    bool is_valid = true;
    REP(i, h) REP(j, w) {
      if (s[i][j] == '#' && grid[i][j] == 0) {
        is_valid = false;
        break;
      }
    }
    (is_valid ? lb : ub) = k;
  }
  cout << lb << '\n';
  return 0;
}
0