結果
問題 | No.1077 Noelちゃんと星々4 |
ユーザー |
![]() |
提出日時 | 2020-06-12 22:42:42 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 276 ms / 2,000 ms |
コード長 | 1,576 bytes |
コンパイル時間 | 408 ms |
コンパイル使用メモリ | 82,360 KB |
実行使用メモリ | 153,884 KB |
最終ジャッジ日時 | 2024-06-24 05:27:18 |
合計ジャッジ時間 | 4,698 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 20 |
ソースコード
def getMinimumOps(ar):# Number of elements in the arrayn = len(ar)# Smallest element in the arraysmall = min(ar)# Largest element in the arraylarge = max(ar)"""dp(i, j) represents the minimum numberof operations needed to make thearray[0 .. i] sorted in non-decreasingorder given that ith element is j"""dp = [[ 0 for i in range(large + 1)]for i in range(n)]# Fill the dp[]][ array for base casesfor j in range(small, large + 1):dp[0][j] = abs(ar[0] - j)"""/*Using results for the first (i - 1)elements, calculate the resultfor the ith element*/"""for i in range(1, n):minimum = 10**9for j in range(small, large + 1):# """# /*# If the ith element is j then we can have# any value from small to j for the i-1 th# element# We choose the one that requires the# minimum operations# """minimum = min(minimum, dp[i - 1][j])dp[i][j] = minimum + abs(ar[i] - j)"""/*If we made the (n - 1)th element equal to jwe required dp(n-1, j) operationsWe choose the minimum among all possibledp(n-1, j) where j goes from small to large*/"""ans = 10**9for j in range(small, large + 1):ans = min(ans, dp[n - 1][j])return ansN = int(input())A = list(map(int,input().split()))print(getMinimumOps(A))