結果
| 問題 | No.1630 Sorting Integers (Greater than K) | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2021-07-30 23:01:48 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                WA
                                 
                             | 
| 実行時間 | - | 
| コード長 | 2,402 bytes | 
| コンパイル時間 | 2,060 ms | 
| コンパイル使用メモリ | 202,940 KB | 
| 最終ジャッジ日時 | 2025-01-23 12:46:29 | 
| ジャッジサーバーID (参考情報) | judge5 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 5 | 
| other | AC * 14 WA * 8 | 
ソースコード
#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;
}
            
            
            
        