結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー Carpenters-CatCarpenters-Cat
提出日時 2021-07-31 03:15:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,771 bytes
コンパイル時間 1,611 ms
コンパイル使用メモリ 165,700 KB
実行使用メモリ 15,568 KB
最終ジャッジ日時 2023-10-14 11:48:38
合計ジャッジ時間 3,543 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 WA -
testcase_05 WA -
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 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 37 ms
5,604 KB
testcase_23 AC 12 ms
5,600 KB
testcase_24 WA -
testcase_25 AC 25 ms
15,468 KB
testcase_26 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int num[10];
int K[500050];
int ans[500050];
bool solve(int i) {
    if (num[K[i]] > 0) {
        num[K[i]] --;
        if (solve(i + 1)) {
            ans[i] = K[i];
            return true;
        } else {
            num[K[i]] ++;
            for (int j = K[i] + 1; j < 10; j ++) {
                if (num[j] > 0) {
                    ans[i] = j;
                    num[j] --;
                    return true;
                }
            }
            return false;
        }
    } else {
        for (int j = K[i] + 1; j < 10; j ++) {
            if (num[j] > 0) {
                num[j] --;
                ans[i] = j;
                return true;
            }
        }
        return false;
    }
}
int main () {
    int N;
    cin >> N;
    string S;
    cin >> S;
    for (int i = 0; i < S.size(); i ++) {
        K[i] = (int)S[i] - (int)'0';
    }
    if (N < S.size()) {
        cout << -1 << endl;
        return 0;
    }
    if (N > S.size()) {
        for (int i = 1; i < 10; i ++) {
            int x;
            cin >> x;
            for (int j = 0; j < x; j ++) {
                cout << i;
            }
        }
        cout << endl;
        return 0;
    }
    for (int i = 1; i < 10; i ++) {
        cin >> num[i];
    }
    for (int i = 0; i < N; i ++) {
        ans[i] = -1;
    }
    if (!solve(0)) {
        cout << -1 << endl;
        return 0;
    }
    for (int i= 0; i < N; i ++) {
        if (ans[i] == -1) {
            cout << ans[i];
        } else{
            for (int j = 1; j < 10; j ++) {
                if (num[j] > 0) {
                    num[j] --;
                    cout << j;
                    break;
                }
            }
        }
    }
    cout << endl;
}
0