結果

問題 No.866 レベルKの正方形
ユーザー IKyopro
提出日時 2019-08-17 20:42:28
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,148 bytes
コンパイル時間 2,730 ms
コンパイル使用メモリ 102,100 KB
最終ジャッジ日時 2025-01-07 13:53:27
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20 TLE * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int H,W,K;
char F[2001][2001];
int dp[26][2001][2001];

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];
        for(int c=0;c<26;c++) dp[c][i][j] = -1;
    }
    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