結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
75,840 KB
testcase_01 AC 70 ms
71,564 KB
testcase_02 AC 72 ms
71,372 KB
testcase_03 AC 74 ms
71,476 KB
testcase_04 AC 72 ms
71,460 KB
testcase_05 AC 72 ms
71,344 KB
testcase_06 AC 71 ms
71,480 KB
testcase_07 AC 73 ms
71,376 KB
testcase_08 AC 73 ms
71,484 KB
testcase_09 AC 72 ms
71,444 KB
testcase_10 AC 75 ms
76,188 KB
testcase_11 AC 76 ms
76,188 KB
testcase_12 AC 75 ms
76,076 KB
testcase_13 AC 76 ms
76,304 KB
testcase_14 AC 78 ms
75,924 KB
testcase_15 AC 77 ms
75,964 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: list[int] = []
        for i, c in enumerate(C, 1):
            res.extend([i] * c)
        return "".join(map(str, 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: list[int] = []
                for j in range(i):
                    res.append(int(K[j]))
                res.append(n)
                # i桁目より下の処理
                for i, c in enumerate(used, 1):
                    res.extend([i] * c)
                return "".join(map(str, res))
    return "-1"


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

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