結果

問題 No.866 レベルKの正方形
ユーザー IKyoproIKyopro
提出日時 2019-08-17 20:45:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5,759 ms / 6,000 ms
コード長 1,204 bytes
コンパイル時間 1,863 ms
コンパイル使用メモリ 88,744 KB
実行使用メモリ 431,312 KB
最終ジャッジ日時 2023-10-25 09:38:10
合計ジャッジ時間 79,991 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 323 ms
431,312 KB
testcase_01 AC 319 ms
431,312 KB
testcase_02 AC 317 ms
431,312 KB
testcase_03 AC 324 ms
431,312 KB
testcase_04 AC 320 ms
431,312 KB
testcase_05 AC 321 ms
431,312 KB
testcase_06 AC 321 ms
431,312 KB
testcase_07 AC 323 ms
431,312 KB
testcase_08 AC 4,929 ms
431,312 KB
testcase_09 AC 5,216 ms
431,312 KB
testcase_10 AC 5,058 ms
431,312 KB
testcase_11 AC 5,440 ms
431,312 KB
testcase_12 AC 5,176 ms
431,312 KB
testcase_13 AC 5,759 ms
431,312 KB
testcase_14 AC 5,473 ms
431,312 KB
testcase_15 AC 5,412 ms
431,312 KB
testcase_16 AC 5,288 ms
431,312 KB
testcase_17 AC 5,320 ms
431,312 KB
testcase_18 AC 5,220 ms
431,312 KB
testcase_19 AC 5,468 ms
431,312 KB
testcase_20 AC 5,326 ms
431,312 KB
testcase_21 AC 4,273 ms
431,312 KB
testcase_22 AC 322 ms
431,312 KB
testcase_23 AC 320 ms
431,312 KB
testcase_24 AC 317 ms
431,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int H,W,K;
vector<vector<char>> F(2001,vector<char>(2001));
vector<vector<vector<int>>> dp(26,vector<vector<int>>(2001,vector<int>(2001,-1)));

int rec(int x,int y,int c){
    if(dp[c][x][y]!=-1) return dp[c][x][y];
    if(F[x][y]-'a'==c) return dp[c][x][y] = 1;
    int res = min(H-x+1,W-y+1)+1;
    if(x!=H && y!=W) res = min(res,rec(x+1,y+1,c)+1);
    if(x!=H) res = min(res,rec(x+1,y,c)+1);
    if(y!=W) res = min(res,rec(x,y+1,c)+1);
    return dp[c][x][y] = res;
}

int main(){
    cin >> H >> W >> K;
    for(int i=1;i<=H;i++) for(int j=1;j<=W;j++) cin >> F[i][j];
    ll ans = 0;
    for(int c=0;c<26;c++){
        for(int i=1;i<=H;i++) for(int j=1;j<=W;j++) rec(i,j,c);
    }
    priority_queue<ll> Q;
    for(int i=1;i<=H;i++) for(int j=1;j<=W;j++){
        for(int c=0;c<26;c++){
            Q.push(dp[c][i][j]);
            if(Q.size()>K+1) Q.pop();
        }
        if(K==26) ans += min(H-i+1,W-j+1)+1-Q.top();
        else{
            ll r = Q.top(); Q.pop();
            ll l = Q.top();
            ans += r-l;
        }
        while(!Q.empty()) Q.pop();
    }
    cout << ans << endl;
}
0