結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー H3PO4H3PO4
提出日時 2023-10-09 09:54:20
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,137 bytes
コンパイル時間 466 ms
コンパイル使用メモリ 87,264 KB
実行使用メモリ 82,156 KB
最終ジャッジ日時 2023-10-09 09:54:26
合計ジャッジ時間 5,841 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
78,216 KB
testcase_01 AC 69 ms
71,344 KB
testcase_02 AC 74 ms
71,572 KB
testcase_03 AC 71 ms
71,340 KB
testcase_04 AC 71 ms
71,592 KB
testcase_05 AC 73 ms
71,624 KB
testcase_06 AC 71 ms
71,496 KB
testcase_07 AC 71 ms
71,340 KB
testcase_08 AC 70 ms
71,616 KB
testcase_09 AC 71 ms
71,600 KB
testcase_10 AC 77 ms
75,960 KB
testcase_11 AC 74 ms
76,188 KB
testcase_12 AC 74 ms
75,776 KB
testcase_13 AC 75 ms
75,948 KB
testcase_14 AC 76 ms
76,248 KB
testcase_15 AC 75 ms
76,152 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