結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー nawawannawawan
提出日時 2021-07-30 21:08:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,659 bytes
コンパイル時間 1,781 ms
コンパイル使用メモリ 173,492 KB
実行使用メモリ 5,828 KB
最終ジャッジ日時 2023-10-14 02:18:34
合計ジャッジ時間 3,385 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 2 ms
4,372 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,372 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 16 ms
5,608 KB
testcase_19 WA -
testcase_20 AC 15 ms
5,660 KB
testcase_21 AC 16 ms
5,572 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 13 ms
5,116 KB
testcase_26 AC 17 ms
5,608 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);
        }
    }
    if(ma <= K) cout << -1 << endl;
    else{
        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]);
                            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]++;
                    }
                }
                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