結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー H3PO4H3PO4
提出日時 2023-10-09 09:54:01
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,137 bytes
コンパイル時間 2,037 ms
コンパイル使用メモリ 10,860 KB
実行使用メモリ 13,496 KB
最終ジャッジ日時 2023-10-09 09:54:10
合計ジャッジ時間 8,586 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
12,220 KB
testcase_01 AC 16 ms
7,876 KB
testcase_02 AC 15 ms
7,836 KB
testcase_03 AC 16 ms
7,776 KB
testcase_04 AC 16 ms
7,764 KB
testcase_05 AC 16 ms
7,764 KB
testcase_06 AC 16 ms
7,904 KB
testcase_07 AC 16 ms
7,840 KB
testcase_08 AC 17 ms
7,776 KB
testcase_09 AC 16 ms
7,776 KB
testcase_10 AC 17 ms
7,780 KB
testcase_11 AC 17 ms
7,784 KB
testcase_12 AC 17 ms
7,936 KB
testcase_13 AC 17 ms
7,900 KB
testcase_14 AC 27 ms
7,892 KB
testcase_15 AC 20 ms
7,904 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 #

def solve(N: int, K: str, C: list[int]) -> str:
    if len(K) > N:
        return "-1"
    if len(K) < N:
        # 一番小さくなるように作る
        res: str = ""
        for i, c in enumerate(C, 1):
            res += str(i) * c
        return res
    for i in range(len(K) - 1, -1, -1):
        # i桁目はK[i]より大きく、i桁目より上はKと同じ、i桁目より下は最小
        used = C.copy()

        # i桁目より上の判定
        for j in range(i):
            k = int(K[j])
            if k == 0:
                break
            used[k - 1] -= 1
            if used[k - 1] < 0:
                break
        else:
            for n in range(int(K[i]) + 1, 10):
                # i桁目の判定
                if used[n - 1] <= 0:
                    continue
                used[n - 1] -= 1
                res = K[:i] + str(n)
                # i桁目より下の処理
                for i, c in enumerate(used, 1):
                    res += str(i) * c
                return res
    return "-1"


N, K = input().split()
C = list(map(int, input().split()))

print(solve(int(N), K, C))
0