結果

問題 No.738 平らな農地
ユーザー しらっ亭しらっ亭
提出日時 2018-09-28 20:12:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 393 ms / 2,000 ms
コード長 2,304 bytes
コンパイル時間 332 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 107,276 KB
最終ジャッジ日時 2024-04-20 10:03:21
合計ジャッジ時間 20,512 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
52,480 KB
testcase_01 AC 46 ms
52,736 KB
testcase_02 AC 43 ms
52,736 KB
testcase_03 AC 41 ms
52,480 KB
testcase_04 AC 41 ms
53,120 KB
testcase_05 AC 100 ms
77,744 KB
testcase_06 AC 105 ms
77,200 KB
testcase_07 AC 106 ms
77,896 KB
testcase_08 AC 88 ms
76,544 KB
testcase_09 AC 62 ms
67,712 KB
testcase_10 AC 55 ms
63,360 KB
testcase_11 AC 88 ms
76,544 KB
testcase_12 AC 64 ms
69,120 KB
testcase_13 AC 103 ms
77,772 KB
testcase_14 AC 65 ms
70,656 KB
testcase_15 AC 286 ms
97,024 KB
testcase_16 AC 296 ms
99,064 KB
testcase_17 AC 316 ms
99,352 KB
testcase_18 AC 302 ms
96,676 KB
testcase_19 AC 355 ms
99,404 KB
testcase_20 AC 294 ms
99,924 KB
testcase_21 AC 346 ms
99,412 KB
testcase_22 AC 305 ms
99,156 KB
testcase_23 AC 335 ms
99,396 KB
testcase_24 AC 341 ms
99,648 KB
testcase_25 AC 63 ms
69,732 KB
testcase_26 AC 64 ms
69,504 KB
testcase_27 AC 63 ms
70,272 KB
testcase_28 AC 69 ms
69,632 KB
testcase_29 AC 65 ms
71,168 KB
testcase_30 AC 64 ms
70,656 KB
testcase_31 AC 65 ms
69,760 KB
testcase_32 AC 64 ms
69,632 KB
testcase_33 AC 64 ms
70,144 KB
testcase_34 AC 62 ms
68,864 KB
testcase_35 AC 63 ms
69,504 KB
testcase_36 AC 63 ms
69,760 KB
testcase_37 AC 63 ms
70,272 KB
testcase_38 AC 65 ms
70,400 KB
testcase_39 AC 66 ms
71,296 KB
testcase_40 AC 68 ms
71,040 KB
testcase_41 AC 67 ms
71,168 KB
testcase_42 AC 63 ms
69,632 KB
testcase_43 AC 64 ms
69,376 KB
testcase_44 AC 64 ms
70,272 KB
testcase_45 AC 305 ms
101,120 KB
testcase_46 AC 292 ms
98,316 KB
testcase_47 AC 298 ms
101,400 KB
testcase_48 AC 287 ms
101,264 KB
testcase_49 AC 280 ms
99,356 KB
testcase_50 AC 283 ms
101,452 KB
testcase_51 AC 309 ms
100,728 KB
testcase_52 AC 289 ms
99,436 KB
testcase_53 AC 300 ms
100,564 KB
testcase_54 AC 307 ms
101,032 KB
testcase_55 AC 312 ms
100,836 KB
testcase_56 AC 303 ms
100,852 KB
testcase_57 AC 286 ms
98,568 KB
testcase_58 AC 292 ms
101,052 KB
testcase_59 AC 296 ms
101,108 KB
testcase_60 AC 299 ms
101,308 KB
testcase_61 AC 292 ms
101,232 KB
testcase_62 AC 278 ms
98,016 KB
testcase_63 AC 318 ms
101,304 KB
testcase_64 AC 307 ms
101,228 KB
testcase_65 AC 63 ms
79,076 KB
testcase_66 AC 64 ms
79,104 KB
testcase_67 AC 265 ms
103,332 KB
testcase_68 AC 261 ms
106,280 KB
testcase_69 AC 319 ms
106,192 KB
testcase_70 AC 283 ms
106,936 KB
testcase_71 AC 187 ms
88,308 KB
testcase_72 AC 181 ms
88,960 KB
testcase_73 AC 172 ms
90,352 KB
testcase_74 AC 189 ms
90,572 KB
testcase_75 AC 300 ms
103,100 KB
testcase_76 AC 279 ms
106,640 KB
testcase_77 AC 311 ms
103,436 KB
testcase_78 AC 335 ms
106,684 KB
testcase_79 AC 329 ms
106,892 KB
testcase_80 AC 284 ms
107,112 KB
testcase_81 AC 302 ms
106,752 KB
testcase_82 AC 328 ms
106,568 KB
testcase_83 AC 240 ms
89,552 KB
testcase_84 AC 237 ms
89,472 KB
testcase_85 AC 393 ms
107,276 KB
testcase_86 AC 358 ms
103,304 KB
testcase_87 AC 219 ms
106,156 KB
testcase_88 AC 206 ms
102,788 KB
testcase_89 AC 42 ms
54,348 KB
testcase_90 AC 41 ms
52,608 KB
testcase_91 AC 42 ms
52,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect
import math


class FenwickTree:
    def __init__(self, n):
        # 0-indexed
        self.N = n
        self.bit = [0] * n
        self.M = 1 << 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 = C[i]
        ft_cnt.add(c, 1)
        ft_sum.add(c, A[i])

        if i >= k:
            r = C[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