結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー H3PO4H3PO4
提出日時 2023-10-09 10:50:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,510 bytes
コンパイル時間 942 ms
コンパイル使用メモリ 74,748 KB
実行使用メモリ 8,780 KB
最終ジャッジ日時 2023-10-09 10:50:59
合計ジャッジ時間 5,272 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,760 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,388 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 TLE -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <array>
#include <iostream>
#include <string>
#include <vector>

std::string solve(int N, std::string K, std::array<int, 9> C) {
    if (K.size() > N) return "-1";
    if (K.size() < N) {
        // 一番小さくなるように作る
        std::string res;
        for (int i = 1; i < 10; i++) {
            res += std::string(C.at(i - 1), char(i) + '0');
        }
        return res;
    }
    for (int i = K.size() - 1; i >= 0; i--) {
        std::array<int, 9> used;
        for (int j = 0; j < 9; j++) used.at(j) = C.at(j);

        bool flag = true;
        for (int j = 0; j < i; j++) {
            int k = (int)K.at(j) - '0';
            if (k == 0) {
                flag = false;
                break;
            };
            used.at(k - 1)--;
            if (used.at(k - 1) < 0) {
                flag = false;
                break;
            }
        }
        if (!flag) continue;

        for (int n = (int)K.at(i) - '0' + 1; n < 10; n++) {
            if (used.at(n - 1) <= 0) continue;
            used.at(n - 1)--;
            std::string res = K.substr(0, i);
            res += (char)n + '0';
            for (int i = 1; i < 10; i++) {
                res += std::string(used.at(i - 1), char(i) + '0');
            }
            return res;
        }
    }
    return "-1";
}

int main() {
    int N;
    std::string K;
    std::array<int, 9> C;
    std::cin >> N >> K;
    for (int i = 0; i < 9; i++) std::cin >> C.at(i);
    std::cout << solve(N, K, C) << std::endl;
}
0