結果

問題 No.738 平らな農地
ユーザー しらっ亭しらっ亭
提出日時 2018-09-28 20:07:53
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,331 bytes
コンパイル時間 146 ms
コンパイル使用メモリ 82,464 KB
実行使用メモリ 107,244 KB
最終ジャッジ日時 2024-04-20 10:01:43
合計ジャッジ時間 24,810 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
53,736 KB
testcase_01 AC 39 ms
53,988 KB
testcase_02 AC 37 ms
53,852 KB
testcase_03 AC 38 ms
54,252 KB
testcase_04 AC 38 ms
53,804 KB
testcase_05 AC 132 ms
78,304 KB
testcase_06 AC 145 ms
79,972 KB
testcase_07 AC 133 ms
78,820 KB
testcase_08 AC 97 ms
77,656 KB
testcase_09 AC 60 ms
71,368 KB
testcase_10 AC 54 ms
65,464 KB
testcase_11 AC 94 ms
78,088 KB
testcase_12 AC 63 ms
71,956 KB
testcase_13 AC 119 ms
78,172 KB
testcase_14 AC 70 ms
76,024 KB
testcase_15 AC 374 ms
96,780 KB
testcase_16 AC 396 ms
99,740 KB
testcase_17 AC 397 ms
99,340 KB
testcase_18 AC 396 ms
97,048 KB
testcase_19 AC 438 ms
99,476 KB
testcase_20 AC 387 ms
99,648 KB
testcase_21 AC 439 ms
99,628 KB
testcase_22 AC 404 ms
99,892 KB
testcase_23 AC 435 ms
99,432 KB
testcase_24 AC 437 ms
99,412 KB
testcase_25 AC 67 ms
73,604 KB
testcase_26 AC 65 ms
74,036 KB
testcase_27 AC 66 ms
74,720 KB
testcase_28 AC 67 ms
73,912 KB
testcase_29 AC 67 ms
74,112 KB
testcase_30 AC 66 ms
74,332 KB
testcase_31 AC 69 ms
74,328 KB
testcase_32 AC 67 ms
73,640 KB
testcase_33 AC 67 ms
74,116 KB
testcase_34 AC 65 ms
73,420 KB
testcase_35 AC 65 ms
72,384 KB
testcase_36 AC 64 ms
74,196 KB
testcase_37 AC 67 ms
73,492 KB
testcase_38 AC 67 ms
73,660 KB
testcase_39 AC 67 ms
75,008 KB
testcase_40 AC 68 ms
74,572 KB
testcase_41 AC 72 ms
76,244 KB
testcase_42 AC 64 ms
73,292 KB
testcase_43 AC 65 ms
73,388 KB
testcase_44 AC 65 ms
73,280 KB
testcase_45 AC 397 ms
101,420 KB
testcase_46 AC 392 ms
98,576 KB
testcase_47 AC 393 ms
101,224 KB
testcase_48 AC 374 ms
100,632 KB
testcase_49 AC 369 ms
99,868 KB
testcase_50 AC 382 ms
100,844 KB
testcase_51 AC 410 ms
101,436 KB
testcase_52 AC 385 ms
100,072 KB
testcase_53 AC 387 ms
100,812 KB
testcase_54 AC 395 ms
101,364 KB
testcase_55 AC 412 ms
100,908 KB
testcase_56 AC 405 ms
101,016 KB
testcase_57 AC 385 ms
98,092 KB
testcase_58 AC 392 ms
100,804 KB
testcase_59 AC 392 ms
101,604 KB
testcase_60 AC 388 ms
101,176 KB
testcase_61 AC 390 ms
101,324 KB
testcase_62 AC 371 ms
98,096 KB
testcase_63 AC 404 ms
101,788 KB
testcase_64 AC 411 ms
101,272 KB
testcase_65 AC 60 ms
79,916 KB
testcase_66 AC 61 ms
80,656 KB
testcase_67 AC 340 ms
102,940 KB
testcase_68 AC 297 ms
106,220 KB
testcase_69 AC 401 ms
106,552 KB
testcase_70 AC 367 ms
107,244 KB
testcase_71 AC 218 ms
88,728 KB
testcase_72 AC 206 ms
89,052 KB
testcase_73 AC 198 ms
90,960 KB
testcase_74 AC 223 ms
90,452 KB
testcase_75 WA -
testcase_76 WA -
testcase_77 AC 421 ms
103,568 KB
testcase_78 AC 445 ms
106,816 KB
testcase_79 WA -
testcase_80 WA -
testcase_81 WA -
testcase_82 WA -
testcase_83 AC 256 ms
89,764 KB
testcase_84 AC 254 ms
89,752 KB
testcase_85 AC 486 ms
106,668 KB
testcase_86 AC 447 ms
103,008 KB
testcase_87 WA -
testcase_88 WA -
testcase_89 AC 38 ms
53,876 KB
testcase_90 AC 39 ms
53,304 KB
testcase_91 AC 38 ms
53,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect
import math


class FenwickTree:
    def __init__(self, n):
        # 0-indexed
        self.N = n
        self.bit = [0] * n
        self.M = 1 << (31 - int(math.ceil(math.log2(self.N))))

    def add(self, i, val):
        while i < self.N:
            self.bit[i] += val
            i |= i + 1

    def sum(self, n):
        """[0, n)"""
        ret = 0
        n -= 1
        while n >= 0:
            ret += self.bit[n]
            n = (n & (n + 1)) - 1
        return ret

    def sum_lr(self, l, r):
        """[l, r)"""
        return self.sum(r) - self.sum(l)

    def count_index(self, w):
        x = 0
        k = self.M - 1

        while True:
            if x + k < self.N and self.bit[x + k] < w:
                w -= self.bit[x + k]
                x += k + 1
            if k == 0:
                break
            k >>= 1
        return x


def solve():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))

    if k == 1:
        return 0

    compressor = list(set(A))
    compressor.sort()

    def compress(x):
        return bisect.bisect_left(compressor, x)

    C = [compress(a) for a in A]
    m = len(compressor)

    counter = FenwickTree(m)

    for i in range(k):
        counter.add(C[i], 1)

    k2 = k // 2 + 1

    medians = []
    medians.append(counter.count_index(k2))
    for i in range(k, n):
        counter.add(C[i], 1)
        counter.add(C[i-k], -1)
        medians.append(counter.count_index(k2))

    ans = 1e18

    ft_cnt = FenwickTree(m)
    ft_sum = FenwickTree(m)

    for i in range(n):
        c = compress(A[i])
        ft_cnt.add(c, 1)
        ft_sum.add(c, A[i])

        if i >= k:
            r = compress(A[i - k])
            ft_cnt.add(r, -1)
            ft_sum.add(r, -A[i - k])

        if i >= k - 1:
            median_c = medians[i - k + 1]
            median = compressor[median_c]

            lower_cnt = ft_cnt.sum(median_c)
            lower_sum = ft_sum.sum(median_c)

            higher_cnt = ft_cnt.sum_lr(median_c, m)
            higher_sum = ft_sum.sum_lr(median_c, m)

            inc_cost = median * lower_cnt - lower_sum
            dec_cost = higher_sum - median * higher_cnt
            cand = inc_cost + dec_cost

            ans = min(ans, cand)
    return ans


if __name__ == '__main__':
    print(solve())
0