結果

問題 No.2064 Smallest Sequence on Grid
ユーザー SSRSSSRS
提出日時 2022-09-02 21:31:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,897 bytes
コンパイル時間 2,254 ms
コンパイル使用メモリ 190,624 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-27 20:57:55
合計ジャッジ時間 11,673 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 RE -
testcase_07 AC 2 ms
6,940 KB
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 TLE -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
権限があれば一括ダウンロードができます

ソースコード

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<vector<int>>> E(H + W - 1);
  for (int i = 0; i < H + W - 1; i++){
    E[i].resize(cnt[i]);
  }
  for (int i = 0; i < H; i++){
    for (int j = 0; j < W; j++){
      if (i < H - 1){
        E[i + j][id[i][j]].push_back(id[i + 1][j]);
      }
      if (j < W - 1){
        E[i + j][id[i][j]].push_back(id[i][j + 1]);
      }
    }
  }
  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;
      for (int k : E[i][j]){
        mn = min(mn, dp[i + 1][k]);
      }
      P[j] = make_pair(S[x[i][j]][y[i][j]], mn);
      for (int k : E[i][j]){
        if (dp[i + 1][k] == mn){
          nxt[i][j] = k;
        }
      }
    }
    vector<pair<char, int>> P2 = P;
    sort(P2.begin(), P2.end());
    P2.erase(unique(P2.begin(), P2.end()), P2.begin());
    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