結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー nawawannawawan
提出日時 2021-07-30 21:36:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 3,354 bytes
コンパイル時間 1,709 ms
コンパイル使用メモリ 177,352 KB
実行使用メモリ 5,892 KB
最終ジャッジ日時 2023-10-14 03:47:05
合計ジャッジ時間 3,420 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 1 ms
4,352 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 1 ms
4,352 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 20 ms
5,680 KB
testcase_17 AC 21 ms
5,612 KB
testcase_18 AC 17 ms
5,568 KB
testcase_19 AC 17 ms
5,892 KB
testcase_20 AC 16 ms
5,656 KB
testcase_21 AC 15 ms
5,600 KB
testcase_22 AC 15 ms
5,740 KB
testcase_23 AC 13 ms
5,068 KB
testcase_24 AC 22 ms
4,348 KB
testcase_25 AC 13 ms
5,208 KB
testcase_26 AC 18 ms
5,604 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
    int N;
    string K;
    cin >> N;
    cin >> K;
    vector<int> c(10);
    for(int i = 1; i < 10; i++) cin >> c[i]; 
    string ma;
    for(int i = 9; i >= 0; i--){
        for(int k = 0; k < c[i]; k++){
            ma += ('0' + i);
        }
    }
    bool flag = false;
    if(N > K.size()) flag = true;
    else if(ma.size() == K.size()){
        for(int i = 0; i < ma.size(); i++){
            if(ma[i] > K[i]){
                flag = true;
                break;
            }
            else if(ma[i] < K[i]){
                break;
            }
        }
    }
    if(!flag) cout << -1 << endl;
    else{
        if(N > K.size()){
            string temp;
            for(int i = 0; i < 10; i++){
                for(int j = 0; j < c[i]; j++){
                    temp += '0' + i;
                }
            }
            cout << temp << endl;
            return 0;
        }
        int sz = K.size();
        string ans;
        vector<int> cnt(10);
        for(int i = 0; i < sz; i++){
            cnt[K[i] - '0']++;
        }
        bool f = true;
        for(int i = 0; i < 10; i++){
            if(c[i] != cnt[i]){
                f = false;
                break;
            }
        }
        if(f){
            set<int> s;
            for(int i = sz - 1; i >= 0; i--){
                auto ite = s.lower_bound(K[i] - '0' + 1);
                if(ite != s.end()){
                    for(int j = i + 1; j < sz; j++){
                        if(K[j] - '0' == *ite){
                            swap(K[i], K[j]);
                            sort(K.begin() + i + 1, K.end());
                            cout << K << endl;
                            return 0;
                        }
                    }
                }
                s.insert(K[i] - '0');
            }
        }
        for(int i = 0; i < sz; i++){
            int ind = K[i] - '0';
            if(c[ind] == 0){
                bool f = false;
                for(int j = ind + 1; j < 10; j++){
                    if(c[j] > 0){
                        ans += '0' + j;
                        c[j]--;
                        f = true;
                        break;
                    }
                }
                if(!f){
                    int siz = ans.size();
                    for(int j = siz - 1; j >= 0; j--){
                        int id = ans[j] - '0';
                        bool f = false;
                        for(int k = id + 1; k < 10; k++){
                            if(c[k] > 0){
                                ans[j] = '0' + k;
                                c[id]++;
                                c[k]--;
                                f = true;
                                break;
                            }
                        }
                        if(f) break;
                        c[id]++;
                        ans.pop_back();
                    }
                }
                for(int j = 0; j < 10; j++){
                    for(int k = 0; k < c[j]; k++){
                        ans += '0' + j;
                    }
                }
                cout << ans << endl;
                return 0;
            }
            else{
                ans += '0' + ind;
                c[ind]--;
            }
        }
    }
}
0