結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー ゆきのんゆきのん
提出日時 2021-07-30 21:18:16
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 38 ms / 2,000 ms
コード長 3,526 bytes
コンパイル時間 2,024 ms
コンパイル使用メモリ 202,816 KB
実行使用メモリ 5,224 KB
最終ジャッジ日時 2023-10-14 03:01:25
合計ジャッジ時間 3,655 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 1 ms
4,352 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 1 ms
4,352 KB
testcase_16 AC 21 ms
5,224 KB
testcase_17 AC 24 ms
5,128 KB
testcase_18 AC 14 ms
5,160 KB
testcase_19 AC 14 ms
5,128 KB
testcase_20 AC 20 ms
5,080 KB
testcase_21 AC 20 ms
5,112 KB
testcase_22 AC 38 ms
4,348 KB
testcase_23 AC 11 ms
4,352 KB
testcase_24 AC 22 ms
4,348 KB
testcase_25 AC 17 ms
5,088 KB
testcase_26 AC 14 ms
5,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
//#include<atcoder/all>
//#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
//using namespace atcoder;
//using namespace boost::multiprecision;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long ll;
typedef pair<int,int> P;
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; }
template<typename T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
const ll mod=1e9 + 7;
const ll LINF=1ll<<60;
const int INF=1<<30;
int dx[]={0,1,0,-1,0,1,-1,1,-1};
int dy[]={0,0,1,0,-1,1,-1,-1,1};



int main(){
    int n;
    string s;cin >> n >> s;
    vector<int> c(10, 0);
    for (int i = 0; i < 9; i++) {
        cin >> c[i + 1];
    }
    if(s.length() > n){
        puts("-1");
        return 0;
    }
    else if(s.length() < n){
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 10; j++) {
                if(c[j] > 0){
                    c[j]--;
                    cout << j;
                    break;
                }
            }
        }
        cout<< endl;
        return 0;
    }
    string ans = "";
    auto b = c;
    string p = "";
    for (int i = 0; i <= n; i++) {
        bool f = true;
        if(i < n){
            if(b[s[i] - '0'] > 0){
                ans += s[i];
                b[s[i]-'0']--;
                f = false;
            }
            else{
                bool ff = true;
                for (int j = s[i]-'0'+1; j < 10; j++) {
                    if(b[j] > 0){
                        b[j]--;
                        p = '0' + j;
                        ans += p;
                        ff = false;
                        break;
                    }
                }
                if(not ff){
                    for (int k = i + 1; k < n; k++) {
                        for (int l = 0; l < 10; l++) {
                            if(b[l] > 0){
                                p = '0' + l;
                                ans += p;
                                b[l]--;
                                break;
                            }
                        }
                    }
                    cout << ans << endl;
                    return 0;
                }
            }
        }
        if(f){
            for (int j = i - 1; j >= 0; j--) {
                bool ff = true;
                ans.pop_back();
                b[s[j] - '0']++;
                for (int k = s[j] - '0' + 1; k < 10; k++) {
                    if(b[k] > 0){
                        p = '0' + k;
                        ans += p;
                        ff = false;
                        b[k]--;
                        break;
                    }
                }
                if(not ff){
                    for (int k = j + 1; k < n; k++) {
                        for (int l = 0; l < 10; l++) {
                            if(b[l] > 0){
                                p = '0' + l;
                                ans += p;
                                b[l]--;
                                break;
                            }
                        }
                    }
                    cout << ans << endl;
                    return 0;
                }
            }
            puts("-1");
            return 0;
        }
    }
    return 0;
}
0