結果

問題 No.866 レベルKの正方形
ユーザー 0w10w1
提出日時 2019-08-24 01:04:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3,729 ms / 6,000 ms
コード長 1,551 bytes
コンパイル時間 2,427 ms
コンパイル使用メモリ 211,500 KB
実行使用メモリ 431,388 KB
最終ジャッジ日時 2023-08-03 17:48:15
合計ジャッジ時間 49,103 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 3,484 ms
431,336 KB
testcase_09 AC 3,145 ms
431,196 KB
testcase_10 AC 2,927 ms
431,212 KB
testcase_11 AC 3,548 ms
431,212 KB
testcase_12 AC 3,126 ms
431,344 KB
testcase_13 AC 3,729 ms
431,260 KB
testcase_14 AC 3,368 ms
431,204 KB
testcase_15 AC 3,507 ms
431,388 KB
testcase_16 AC 3,358 ms
431,200 KB
testcase_17 AC 3,390 ms
431,256 KB
testcase_18 AC 3,532 ms
431,304 KB
testcase_19 AC 3,498 ms
431,348 KB
testcase_20 AC 3,036 ms
431,256 KB
testcase_21 AC 1,146 ms
431,256 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,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