atcoder Educational DP Contest B-Frog2をpython3で解いているのですが、
Traceback (most recent call last): File ".\atcoder.py", line 20, in <module> chmin(dp[i+j], dp[i]+abs(dp[i]-h[i+j])) IndexError: list index out of range
というエラーがでます。単純なミスだと思うのですが全くわからないまま時間が過ぎてしまったのでどなたか教えていただけると幸いです。コードは以下になります。Qiitaの解説を参考にしています。
import math import sys import os f = open('input.txt', 'r') sys.stdin = f def chmin(a, b): if a > b: a = b return a n, k = map(int, (input().split())) h = list(input().split()) h = [int(i) for i in h] dp = [100000]*n dp[0] = 0 for i in range(0, n): for j in range(1, k+1): if i+j <= n: chmin(dp[i+j], dp[i]+abs(dp[i]-h[i+j])) print(dp[n-1])
input
15 3 210 30 40 50 20 3
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/03/22 06:17
退会済みユーザー
2019/03/22 09:00