結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー ygd.ygd.
提出日時 2021-07-30 21:33:06
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 1,830 bytes
コンパイル時間 235 ms
コンパイル使用メモリ 10,708 KB
実行使用メモリ 9,436 KB
最終ジャッジ日時 2023-10-14 03:40:26
合計ジャッジ時間 2,420 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
8,468 KB
testcase_01 AC 20 ms
8,476 KB
testcase_02 AC 20 ms
8,616 KB
testcase_03 AC 19 ms
8,596 KB
testcase_04 AC 19 ms
8,644 KB
testcase_05 AC 20 ms
8,544 KB
testcase_06 AC 19 ms
8,644 KB
testcase_07 AC 20 ms
8,596 KB
testcase_08 AC 20 ms
8,440 KB
testcase_09 AC 20 ms
8,616 KB
testcase_10 AC 20 ms
8,508 KB
testcase_11 AC 19 ms
8,548 KB
testcase_12 AC 19 ms
8,640 KB
testcase_13 AC 20 ms
8,652 KB
testcase_14 AC 20 ms
8,564 KB
testcase_15 AC 20 ms
8,436 KB
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import copy

def main():
    N,K = map(int,input().split())
    K = list(str(K))
    K = [int(k) for k in K]
    if len(K) < N:
        t = N - len(K)
        K = [0 for _ in range(t)] + K
    #print(K)
    C = [0] + list(map(int,input().split()))

    #dp[i][j]:i桁目まで見て0/1完全に一致/より大の時の[値、残りのリスト]
    dp = [[],[]]
    val = -1
    if C[K[0]] >= 1:
        NC = copy.copy(C)
        NC[K[0]] -= 1
        dp[0] = [str(K[0]), NC]
    else:
        dp[0] = ["-1",[-1]*10]
    Flag = 0
    for i in range(K[0]+1,10):
        if C[i] >= 1:
            Flag = 1
            NC = copy.copy(C)
            NC[i] -= 1
            dp[1] = [str(i), NC]
            break
    if Flag == 0:
        dp[1] = ["-1",[-1]*10]
    #print(dp)
    for i in range(1,N):
        #p = [[],[]]
        #p,dp = dp,p
        nxt = K[i]
        pre_val = dp[0][0]
        pre1 = copy.copy(dp[0][1])
        if dp[0][1][nxt] >= 1:
            dp[0][0] = pre_val + str(nxt)
            dp[0][1][nxt] -= 1 #一個減らす
        else:
            dp[0] = ["-1",[-1]*10]
        Flag = 0
        pre2_val = dp[1][0]
        pre2 = copy.copy(dp[1][1])
        for j in range(nxt+1,10):
            if pre1[j] >= 1:
                Flag = 1
                pre1[j] -= 1
                dp[1][0] = pre_val + str(j)
                dp[1][1] = pre1
                break
        if Flag == 0:
            #print("2",pre2)
            for j in range(1,10):
                if pre2[j] >= 1:
                    Flag = 1
                    pre2[j] -= 1
                    dp[1][0] = pre2_val + str(j)
                    dp[1][1] = pre2
                    break
        if Flag == 0:
            dp[1] = ["-1",[-1]*10]
        #print(dp)
    ans = dp[1][0]
    print(ans)
    

if __name__ == '__main__':
    main()
0