結果

問題 No.1391 ±1 Abs Sum
ユーザー 👑 terry_u16terry_u16
提出日時 2021-02-12 22:50:16
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 363 ms / 2,000 ms
コード長 1,531 bytes
コンパイル時間 666 ms
コンパイル使用メモリ 87,232 KB
実行使用メモリ 117,160 KB
最終ジャッジ日時 2023-09-27 05:46:53
合計ジャッジ時間 11,862 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 153 ms
80,192 KB
testcase_01 AC 149 ms
80,408 KB
testcase_02 AC 147 ms
80,180 KB
testcase_03 AC 148 ms
80,296 KB
testcase_04 AC 148 ms
80,192 KB
testcase_05 AC 149 ms
80,124 KB
testcase_06 AC 147 ms
80,184 KB
testcase_07 AC 149 ms
80,180 KB
testcase_08 AC 156 ms
80,756 KB
testcase_09 AC 152 ms
80,760 KB
testcase_10 AC 148 ms
81,024 KB
testcase_11 AC 354 ms
116,448 KB
testcase_12 AC 318 ms
108,528 KB
testcase_13 AC 340 ms
112,596 KB
testcase_14 AC 306 ms
100,132 KB
testcase_15 AC 301 ms
102,372 KB
testcase_16 AC 319 ms
108,552 KB
testcase_17 AC 324 ms
105,420 KB
testcase_18 AC 363 ms
112,788 KB
testcase_19 AC 305 ms
105,212 KB
testcase_20 AC 353 ms
116,520 KB
testcase_21 AC 315 ms
107,500 KB
testcase_22 AC 303 ms
107,176 KB
testcase_23 AC 308 ms
111,400 KB
testcase_24 AC 310 ms
110,268 KB
testcase_25 AC 304 ms
111,012 KB
testcase_26 AC 303 ms
111,088 KB
testcase_27 AC 286 ms
103,504 KB
testcase_28 AC 287 ms
104,880 KB
testcase_29 AC 291 ms
110,740 KB
testcase_30 AC 298 ms
104,592 KB
testcase_31 AC 347 ms
116,628 KB
testcase_32 AC 234 ms
97,800 KB
testcase_33 AC 246 ms
102,232 KB
testcase_34 AC 278 ms
116,292 KB
testcase_35 AC 147 ms
80,328 KB
testcase_36 AC 289 ms
117,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import typing

class FenwickTree:
    '''Reference: https://en.wikipedia.org/wiki/Fenwick_tree'''

    def __init__(self, n: int = 0) -> None:
        self._n = n
        self.data = [0] * n

    def add(self, p: int, x: typing.Any) -> None:
        assert 0 <= p < self._n

        p += 1
        while p <= self._n:
            self.data[p - 1] += x
            p += p & -p

    def sum(self, left: int, right: int) -> typing.Any:
        assert 0 <= left <= right <= self._n

        return self._sum(right) - self._sum(left)

    def _sum(self, r: int) -> typing.Any:
        s = 0
        while r > 0:
            s += self.data[r - 1]
            r -= r & -r

        return s

n, k = map(int, input().split())
a = list(map(int, input().split()))
bit = FenwickTree(n)

current = 0

for i in range(n):
    if i < k:
        current += abs(a[i] - a[0])
        bit.add(i, 1)
    else:
        current -= abs(a[i] - a[0])
        bit.add(i, -1)

minimum = current
nextSub = k

for i in range(1, n):
    plus = -bit.sum(i, n) + bit.sum(0, i)
    current += plus * abs(a[i] - a[i - 1])

    while nextSub < n:
        left = a[nextSub - k]
        right = a[nextSub]
        leftDiff = abs(left - a[i])
        rightDiff = abs(right - a[i])

        if leftDiff > rightDiff:
            current -= 2 * leftDiff
            current += 2 * rightDiff
            bit.add(nextSub, 2)
            bit.add(nextSub - k, -2)
            nextSub += 1
        else:
            break
    
    minimum = min(minimum, current)

print(minimum)
0