結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー carrot46carrot46
提出日時 2021-07-30 21:21:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 2,837 bytes
コンパイル時間 2,061 ms
コンパイル使用メモリ 203,156 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-14 03:11:22
合計ジャッジ時間 3,536 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 1 ms
4,352 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 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
4,348 KB
testcase_17 AC 9 ms
4,352 KB
testcase_18 AC 5 ms
4,348 KB
testcase_19 AC 5 ms
4,348 KB
testcase_20 AC 7 ms
4,352 KB
testcase_21 AC 8 ms
4,348 KB
testcase_22 AC 23 ms
4,348 KB
testcase_23 AC 3 ms
4,352 KB
testcase_24 AC 9 ms
4,348 KB
testcase_25 AC 6 ms
4,352 KB
testcase_26 AC 5 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/modint>
using namespace std;
using namespace atcoder;
//#pragma GCC optimize("Ofast")
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()

using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;

ll N,M,H,W,Q,K,A,B;
string S;
using P = pair<ll, ll>;
using tp = tuple<ll, ll, ll>;
const ll INF = (1LL<<60);

template<class T> bool chmin(T &a, const T b){
    if(a > b) {a = b; return true;}
    else return false;
}
template<class T> bool chmax(T &a, const T b){
    if(a < b) {a = b; return true;}
    else return false;
}
template<class T> void my_printv(std::vector<T> v,bool endline = true){
    if(!v.empty()){
        for(std::size_t i{}; i<v.size()-1; ++i) std::cout<<v[i]<<" ";
        std::cout<<v.back();
    }
    if(endline) std::cout<<std::endl;
}


int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cin>>N>>S;
    vec c(10, 0);
    rep(i, 9) cin>>c[i+1];
    if(S.size() > N){
        cout<<-1<<endl;
    }else if(size(S) < N){
        rep(i, 10) rep(j, c[i]) cout<<i;
        cout<<endl;
    }else{
        string res = string(N, '0');
        auto res_fill_min = [&](int p){
            int pos = -1;
            Rrep(i, p) {
                ++c[res[i]-'0'];
                //my_printv(c);
                reps(j, res[i] - '0' + 1, 10) if(c[j]) {pos = i; break;}
                if(pos != -1) break;
            }
            //cout<<pos<<endl;
            if(pos == -1){
                cout<<-1<<endl;
                exit(0);
            }else{
                reps(i, res[pos] - '0' + 1, 10){
                    if(c[i]){
                        --c[i];
                        res[pos] = '0' + i;
                        break;
                    }
                }
                reps(i, pos + 1, N){
                    rep(j, 10) {
                        if(c[j] > 0){
                            --c[j];
                            res[i] = '0' + j;
                            break;
                        }
                    }
                }
            }
        };
        bool eq = true;
        rep(p, N){
            char s = S[p];
            bool ok = false;
            reps(i, eq ? s - '0' : 0, 10) {
                if (c[i]) {
                    --c[i];
                    ok = true;
                    if(i != s - '0') eq = false;
                    res[p] = '0' + i;
                    break;
                }
            }
            if (!ok) {
                res_fill_min(p);
                cout<<res<<endl;
                return 0;
            }
        }
        if(S == res){
            res_fill_min(N);
        }
        cout<<res<<endl;
    }
}
0