結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー ぷらぷら
提出日時 2021-07-30 21:21:13
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 28 ms / 2,000 ms
コード長 3,695 bytes
コンパイル時間 2,233 ms
コンパイル使用メモリ 201,904 KB
実行使用メモリ 5,220 KB
最終ジャッジ日時 2023-10-14 03:10:42
合計ジャッジ時間 3,921 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
    int N;
    string K;
    cin >> N >> K;
    vector<int>tmp(9);
    for(int i = 0; i < 9; i++) {
        cin >> tmp[i];
    }
    if(N > K.size()) {
        string S = "";
        for(int i = 0; i < 9; i++) {
            S += string(tmp[i],(char)('0'+i+1));
        }
        cout << S << endl;
    }
    else if(N < K.size()) {
        cout << -1 << endl;
    }
    else {
        string S = "";
        bool flag = false;
        for(int i = 0; i < N; i++) {
            for(int j = 0; j < 9; j++) {
                char c = (char)('0'+j+1);
                if(flag) {
                    if(tmp[j]) {
                        tmp[j]--;
                        S += c;
                        break;
                    }
                }
                else {
                    if(K[i] == c && tmp[j]) {
                        tmp[j]--;
                        S += c;
                        break;
                    }
                    if(K[i] < c && tmp[j]) {
                        tmp[j]--;
                        S += c;
                        flag = true;
                        break;
                    }
                }
            }
            if(i+1 != S.size()) {
                bool ok = false;
                for(int j = i-1; j >= 0; j--) {
                    for(int k = 0; k < 9; k++) {
                        char c = (char)('0'+k+1);
                        if(tmp[k] && K[j] < c) {
                            tmp[S[j]-'0'-1]++;
                            S[j] = c;
                            tmp[k]--;
                            ok = true;
                            break;
                        }
                    }
                    if(ok) {
                        break;
                    }
                    tmp[S[j]-'0'-1]++;
                    S.pop_back();
                }
                if(!ok) {
                    cout << -1 << endl;
                    return 0;
                }
                flag = true;
                for(i = S.size(); i < N; i++) {
                    for(int j = 0; j < 9; j++) {
                        char c = (char)('0'+j+1);
                        if(flag) {
                            if(tmp[j]) {
                                tmp[j]--;
                                S += c;
                                break;
                            }
                        }
                    }
                }
                break;
            }
        }
        if(!flag) {
            bool ok = false;
            for(int j = N-1; j >= 0; j--) {
                for(int k = 0; k < 9; k++) {
                    char c = (char)('0'+k+1);
                    if(tmp[k] && K[j] < c) {
                        tmp[S[j]-'0'-1]++;
                        S[j] = c;
                        tmp[k]--;
                        ok = true;
                        break;
                    }
                }
                if(ok) {
                    break;
                }
                tmp[S[j]-'0'-1]++;
                S.pop_back();
            }
            if(!ok) {
                cout << -1 << endl;
                return 0;
            }
            flag = true;
            for(int i = S.size(); i < N; i++) {
                for(int j = 0; j < 9; j++) {
                    char c = (char)('0'+j+1);
                    if(flag) {
                        if(tmp[j]) {
                            tmp[j]--;
                            S += c;
                            break;
                        }
                    }
                }
            }
        }
        cout << S << endl;
    }
}
0