結果

問題 No.2064 Smallest Sequence on Grid
ユーザー 遭難者遭難者
提出日時 2022-08-27 02:45:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,176 ms / 3,000 ms
コード長 2,541 bytes
コンパイル時間 4,322 ms
コンパイル使用メモリ 218,904 KB
実行使用メモリ 119,040 KB
最終ジャッジ日時 2024-04-27 20:14:51
合計ジャッジ時間 19,594 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 1,176 ms
118,912 KB
testcase_18 AC 1,154 ms
118,912 KB
testcase_19 AC 1,171 ms
119,040 KB
testcase_20 AC 764 ms
119,040 KB
testcase_21 AC 766 ms
118,912 KB
testcase_22 AC 799 ms
118,912 KB
testcase_23 AC 965 ms
119,040 KB
testcase_24 AC 966 ms
119,040 KB
testcase_25 AC 972 ms
118,912 KB
testcase_26 AC 780 ms
119,040 KB
testcase_27 AC 777 ms
119,040 KB
testcase_28 AC 1,020 ms
119,040 KB
testcase_29 AC 615 ms
100,352 KB
testcase_30 AC 1,040 ms
108,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0