結果

問題 No.866 レベルKの正方形
ユーザー IKyoproIKyopro
提出日時 2019-08-17 20:16:08
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 1,353 bytes
コンパイル時間 1,114 ms
コンパイル使用メモリ 80,132 KB
実行使用メモリ 813,348 KB
最終ジャッジ日時 2023-10-25 08:44:59
合計ジャッジ時間 4,535 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
4,348 KB
testcase_08 MLE -
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>
#include <functional>
#include <queue>
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<ll>>> dp(26,vector<vector<ll>>(H+1,vector<ll>(W+1,-1)));
    ll ans = 0;
    function<ll(int,int,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;
        ll 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;
    };
    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++) cerr << dp[0][i][j] << (j!=W? " ":"\n");
    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