結果

問題 No.738 平らな農地
ユーザー しらっ亭しらっ亭
提出日時 2018-09-28 19:30:42
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,546 bytes
コンパイル時間 263 ms
コンパイル使用メモリ 82,316 KB
実行使用メモリ 112,044 KB
最終ジャッジ日時 2024-04-20 09:59:53
合計ジャッジ時間 21,734 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
54,008 KB
testcase_01 AC 37 ms
53,268 KB
testcase_02 AC 36 ms
53,000 KB
testcase_03 AC 35 ms
53,992 KB
testcase_04 AC 37 ms
53,544 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 335 ms
99,152 KB
testcase_16 AC 337 ms
102,004 KB
testcase_17 AC 351 ms
101,612 KB
testcase_18 AC 325 ms
99,436 KB
testcase_19 AC 373 ms
101,416 KB
testcase_20 AC 324 ms
101,988 KB
testcase_21 AC 384 ms
101,352 KB
testcase_22 AC 340 ms
101,820 KB
testcase_23 AC 360 ms
101,636 KB
testcase_24 AC 373 ms
101,556 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
testcase_63 WA -
testcase_64 WA -
testcase_65 AC 58 ms
80,260 KB
testcase_66 AC 55 ms
79,852 KB
testcase_67 WA -
testcase_68 WA -
testcase_69 WA -
testcase_70 WA -
testcase_71 AC 148 ms
94,624 KB
testcase_72 WA -
testcase_73 WA -
testcase_74 WA -
testcase_75 WA -
testcase_76 WA -
testcase_77 WA -
testcase_78 WA -
testcase_79 WA -
testcase_80 WA -
testcase_81 WA -
testcase_82 WA -
testcase_83 AC 198 ms
90,280 KB
testcase_84 WA -
testcase_85 AC 383 ms
111,568 KB
testcase_86 AC 358 ms
108,464 KB
testcase_87 WA -
testcase_88 WA -
testcase_89 AC 35 ms
54,520 KB
testcase_90 WA -
testcase_91 AC 34 ms
53,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect
import math


class FenwickTree:
    def __init__(self, a_list):
        # 0-indexed
        self.N = len(a_list)
        self.M = 1 << int(math.ceil(math.log(self.N, 2)))
        self.bit = a_list[:]
        for _ in range(self.N, self.M):
            self.bit.append(0)
        for i in range(self.N-1):
            self.bit[i | (i+1)] += self.bit[i]

    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 index(self, k):
        return self.sum(k + 1) - self.sum(k)

    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 //= 2
        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)

    m = len(compressor)

    counter = FenwickTree([0] * m)

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

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

    ans = 1e18

    ft_cnt = FenwickTree([0] * m)
    ft_sum = FenwickTree([0] * 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 = medians[i - k + 1]
            median_c = compress(median)

            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