結果

問題 No.2456 Stamp Art
ユーザー t98slidert98slider
提出日時 2023-09-01 23:11:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 836 ms / 5,000 ms
コード長 3,443 bytes
コンパイル時間 1,740 ms
コンパイル使用メモリ 174,344 KB
実行使用メモリ 39,012 KB
最終ジャッジ日時 2023-09-02 11:20:17
合計ジャッジ時間 14,476 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,388 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 677 ms
38,960 KB
testcase_04 AC 2 ms
4,388 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 740 ms
38,844 KB
testcase_07 AC 724 ms
38,908 KB
testcase_08 AC 643 ms
38,892 KB
testcase_09 AC 749 ms
38,840 KB
testcase_10 AC 834 ms
38,888 KB
testcase_11 AC 734 ms
38,836 KB
testcase_12 AC 699 ms
38,916 KB
testcase_13 AC 836 ms
38,912 KB
testcase_14 AC 826 ms
38,936 KB
testcase_15 AC 708 ms
39,012 KB
testcase_16 AC 373 ms
21,036 KB
testcase_17 AC 2 ms
4,384 KB
testcase_18 AC 2 ms
4,384 KB
testcase_19 AC 240 ms
26,936 KB
testcase_20 AC 776 ms
38,920 KB
testcase_21 AC 731 ms
35,520 KB
testcase_22 AC 828 ms
38,900 KB
testcase_23 AC 12 ms
5,164 KB
testcase_24 AC 29 ms
6,136 KB
testcase_25 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

template<class T> istream& operator >> (istream& is, vector<T>& vec) {
    for(T& x : vec) is >> x;
    return is;
}

template<class T> ostream& operator << (ostream& os, const vector<T>& vec) {
    if(vec.empty()) return os;
    os << vec[0];
    for(auto it = vec.begin(); ++it != vec.end(); ) os << ' ' << *it;
    return os;
}

template <class T> struct CumulativeSum2D{
    int h, w;
    std::vector<std::vector<T>> dat;
    CumulativeSum2D(int H, int W) : h(H), w(W), dat(H + 1, std::vector<T>(W + 1, 0)) {}
    CumulativeSum2D(std::vector<std::vector<T>> &A) 
        : h(A.size()), w(A[0].size()), dat(h + 1, std::vector<T>(w + 1, 0)) {
        for(int y = 1; y <= h; y++){
            for(int x = 1; x <= w; x++){
                dat[y][x] = A[y - 1][x - 1] + dat[y][x - 1] + dat[y - 1][x] - dat[y - 1][x - 1];
            }
        }
    }
    void add(int y, int x, T z){
        assert(0 <= y && y < h);
        assert(0 <= x && x < w);
        dat[y + 1][x + 1] += z;
    }
    void build(){
        for(int y = 1; y <= h; y++) {
            for(int x = 1; x <= w; x++) {
                dat[y][x] += dat[y][x - 1] + dat[y - 1][x] - dat[y - 1][x - 1];
            }
        }
    }
    T query(int ly, int lx, int ry, int rx){
        assert(0 <= ly && ly <= ry && ry <= h);
        assert(0 <= lx && lx <= rx && rx <= w);
        return dat[ry][rx] - dat[ly][rx] - dat[ry][lx] + dat[ly][lx];
    }
};

template <class T> struct imos2D{
    int h, w;
    std::vector<std::vector<T>> dat;
    imos2D(int H, int W) : h(H), w(W), dat(H + 1, std::vector<T>(W + 1, 0)) {}
    void add(int ly, int lx, int ry, int rx, T v){
        assert(0 <= ly && ly <= ry && ry <= h);
        assert(0 <= lx && lx <= rx && rx <= w);
        dat[ry][rx] += v;
        dat[ly][rx] -= v;
        dat[ry][lx] -= v;
        dat[ly][lx] += v;
    }
    void build(){
        for(int i = 0; i <= h; i++) {
            for(int j = 1; j <= w; j++) {
                dat[i][j] += dat[i][j - 1];
            }
        }
        for(int i = 0; i <= w; i++) {
            for(int j = 1; j <= h; j++) {
                dat[j][i] += dat[j - 1][i];
            }
        }
    }
    const std::vector<T>& operator[](int y) const {
        assert(0 <= y && y < h);
        return dat[y];
    }
    std::vector<T>& operator[](int y) { 
        assert(0 <= y && y < h);
        return dat[y];
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int h, w;
    cin >> h >> w;
    vector<string> A(h);
    cin >> A;
    CumulativeSum2D<int> S(h, w);
    for(int y = 0; y < h; y++){
        for(int x = 0; x < w; x++){
            if(A[y][x] == '#') S.add(y, x, 1);
        }
    }
    auto f = [&](int L){
        imos2D<int> S2(h, w);
        for(int y = 0; y + L <= h; y++){
            for(int x = 0; x + L <= w; x++){
                if(S.query(y, x, y + L, x + L) == L * L){
                    S2.add(y, x, y + L, x + L, 1);
                }
            }
        }
        S2.build();
        for(int y = 0; y < h; y++){
            for(int x = 0; x < w; x++){
                if(A[y][x] == '#' && S2[y][x] == 0) return false;
            }
        }
        return true;
    };
    S.build();
    int ok = 1, ng = min(h, w) + 1, mid;
    while(ok + 1 < ng){
        mid = (ok + ng) / 2;
        if(f(mid)) ok = mid;
        else ng = mid;
    }
    cout << ok << '\n';
}
0