結果

問題 No.866 レベルKの正方形
ユーザー IKyoproIKyopro
提出日時 2019-08-17 11:46:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,456 bytes
コンパイル時間 820 ms
コンパイル使用メモリ 83,088 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-25 00:05:45
合計ジャッジ時間 9,988 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

int main(){
    int H,W,K;
    cin >> H >> W >> K;
    vector<vector<char>> F(H+1,vector<char>(W+1));
    for(int i=1;i<=H;i++) for(int j=1;j<=W;j++) cin >> F[i][j];
    vector<vector<vector<int>>> sum(26,vector<vector<int>>(H+1,vector<int>(W+1,0)));
    for(char c='a';c<='z';c++){
        for(int i=1;i<=H;i++) for(int j=1;j<=W;j++) if(F[i][j]==c){
            sum[c-'a'][i][j]++;            
        }
        for(int i=1;i<=H;i++) for(int j=1;j<=W;j++) sum[c-'a'][i][j] += sum[c-'a'][i][j-1];
        for(int i=1;i<=H;i++) for(int j=1;j<=W;j++) sum[c-'a'][i][j] += sum[c-'a'][i-1][j];
    }
    ll ans = 0;
    auto exist = [&](int x1,int y1,int x2,int y2,int c){
        return sum[c][x2][y2]-sum[c][x1-1][y2]-sum[c][x2][y1-1]+sum[c][x1-1][y1-1]>=1;
    };
    for(int i=1;i<=H;i++) for(int j=1;j<=W;j++){
        ll l1 = 1,r1 = min(H-i+1,W-j+1)+1;
        while(l1+1<r1){
            ll m = (l1+r1)/2;
            int cnt = 0;
            for(int k=0;k<26;k++) cnt += exist(i,j,i+m-1,j+m-1,k);
            if(cnt<=K) l1 = m;
            else r1 = m;
        }
        ll l2 = 0,r2 = min(H-i+1,W-j+1)+1;
        while(l2+1<r2){
            ll m = (l2+r2)/2;
            int cnt = 0;
            for(int k=0;k<26;k++) cnt += exist(i,j,i+m-1,j+m-1,k);
            if(cnt<K) l2 = m;
            else r2 = m;
        }
        ans += l1-l2;
    }
    cout << ans << endl;
}
0