結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー H3PO4H3PO4
提出日時 2023-10-11 17:59:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 16 ms / 2,000 ms
コード長 1,733 bytes
コンパイル時間 631 ms
コンパイル使用メモリ 73,748 KB
実行使用メモリ 4,968 KB
最終ジャッジ日時 2023-10-11 17:59:21
合計ジャッジ時間 2,251 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 1 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,348 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 1 ms
4,352 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 1 ms
4,352 KB
testcase_16 AC 15 ms
4,356 KB
testcase_17 AC 16 ms
4,604 KB
testcase_18 AC 12 ms
4,660 KB
testcase_19 AC 13 ms
4,688 KB
testcase_20 AC 12 ms
4,824 KB
testcase_21 AC 12 ms
4,860 KB
testcase_22 AC 12 ms
4,968 KB
testcase_23 AC 11 ms
4,352 KB
testcase_24 AC 14 ms
4,348 KB
testcase_25 AC 14 ms
4,348 KB
testcase_26 AC 13 ms
4,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

inline int ctoi(const char &c) { return (int)c - '0'; };
inline char itoc(const int &n) { return (char)n + '0'; };

std::string solve(const int N, const std::string &K,
                  const 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), itoc(i));
        }
        return res;
    }

    // i桁目より上の判定
    int max_i = K.size() - 1;
    std::array<int, 9> D;
    for (int i = 0; i < 9; i++) D.at(i) = C.at(i);
    for (int i = 0; i < K.size() - 1; i++) {
        int k = ctoi(K.at(i));
        if (k == 0) {
            max_i = i;
            break;
        }
        if (D.at(k - 1) <= 0) {
            max_i = i;
            break;
        }
        D.at(k - 1)--;
    }

    for (int i = max_i; i >= 0; i--) {
        std::array<int, 9> used;
        for (int j = 0; j < 9; j++) used.at(j) = D.at(j);

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

        if (i == 0) break;
        D.at(ctoi(K.at(i - 1)) - 1)++;
    }
    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