結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー boutarouboutarou
提出日時 2021-07-31 10:49:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 1,483 bytes
コンパイル時間 1,847 ms
コンパイル使用メモリ 203,380 KB
実行使用メモリ 5,168 KB
最終ジャッジ日時 2023-10-14 14:37:21
合計ジャッジ時間 3,579 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < int(n); i++)
using ll = long long;
using P = pair<int, int>;

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    
    int n; cin >> n;
    string k; cin >> k;
    vector<int> c(10);
    rep(i, 9) cin >> c[i + 1];
    vector<int> c2 = c;
    if (n < (int)k.size()) {
        cout << -1 << endl;
        return 0;
    }

    // fill 0
    string zero = "";
    rep(i, n - k.size()) zero += "0";
    k = zero + k;

    // Kより数字を大きくする桁を考える
    int big = n - 1;
    rep(i, n) {
        if (c[k[i] - '0'] == 0) {
            big = i;
            break;
        }
        c[k[i] - '0']--;
    }

    for (; big >= -1; big--) {
        if (big == -1) {
            cout << -1 << endl;
            return 0;
        }
        bool ok = false;
        for (int j = k[big] - '0' + 1; j <= 9; j++) {
            if (c[j] > 0) ok = true;
        }
        if (ok) break;
        c[k[big] - '0']++;
    }

    if (big < 0) {
        cout << -1 << endl;
        return 0;
    }
    string ans = "";
    rep(i, big) {
        ans += k[i];
        c2[k[i] - '0']--;
    }
    for (int i = k[big] - '0' + 1; i <= 9; i++) {
        if (c2[i] > 0) {
            c2[i]--;
            ans += to_string(i);
            break;
        }
    }
    rep(i, 10) {
        rep(j, c2[i]) {
            ans += to_string(i);
        }
    }
    cout << ans << endl;

    return 0;
}
0