結果
問題 |
No.2064 Smallest Sequence on Grid
|
ユーザー |
![]() |
提出日時 | 2022-08-27 02:45:55 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,626 ms / 3,000 ms |
コード長 | 2,541 bytes |
コンパイル時間 | 20,661 ms |
コンパイル使用メモリ | 246,372 KB |
最終ジャッジ日時 | 2025-01-31 06:07:54 |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 29 |
ソースコード
#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <stdio.h> #include <math.h> #include <iostream> #include <iomanip> #include <vector> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <queue> #include <string> #include <stack> #include <numeric> #include <algorithm> using namespace std; using ll = long long; #include <atcoder/all> using namespace atcoder; using mint = modint998244353; #define rep(i, n) for (ll i = 0; i < n; i++) #define ALL(a) (a).begin(), (a).end() void chmin(ll &a, ll b) { if (a > b) a = b; } void chmax(ll &a, ll b) { if (a < b) a = b; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int h, w; cin >> h >> w; vector<string> ss(h); for (int i = 0; i < h; i++) { cin >> ss[i]; } vector<vector<int>> s(h, vector<int>(w)); for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) s[i][j] = ss[i][j] - 'a'; vector<vector<int>> yu(h, vector<int>(w)), ha(h, vector<int>(w)); const int inf = 5e7; vector<int> size(h + w - 1); for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) size[i + j]++; for (int i, j, ij = h + w - 3; ij >= 0; ij--) { int ii = 0, jj = ij; if (jj >= w) { int sa = jj - w + 1; ii += sa; jj -= sa; } i = ii; j = jj; vector<int> hash(size[ij]); int count = 0; while (i < h && j >= 0) { hash[count++] = ha[i][j] = s[i][j] * inf + min(i == h - 1 ? inf : yu[i + 1][j], j == w - 1 ? inf : yu[i][j + 1]); i++; j--; } i = ii; j = jj; sort(ALL(hash)); while (i < h && j >= 0) { int ok = 0, ng = size[ij]; while (ng - ok != 1) { int vs = (ok + ng) >> 1; if (hash[vs] <= ha[i][j]) ok = vs; else ng = vs; } yu[i][j] = ok; i++; j--; } } int i = 0, j = 0; while (j != w) { cout << ss[i][j]; if (i == h - 1) j++; else if (j == w - 1) i++; else { int hi = yu[i + 1][j], hj = yu[i][j + 1]; if (hi < hj) i++; else j++; } } return 0; }