結果
問題 | No.1077 Noelちゃんと星々4 |
ユーザー | Nagisa |
提出日時 | 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 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 38 ms
52,596 KB |
testcase_01 | AC | 38 ms
54,348 KB |
testcase_02 | AC | 223 ms
129,244 KB |
testcase_03 | AC | 270 ms
144,984 KB |
testcase_04 | AC | 116 ms
91,560 KB |
testcase_05 | AC | 105 ms
90,800 KB |
testcase_06 | AC | 142 ms
102,752 KB |
testcase_07 | AC | 164 ms
106,640 KB |
testcase_08 | AC | 127 ms
98,928 KB |
testcase_09 | AC | 117 ms
91,292 KB |
testcase_10 | AC | 260 ms
145,252 KB |
testcase_11 | AC | 189 ms
121,208 KB |
testcase_12 | AC | 169 ms
112,224 KB |
testcase_13 | AC | 110 ms
90,272 KB |
testcase_14 | AC | 253 ms
143,496 KB |
testcase_15 | AC | 168 ms
111,104 KB |
testcase_16 | AC | 128 ms
99,100 KB |
testcase_17 | AC | 176 ms
116,068 KB |
testcase_18 | AC | 259 ms
146,632 KB |
testcase_19 | AC | 109 ms
89,516 KB |
testcase_20 | AC | 40 ms
57,296 KB |
testcase_21 | AC | 276 ms
153,884 KB |
ソースコード
def getMinimumOps(ar): # Number of elements in the array n = len(ar) # Smallest element in the array small = min(ar) # Largest element in the array large = max(ar) """ dp(i, j) represents the minimum number of operations needed to make the array[0 .. i] sorted in non-decreasing order 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 cases for j in range(small, large + 1): dp[0][j] = abs(ar[0] - j) """ /* Using results for the first (i - 1) elements, calculate the result for the ith element */ """ for i in range(1, n): minimum = 10**9 for 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 j we required dp(n-1, j) operations We choose the minimum among all possible dp(n-1, j) where j goes from small to large */ """ ans = 10**9 for j in range(small, large + 1): ans = min(ans, dp[n - 1][j]) return ans N = int(input()) A = list(map(int,input().split())) print(getMinimumOps(A))