結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー boutarouboutarou
提出日時 2021-07-30 23:01:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,402 bytes
コンパイル時間 3,148 ms
コンパイル使用メモリ 207,852 KB
実行使用メモリ 6,588 KB
最終ジャッジ日時 2023-10-14 07:56:41
合計ジャッジ時間 3,770 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 WA -
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 2 ms
4,352 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 10 ms
5,320 KB
testcase_19 WA -
testcase_20 AC 13 ms
5,124 KB
testcase_21 AC 13 ms
5,100 KB
testcase_22 AC 14 ms
5,192 KB
testcase_23 WA -
testcase_24 AC 24 ms
6,588 KB
testcase_25 AC 11 ms
5,120 KB
testcase_26 AC 10 ms
5,176 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>;

// 基本的には上のけたから順番に、その桁の数以上で最小のものを使っていく
// 途中でできなくなったら-1
// ans = K になったときはどこかのけたを入れ替える
// ...o..x... でo < xだったら入れ替えられる
// 下のけたから広義単調増加に来たら面倒
// ...4555333222111
// 下から見ていきそれまでのmaxよりも小さいものがきたらそこで変えるべき
// 変える元の数字をXとする
// ...4666553311みたいなときは4より大きい数字の中で一番小さい5と交換すべき、できるだけ左側(大きい桁側)の

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    
    int n; cin >> n;
    string k; cin >> k;
    vector<int> a(9);
    rep(i, 9) cin >> a[i];
    string zero = ""; rep(i, n - k.size()) zero += "0";
    k = zero + k;
    string ans = "";
    bool big = false;
    rep(i, n) {
        bool ok = false;
        for (int j = ( big ? 1 : max(k[i] - '0', 1)); j <= 9; j++) {
            if (a[j - 1] > 0) {
                ans += to_string(j);
                a[j - 1]--;
                if (j > k[i] - '0') big = true;
                ok = true; break;
            }
        }
        if (!ok) {
            cout << -1 << endl;
            return 0;
        }
    }
    if (ans > k) {
        cout << ans << endl;
        return 0;
    }
    int ma = 0;
    for (int i = n - 1; i >= 0; i--) {
        if (ma > ans[i] - '0') {
            int mi = 10;
            for (int j = i + 1; j < n; j++) if (ans[i] - '0' < ans[j] - '0') mi = min(mi, ans[j] - '0');
            for (int j = i + 1; j < n; j++) {
                if (ans[j] - '0' == mi) {
                    swap(ans[i], ans[j]);
                    vector<int> x;
                    for (int j = i + 1; j < n; j++) {
                        x.push_back(ans[j] - '0');
                    }
                    sort(x.begin(), x.end());
                    ans = ans.substr(0, i + 1);
                    for(auto elem : x) ans += to_string(elem);
                    cout << ans << endl;
                    return 0;
                }
            }
        }
        ma = max(ma, ans[i] - '0');
    }
    cout << -1 << endl;

    return 0;
}
0