結果

問題 No.866 レベルKの正方形
ユーザー totori_nyaatotori_nyaa
提出日時 2019-08-16 23:45:05
言語 C++14
(gcc 12.2.0 + boost 1.81.0)
結果
AC  
実行時間 5,410 ms / 6,000 ms
コード長 2,359 bytes
コンパイル時間 1,364 ms
実行使用メモリ 414,060 KB
最終ジャッジ日時 2023-03-31 21:39:42
合計ジャッジ時間 60,245 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
7,608 KB
testcase_01 AC 2 ms
7,616 KB
testcase_02 AC 2 ms
7,596 KB
testcase_03 AC 1 ms
7,660 KB
testcase_04 AC 2 ms
9,700 KB
testcase_05 AC 2 ms
7,612 KB
testcase_06 AC 1 ms
7,652 KB
testcase_07 AC 2 ms
7,656 KB
testcase_08 AC 4,241 ms
413,948 KB
testcase_09 AC 4,384 ms
414,028 KB
testcase_10 AC 4,834 ms
414,060 KB
testcase_11 AC 5,015 ms
413,988 KB
testcase_12 AC 4,168 ms
414,060 KB
testcase_13 AC 4,642 ms
413,916 KB
testcase_14 AC 3,120 ms
414,012 KB
testcase_15 AC 4,445 ms
414,032 KB
testcase_16 AC 4,474 ms
413,988 KB
testcase_17 AC 4,615 ms
413,980 KB
testcase_18 AC 4,537 ms
414,016 KB
testcase_19 AC 2,736 ms
413,904 KB
testcase_20 AC 481 ms
413,984 KB
testcase_21 AC 5,410 ms
414,036 KB
testcase_22 AC 2 ms
9,688 KB
testcase_23 AC 1 ms
9,692 KB
testcase_24 AC 2 ms
9,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int c[2001][2001][26];

signed main(){
    //cout << setprecision(16) ;
    ios::sync_with_stdio(false);
	cin.tie(0);

    int h,w,n;
    cin>>h>>w>>n;
    string s[h];
    for(int i=0;i<h;i++){
        cin>>s[i];
        for(int j=0;j<w;j++){
            c[i+1][j+1][s[i][j]-'a']++;
            for(int k=0;k<26;k++){
                c[i+1][j+1][k] += c[i+1][j][k];
            }
        }
    }
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            for(int k=0;k<26;k++){
                c[i+1][j+1][k] += c[i][j+1][k];
            }
        }
    }

    ll ans = 0;

    for(int i=1;i<=h;i++){
        for(int j=1;j<=w;j++){
            int cnt=0;
            for(int k=0;k<26;k++){
                if(c[i][j][k]) cnt++;
            }
            if(cnt<n) continue;
            int upper,lower;

            //lowは下限―1、upは上限+1にする
            long long low=-1,up=min(i,j)+1,mid;
            while(up-low>1){
                mid=(up+low)/2;
                //上半分か下半分の条件を求める
                cnt = 0;
                for(int k=0;k<26;k++){
                    if(c[i][j][k] - c[i-mid][j][k] - c[i][j-mid][k] + c[i-mid][j-mid][k]>0) cnt++;
                }
            
                //上半分の時
                if(cnt<=n){
                    low=mid;
                }
            
                //下半分の時
                else{
                    up=mid;
                }
            }
            //lowが答え
            upper = low;

            low = -1, up = min(i,j)+1;
            while(up-low>1){
                mid=(up+low)/2;
                //上半分か下半分の条件を求める
                cnt = 0;
                for(int k=0;k<26;k++){
                    if(c[i][j][k] - c[i-mid][j][k] - c[i][j-mid][k] + c[i-mid][j-mid][k]>0) cnt++;
                }
            
                //上半分の時
                if(cnt<n){
                    low=mid;
                }
            
                //下半分の時
                else{
                    up=mid;
                }
            }
            lower = low;

            //cerr<<i<<" "<<j<<" "<<lower<<" "<<upper<<endl;
            
            ans += upper - lower;
        }
    }
    cout<<ans<<endl;


}
0