結果

問題 No.866 レベルKの正方形
ユーザー totori_nyaatotori_nyaa
提出日時 2019-08-16 23:48:53
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3,669 ms / 6,000 ms
コード長 2,434 bytes
コンパイル時間 1,727 ms
コンパイル使用メモリ 170,876 KB
実行使用メモリ 414,168 KB
最終ジャッジ日時 2023-10-24 08:49:34
合計ジャッジ時間 44,275 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,680 KB
testcase_01 AC 2 ms
5,680 KB
testcase_02 AC 2 ms
5,680 KB
testcase_03 AC 2 ms
5,680 KB
testcase_04 AC 3 ms
5,680 KB
testcase_05 AC 2 ms
5,680 KB
testcase_06 AC 2 ms
5,680 KB
testcase_07 AC 2 ms
5,680 KB
testcase_08 AC 2,685 ms
414,164 KB
testcase_09 AC 2,850 ms
414,168 KB
testcase_10 AC 3,050 ms
414,168 KB
testcase_11 AC 3,188 ms
414,168 KB
testcase_12 AC 2,541 ms
414,168 KB
testcase_13 AC 3,303 ms
414,168 KB
testcase_14 AC 2,012 ms
414,168 KB
testcase_15 AC 3,375 ms
414,168 KB
testcase_16 AC 3,669 ms
414,168 KB
testcase_17 AC 3,099 ms
414,168 KB
testcase_18 AC 3,074 ms
414,168 KB
testcase_19 AC 1,710 ms
414,168 KB
testcase_20 AC 695 ms
414,168 KB
testcase_21 AC 3,635 ms
414,164 KB
testcase_22 AC 2 ms
5,676 KB
testcase_23 AC 2 ms
5,676 KB
testcase_24 AC 2 ms
7,740 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] - c[i-min(i,j)][j][k] - c[i][j-min(i,j)][k] + c[i-min(i,j)][j-min(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