結果

問題 No.2064 Smallest Sequence on Grid
ユーザー karinohitokarinohito
提出日時 2022-09-02 22:19:21
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,750 ms / 3,000 ms
コード長 3,649 bytes
コンパイル時間 2,305 ms
コンパイル使用メモリ 217,144 KB
実行使用メモリ 15,052 KB
最終ジャッジ日時 2023-08-10 04:34:20
合計ジャッジ時間 16,704 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,384 KB
testcase_17 AC 161 ms
14,724 KB
testcase_18 AC 160 ms
14,756 KB
testcase_19 AC 162 ms
14,772 KB
testcase_20 AC 1,230 ms
14,912 KB
testcase_21 AC 1,355 ms
14,940 KB
testcase_22 AC 866 ms
14,948 KB
testcase_23 AC 962 ms
15,020 KB
testcase_24 AC 958 ms
15,052 KB
testcase_25 AC 963 ms
14,964 KB
testcase_26 AC 1,750 ms
15,036 KB
testcase_27 AC 1,746 ms
15,016 KB
testcase_28 AC 159 ms
14,812 KB
testcase_29 AC 1,439 ms
14,920 KB
testcase_30 AC 147 ms
14,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <random>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vvvvll = vector<vvvll>;
using vvvvvll = vector<vvvvll>;
using vvvvvvll = vector<vvvvvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vvvvb = vector<vvvb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;
using vvvvd = vector<vvvd>;
using vvvvvd = vector<vvvvd>;
#define all(A) A.begin(),A.end()
#define ALL(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
using pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;

template<class T>
bool chmax(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p < q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

template<class T>
bool chmin(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p > q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

ll gcd(ll(a), ll(b)) {
    if (b == 0)return a;
    ll c = a;
    while (a % b != 0) {
        c = a % b;
        a = b;
        b = c;
    }
    return b;
}


vector<ll> fact, factinv, inv;
ll mod = 1e9 + 7;
void prenCkModp(ll n) {
    fact.resize(n + 5);
    factinv.resize(n + 5);
    inv.resize(n + 5);
    fact.at(0) = fact.at(1) = 1;
    factinv.at(0) = factinv.at(1) = 1;
    inv.at(1) = 1;
    for (ll i = 2; i < n + 5; i++) {
        fact.at(i) = (fact.at(i - 1) * i) % mod;
        inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;
        factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;
    }

}
ll nCk(ll n, ll k) {
    if (n < k) return 0;
    return fact.at(n) * (factinv.at(k) * factinv.at(n - k) % mod) % mod;
}
ll floor_sum(ll n, ll m, ll a, ll b) {
    ll ans = 0;
    if (a >= m) {
        ans += (n - 1) * n * (a / m) / 2;
        a %= m;
    }
    if (b >= m) {
        ans += n * (b / m);
        b %= m;
    }

    ll y_max = (a * n + b) / m, x_max = (y_max * m - b);
    if (y_max == 0) return ans;
    ans += (n - (x_max + a - 1) / a) * y_max;
    ans += floor_sum(y_max, a, m, (a - x_max % a) % a);
    return ans;
}

int main() {
    ll H, W;
    cin >> H >> W;
    vector<string> S(H);
    rep(h, H)cin >> S[h];
    string AN = "";
    AN+=S[0][0];
    queue<pair<ll, ll>> Q; Q.push({ 0,0 });
    vll dy = { 1,0 };
    vll dx = { 0,1 };
    rep(d, H + W - 2) {
        queue<pair<ll, ll>> NQ;
        char C = 'z' + 1;
        while (!Q.empty()) {
            ll y = Q.front().first;
            ll x = Q.front().second;
            Q.pop();
            NQ.push({ y,x });
            rep(d, 2) {
                ll ny = y + dy[d];
                ll nx = x + dx[d];
                if (ny >= 0 && nx >= 0 && ny < H && nx < W) {
                    chmin(C, S[ny][nx]);
                }
            }
        }
        AN += C;
        set<pair<ll, ll>> SE;
        while (!NQ.empty()) {
            ll y = NQ.front().first;
            ll x = NQ.front().second;
            NQ.pop();
            rep(d, 2) {
                ll ny = y + dy[d];
                ll nx = x + dx[d];
                if (ny >= 0 && nx >= 0 && ny < H && nx < W) {
                    if (S[ny][nx] == C) {
                        if (!SE.count({ ny,nx })) {
                            Q.push({ ny,nx });
                            SE.insert({ ny,nx });
                        }
                    }
                }
            }
        }
    }
    cout << AN << endl;
}
0