結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー ygd.ygd.
提出日時 2021-07-30 21:26:05
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,806 bytes
コンパイル時間 286 ms
コンパイル使用メモリ 86,744 KB
実行使用メモリ 80,868 KB
最終ジャッジ日時 2023-10-14 03:23:05
合計ジャッジ時間 5,213 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
71,288 KB
testcase_01 AC 88 ms
71,400 KB
testcase_02 AC 87 ms
71,396 KB
testcase_03 AC 86 ms
71,396 KB
testcase_04 AC 86 ms
71,376 KB
testcase_05 AC 86 ms
71,492 KB
testcase_06 AC 87 ms
71,420 KB
testcase_07 AC 86 ms
71,468 KB
testcase_08 AC 87 ms
71,272 KB
testcase_09 AC 85 ms
71,356 KB
testcase_10 AC 85 ms
71,288 KB
testcase_11 AC 87 ms
71,296 KB
testcase_12 AC 86 ms
71,428 KB
testcase_13 AC 88 ms
71,192 KB
testcase_14 AC 89 ms
71,316 KB
testcase_15 AC 87 ms
71,364 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] = [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] = [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*10 + 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*10 + 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*10 + 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