結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー sten_sansten_san
提出日時 2021-07-30 21:17:34
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 13 ms / 2,000 ms
コード長 2,316 bytes
コンパイル時間 2,033 ms
コンパイル使用メモリ 202,676 KB
実行使用メモリ 6,220 KB
最終ジャッジ日時 2023-10-14 02:59:01
合計ジャッジ時間 3,574 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,352 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 11 ms
5,928 KB
testcase_17 AC 11 ms
5,512 KB
testcase_18 AC 13 ms
6,188 KB
testcase_19 AC 13 ms
5,932 KB
testcase_20 AC 6 ms
6,188 KB
testcase_21 AC 7 ms
6,220 KB
testcase_22 AC 5 ms
5,344 KB
testcase_23 AC 4 ms
5,072 KB
testcase_24 AC 6 ms
5,288 KB
testcase_25 AC 4 ms
5,072 KB
testcase_26 AC 4 ms
5,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct iofast_t {
    iofast_t() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    }
} iofast;

struct uns_t {} uns;
template <typename Element, typename Head, typename ...Args>
auto vec(Element init, Head arg, Args ...args) {
    if constexpr (sizeof...(Args) == 0) return std::vector(arg, init);
    else return std::vector(arg, vec(init, args...));
}
template <typename Element, typename Head, typename ...Args>
auto vec(uns_t, Head arg, Args ...args) {
    return vec(Element(), arg, args...);
}

template <typename T, typename Compare = less<T>>
T &chmin(T &l, T r, Compare &&f = less<T>()) { return l = min(l, r, f); }
template <typename T, typename Compare = less<T>>
T &chmax(T &l, T r, Compare &&f = less<T>()) { return l = max(l, r, f); }

int main() {
    int n; cin >> n;
    string k; cin >> k;
    int c[9] = {};
    for (auto &e : c) cin >> e;

    auto less = [](auto &s, auto &t) {
        if (size(s) < size(t)) {
            return true;
        }
        if (size(s) > size(t)) {
            return false;
        }
        return s < t;
    };

    string l, r;
    for (int i = 1; i <= 9; ++i) {
        l += string(c[i - 1], '0' + i);
        r += string(c[i - 1], '0' + i);
    }
    reverse(begin(r), end(r));

    if (less(r, k) || r == k) {
        cout << -1 << endl;
        return 0;
    }

    if (less(k, l)) {
        cout << l << endl;
        return 0;
    }

    int last = 0, rem[9] = {};

    copy(begin(c), end(c), begin(rem));
    for (int i = 0; i < n; ++i) {
        int d = k[i] - '0';

        bool found = false;
        for (int j = d + 1; j <= 9; ++j) {
            found |= 0 < rem[j - 1];
        }

        if (found) {
            last = i;
        }

        if (k[i] == '0' || --rem[k[i] - '1'] < 0) {
            break;
        }
    }

    string ans;

    copy(begin(c), end(c), begin(rem));
    for (int i = 0; i < last; ++i) {
        ans += k[i];
        --rem[k[i] - '1'];
    }

    for (int i = k[last] - '0' + 1; i <= 9; ++i) {
        if (0 < rem[i - 1]) {
            ans += '0' + i;
            --rem[i - 1];
            break;
        }
    }

    for (int i = 1; i <= 9; ++i) {
        while (rem[i - 1]--) {
            ans += '0' + i;
        }
    }

    cout << ans << endl;
}

0