結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー abc3abc3
提出日時 2021-07-30 22:43:13
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 34 ms / 2,000 ms
コード長 1,920 bytes
コンパイル時間 2,172 ms
コンパイル使用メモリ 209,716 KB
実行使用メモリ 7,608 KB
最終ジャッジ日時 2023-10-14 06:51:27
合計ジャッジ時間 4,741 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,352 KB
testcase_13 AC 2 ms
4,356 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 31 ms
7,308 KB
testcase_17 AC 29 ms
7,316 KB
testcase_18 AC 34 ms
7,608 KB
testcase_19 AC 33 ms
7,572 KB
testcase_20 AC 25 ms
7,304 KB
testcase_21 AC 26 ms
7,324 KB
testcase_22 AC 25 ms
6,528 KB
testcase_23 AC 17 ms
6,532 KB
testcase_24 AC 23 ms
6,064 KB
testcase_25 AC 19 ms
6,664 KB
testcase_26 AC 30 ms
7,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

   #include <bits/stdc++.h>
    using namespace std;
    #include <math.h>
    #include <iomanip>
    #include <cstdint>
    #include <string>
    #include <sstream>
    template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
    template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
    #define rep(i,n) for (int i = 0; i < (n); ++i)
    typedef long long ll;
    typedef unsigned long long ull;
    using P=pair<ll,ll>;
    const int INF=1001001001;
    const int mod=1e9+7;
    
    void solve(){
        int n;
        string x;
        cin>>n>>x;

        vector<int>s(n);
        rep(i,n){s[i]=x[i]-'0';}

        vector<int>cnt(10);
        string mx;
        for(int i=1;i<=9;i++){
            cin>>cnt[i];
            rep(j,cnt[i]){
               mx+=(char)('0'+i); 
            }
        }
        sort(mx.rbegin(),mx.rend());
        int X=x.size();

        if(n<X||mx<=x){cout<<-1<<endl;return;}
        sort(mx.begin(),mx.end());
        if(n>X){cout<<mx<<endl;return;}
        auto c2=cnt;
        int lst=0;
        rep(i,n){
            bool ok=false;
            for(int j=s[i]+1;j<=9;j++){
                if(c2[j]!=0){ok=true;}
            }
            if(ok){chmax(lst,i);}
            if(c2[s[i]]==0){break;}
            c2[s[i]]--;
        }
        string ans;
        rep(i,lst){
           ans+=x[i];
           cnt[s[i]]--;
        }
        for(int i=s[lst]+1;i<=9;i++){
            if(cnt[i]!=0){
                ans+=(char)(i+'0');
                cnt[i]--;
                break;
            }
        }
        for(int i=1;i<=9;i++){
            if(cnt[i]!=0){
                ans+=string(cnt[i],(char)(i+'0'));
            }
        }
        cout<<ans<<endl;
    }  

    int main(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        
        solve(); 
        
        return 0;
    }
0