結果

問題 No.866 レベルKの正方形
ユーザー totori_nyaatotori_nyaa
提出日時 2019-12-30 22:27:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3,430 ms / 6,000 ms
コード長 2,434 bytes
コンパイル時間 1,367 ms
コンパイル使用メモリ 169,880 KB
実行使用メモリ 414,020 KB
最終ジャッジ日時 2024-04-27 16:23:20
合計ジャッジ時間 40,795 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2,669 ms
413,928 KB
testcase_09 AC 2,703 ms
413,952 KB
testcase_10 AC 2,931 ms
413,952 KB
testcase_11 AC 3,102 ms
413,968 KB
testcase_12 AC 2,554 ms
413,924 KB
testcase_13 AC 3,142 ms
413,876 KB
testcase_14 AC 1,945 ms
413,932 KB
testcase_15 AC 2,950 ms
414,020 KB
testcase_16 AC 3,134 ms
413,872 KB
testcase_17 AC 3,038 ms
413,952 KB
testcase_18 AC 3,030 ms
413,952 KB
testcase_19 AC 1,693 ms
413,952 KB
testcase_20 AC 754 ms
413,792 KB
testcase_21 AC 3,430 ms
413,952 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 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