結果

問題 No.1077 Noelちゃんと星々4
ユーザー NagisaNagisa
提出日時 2020-06-12 22:42:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 267 ms / 2,000 ms
コード長 1,576 bytes
コンパイル時間 559 ms
コンパイル使用メモリ 87,292 KB
実行使用メモリ 155,184 KB
最終ジャッジ日時 2023-09-06 10:51:46
合計ジャッジ時間 4,665 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
71,184 KB
testcase_01 AC 59 ms
71,168 KB
testcase_02 AC 220 ms
130,328 KB
testcase_03 AC 267 ms
146,060 KB
testcase_04 AC 130 ms
97,476 KB
testcase_05 AC 114 ms
90,700 KB
testcase_06 AC 144 ms
103,856 KB
testcase_07 AC 166 ms
107,804 KB
testcase_08 AC 135 ms
99,212 KB
testcase_09 AC 128 ms
96,328 KB
testcase_10 AC 244 ms
145,928 KB
testcase_11 AC 184 ms
121,636 KB
testcase_12 AC 173 ms
113,416 KB
testcase_13 AC 117 ms
90,360 KB
testcase_14 AC 240 ms
144,212 KB
testcase_15 AC 167 ms
112,436 KB
testcase_16 AC 138 ms
99,844 KB
testcase_17 AC 177 ms
117,020 KB
testcase_18 AC 244 ms
147,568 KB
testcase_19 AC 109 ms
90,412 KB
testcase_20 AC 63 ms
75,108 KB
testcase_21 AC 264 ms
155,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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))
0