結果

問題 No.2064 Smallest Sequence on Grid
ユーザー SSRSSSRS
提出日時 2022-09-02 21:40:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,653 ms / 3,000 ms
コード長 1,785 bytes
コンパイル時間 2,101 ms
コンパイル使用メモリ 183,688 KB
実行使用メモリ 217,432 KB
最終ジャッジ日時 2023-08-10 03:36:28
合計ジャッジ時間 29,872 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,388 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 2,653 ms
216,980 KB
testcase_18 AC 2,639 ms
217,048 KB
testcase_19 AC 2,626 ms
217,108 KB
testcase_20 AC 1,316 ms
217,068 KB
testcase_21 AC 1,247 ms
217,076 KB
testcase_22 AC 1,282 ms
217,048 KB
testcase_23 AC 1,904 ms
216,984 KB
testcase_24 AC 1,895 ms
217,084 KB
testcase_25 AC 1,905 ms
216,988 KB
testcase_26 AC 1,168 ms
217,036 KB
testcase_27 AC 1,185 ms
217,060 KB
testcase_28 AC 2,127 ms
217,432 KB
testcase_29 AC 989 ms
183,200 KB
testcase_30 AC 2,380 ms
198,920 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF = 100000;
int main(){
  int H, W;
  cin >> H >> W;
  vector<vector<char>> S(H, vector<char>(W));
  for (int i = 0; i < H; i++){
    for (int j = 0; j < W; j++){
      cin >> S[i][j];
    }
  }
  vector<int> cnt(H + W - 1, 0);
  vector<vector<int>> x(H + W - 1), y(H + W - 1);
  vector<vector<int>> id(H, vector<int>(W));
  for (int i = 0; i < H; i++){
    for (int j = 0; j < W; j++){
      id[i][j] = cnt[i + j];
      cnt[i + j]++;
      x[i + j].push_back(i);
      y[i + j].push_back(j);
    }
  }
  vector<vector<int>> dp(H + W - 1);
  vector<vector<int>> nxt(H + W - 1);
  for (int i = 0; i < H + W - 1; i++){
    dp[i].resize(cnt[i]);
    nxt[i].resize(cnt[i]);
  }
  dp[H + W - 2][0] = 0;
  for (int i = H + W - 3; i >= 0; i--){
    vector<pair<char, int>> P(cnt[i]);
    for (int j = 0; j < cnt[i]; j++){
      int mn = INF;
      if (x[i][j] < H - 1){
        mn = min(mn, dp[i + 1][id[x[i][j] + 1][y[i][j]]]);
        if (mn == dp[i + 1][id[x[i][j] + 1][y[i][j]]]){
          nxt[i][j] = id[x[i][j] + 1][y[i][j]];
        }
      }
      if (y[i][j] < W - 1){
        mn = min(mn, dp[i + 1][id[x[i][j]][y[i][j] + 1]]);
        if (mn == dp[i + 1][id[x[i][j]][y[i][j] + 1]]){
          nxt[i][j] = id[x[i][j]][y[i][j] + 1];
        }
      }
      P[j] = make_pair(S[x[i][j]][y[i][j]], mn);
    }
    vector<pair<char, int>> P2 = P;
    sort(P2.begin(), P2.end());
    P2.erase(unique(P2.begin(), P2.end()), P2.end());
    for (int j = 0; j < cnt[i]; j++){
      dp[i][j] = lower_bound(P2.begin(), P2.end(), P[j]) - P2.begin();
    }
  }
  string ans;
  int p = 0;
  for (int i = 0; i < H + W - 1; i++){
    ans += S[x[i][p]][y[i][p]];
    if (i < H + W - 2){
      p = nxt[i][p];
    }
  }
  cout << ans << endl;
}
0