結果

問題 No.2456 Stamp Art
ユーザー milanis48663220milanis48663220
提出日時 2023-09-01 22:46:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 331 ms / 5,000 ms
コード長 3,783 bytes
コンパイル時間 1,508 ms
コンパイル使用メモリ 126,296 KB
実行使用メモリ 85,884 KB
最終ジャッジ日時 2023-09-02 11:18:59
合計ジャッジ時間 6,928 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 89 ms
85,820 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 262 ms
85,852 KB
testcase_07 AC 270 ms
85,856 KB
testcase_08 AC 242 ms
85,880 KB
testcase_09 AC 281 ms
85,840 KB
testcase_10 AC 309 ms
85,816 KB
testcase_11 AC 234 ms
85,784 KB
testcase_12 AC 268 ms
85,848 KB
testcase_13 AC 331 ms
85,884 KB
testcase_14 AC 321 ms
85,780 KB
testcase_15 AC 207 ms
85,796 KB
testcase_16 AC 118 ms
52,548 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 129 ms
59,980 KB
testcase_20 AC 289 ms
85,800 KB
testcase_21 AC 279 ms
79,616 KB
testcase_22 AC 292 ms
85,796 KB
testcase_23 AC 19 ms
20,860 KB
testcase_24 AC 26 ms
16,368 KB
testcase_25 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}


template<typename T>
class CumsumGrid{
    public:
    CumsumGrid(vector<vector<T>> v): v(v){
        n = v.size();
        m = v[0].size();
        v_cumsum = vector<vector<T>>(n+1, vector<T>(m+1, T(0)));
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++){
                v_cumsum[i+1][j+1] = v_cumsum[i+1][j]+v_cumsum[i][j+1]-v_cumsum[i][j]+v[i][j];
            }
        }
    }
    /**
     * sum(v[il:ir)[jl:jr))
     */ 
    T sum(int il, int ir, int jl, int jr){
        if(ir <= il) return T(0);
        if(jr <= jl) return T(0);
        if(ir > n) ir = n;
        if(jr > m) jr = m;
        if(il < 0) il = 0;
        if(jl < 0) jl = 0;
        return v_cumsum[ir][jr]-v_cumsum[ir][jl]-v_cumsum[il][jr]+v_cumsum[il][jl];
    }
    private:
    int n, m;
    vector<vector<T>> v;
    vector<vector<T>> v_cumsum;
};

int imos[2005][2005];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int h, w; cin >> h >> w;
    vector<string> s(h);
    for(int i = 0; i < h; i++) cin >> s[i];
    auto c = vec2d(h, w, 0);
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if(s[i][j] == '.') c[i][j] = 1;
        }
    }
    auto cumsum = CumsumGrid<int>(c);
    auto clear = [&](){
        for(int i = 0; i <= h; i++){
            for(int j = 0; j <= w; j++){
                imos[i][j] = 0;
            }
        }
    };
    auto ok = [&](int len){
        clear();
        for(int i = 0; i+len <= h; i++){
            for(int j = 0; j+len <= w; j++){
                if(cumsum.sum(i, i+len, j, j+len) == 0){
                    imos[i][j]++;
                    imos[i+len][j]--;
                    imos[i][j+len]--;
                    imos[i+len][j+len]++;
                }
            }
        }
        for(int i = 0; i < h; i++){
            for(int j = 1; j <= w; j++){
                imos[i][j] += imos[i][j-1];
            }
        }
        for(int i = 1; i <= h; i++){
            for(int j = 0; j < w; j++){
                imos[i][j] += imos[i-1][j];
            }
        }
        for(int i = 0; i < h; i++) {
            for(int j = 0; j < w; j++){
                bool black = s[i][j] == '#';
                bool cur_black = imos[i][j] > 0;
                if(black != cur_black) return false;
            }
        }
        return true;
    };

    int l = 1, r = min(h, w);
    if(ok(r)){
        cout << r << endl;
        return 0;
    }
    while(r-l > 1){
        int len = (l+r)/2;
        if(ok(len)) l = len;
        else r = len;
    }
    cout << l << endl;
}
0