結果

問題 No.866 レベルKの正方形
ユーザー 0w10w1
提出日時 2019-08-24 01:04:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3,309 ms / 6,000 ms
コード長 1,551 bytes
コンパイル時間 2,195 ms
コンパイル使用メモリ 207,548 KB
実行使用メモリ 431,616 KB
最終ジャッジ日時 2024-04-21 15:57:03
合計ジャッジ時間 43,518 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 3,162 ms
431,488 KB
testcase_09 AC 2,828 ms
431,488 KB
testcase_10 AC 2,630 ms
431,360 KB
testcase_11 AC 3,094 ms
431,488 KB
testcase_12 AC 2,764 ms
431,488 KB
testcase_13 AC 3,309 ms
431,488 KB
testcase_14 AC 2,967 ms
431,360 KB
testcase_15 AC 3,257 ms
431,360 KB
testcase_16 AC 2,934 ms
431,616 KB
testcase_17 AC 2,941 ms
431,488 KB
testcase_18 AC 3,107 ms
431,488 KB
testcase_19 AC 3,122 ms
431,488 KB
testcase_20 AC 2,704 ms
431,488 KB
testcase_21 AC 1,023 ms
431,488 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);

  int H, W, K;
  cin >> H >> W >> K;

  vector<string> G(H);
  for (int i = 0; i < H; ++i) {
    cin >> G[i];
  }

  vector<vector<vector<int>>> dp(26, vector<vector<int>>(H, vector<int>(W, 0x3f3f3f3f)));
  for (int i = H - 1; ~i; --i) {
    for (int j = W - 1; ~j; --j) {
      for (int c = 0; c < 26; ++c) {
        if (c == G[i][j] - 'a') {
          dp[c][i][j] = 1;
        } else {
          if (i + 1 < H && dp[c][i + 1][j] < 0x3f3f3f3f) {
            if (i + dp[c][i + 1][j] < H && j + dp[c][i + 1][j] < W) {
              dp[c][i][j] = min(dp[c][i][j], dp[c][i + 1][j] + 1);
            }
          }
          if (j + 1 < W && dp[c][i][j + 1] < 0x3f3f3f3f) {
            if (i + dp[c][i][j + 1] < H && j + dp[c][i][j + 1] < W) {
              dp[c][i][j] = min(dp[c][i][j], dp[c][i][j + 1] + 1);
            }
          }
          if (i + 1 < H && j + 1 < W) {
            dp[c][i][j] = min(dp[c][i][j], dp[c][i + 1][j + 1] + 1);
          }
        }
      }
    }
  }

  int64_t ans = 0;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      vector<int> ks;
      for (int c = 0; c < 26; ++c) {
        if (dp[c][i][j] < 0x3f3f3f3f) {
          ks.push_back(dp[c][i][j]);
        }
      }
      sort(ks.begin(), ks.end());
      if (K < ks.size()) {
        ans += ks[K] - ks[K - 1];
      } else if (K == ks.size()) {
        ans += 1 + min(H - i, W - j) - ks[K - 1];
      }

    }
  }

  cout << ans << endl;

  return 0;
}
0