結果

問題 No.866 レベルKの正方形
ユーザー IKyoproIKyopro
提出日時 2019-08-17 20:36:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5,314 ms / 6,000 ms
コード長 1,237 bytes
コンパイル時間 1,334 ms
コンパイル使用メモリ 92,364 KB
実行使用メモリ 431,840 KB
最終ジャッジ日時 2023-10-25 09:12:21
合計ジャッジ時間 74,333 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 368 ms
431,252 KB
testcase_01 AC 324 ms
431,404 KB
testcase_02 AC 320 ms
431,400 KB
testcase_03 AC 323 ms
431,400 KB
testcase_04 AC 316 ms
431,400 KB
testcase_05 AC 314 ms
431,400 KB
testcase_06 AC 325 ms
431,400 KB
testcase_07 AC 315 ms
431,400 KB
testcase_08 AC 5,314 ms
431,428 KB
testcase_09 AC 5,196 ms
431,400 KB
testcase_10 AC 4,953 ms
431,840 KB
testcase_11 AC 4,915 ms
431,400 KB
testcase_12 AC 4,515 ms
431,840 KB
testcase_13 AC 4,842 ms
431,400 KB
testcase_14 AC 4,650 ms
431,840 KB
testcase_15 AC 4,824 ms
431,400 KB
testcase_16 AC 4,671 ms
431,400 KB
testcase_17 AC 4,781 ms
431,400 KB
testcase_18 AC 4,771 ms
431,400 KB
testcase_19 AC 4,493 ms
431,400 KB
testcase_20 AC 4,555 ms
431,400 KB
testcase_21 AC 4,725 ms
431,400 KB
testcase_22 AC 316 ms
431,400 KB
testcase_23 AC 309 ms
431,400 KB
testcase_24 AC 313 ms
431,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#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);
    }
    for(int i=1;i<=H;i++) for(int j=1;j<=W;j++){
        priority_queue<ll> Q;
        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;
        }
    }
    cout << ans << endl;
}
0