結果

問題 No.1536 仕切り直し
ユーザー ygd.ygd.
提出日時 2021-06-12 20:45:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 154 ms / 2,000 ms
コード長 1,511 bytes
コンパイル時間 329 ms
コンパイル使用メモリ 87,024 KB
実行使用メモリ 90,500 KB
最終ジャッジ日時 2023-08-22 18:01:43
合計ジャッジ時間 3,598 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,132 KB
testcase_01 AC 70 ms
71,100 KB
testcase_02 AC 69 ms
71,212 KB
testcase_03 AC 84 ms
76,084 KB
testcase_04 AC 112 ms
84,112 KB
testcase_05 AC 137 ms
90,412 KB
testcase_06 AC 87 ms
76,648 KB
testcase_07 AC 84 ms
75,960 KB
testcase_08 AC 110 ms
82,968 KB
testcase_09 AC 116 ms
85,100 KB
testcase_10 AC 154 ms
90,500 KB
testcase_11 AC 98 ms
76,844 KB
testcase_12 AC 140 ms
90,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    N,M = map(int,input().split())
    A = list(map(int,input().split()))
    #dp[i][j]: i番目まで見てj個の仕切り
    dp = [[0]*(M+1) for _ in range(N+1)]

    #初期化. j = 0の時はそのままなのでそれを入れる
    for i in range(N):
        dp[i+1][0] = dp[i][0] + A[i]

    for i in range(1,N+1):
        for j in range(1,M+1):
            if j%2 == 0: #1-indexで偶数の時はそのまま
                dp[i][j] = max(dp[i-1][j] + A[i-1], dp[i-1][j-1] + A[i-1])
            else:
                dp[i][j] = max(dp[i-1][j] - A[i-1], dp[i-1][j-1] - A[i-1])
    
    #print(dp)

    #答えはdp[N][j]の最大値.途中で2枚以上入れる場合、最後に入れるとしてよい.
    ans = max(dp[N])
    for j in range(M+1):
        if dp[N][j] == ans:
            num = j; break
    #num枚入れる。
    ret = []
    #print(num)
    for i in reversed(range(1,N+1)):
        if num == 0: break
        #print("first",i,num,ans)
        if num%2 == 0:
            ans -= A[i-1]
            if dp[i-1][num-1] == ans: #ここに入っていた
                ret.append(i-1)
                num -= 1

        else:
            ans += A[i-1]
            if dp[i-1][num-1] == ans:
                ret.append(i-1)
                num -= 1

        #print("second",i,num,ans)
    #print(ret)
    ret.sort()
    for a in ret:
        print(a)
    for i in range(M - len(ret)): #最後にまとめて入れる
        print(N)



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