結果

問題 No.738 平らな農地
ユーザー terasaterasa
提出日時 2023-01-09 13:20:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,288 ms / 2,000 ms
コード長 9,117 bytes
コンパイル時間 365 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 175,132 KB
最終ジャッジ日時 2024-05-09 22:47:46
合計ジャッジ時間 50,056 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
70,144 KB
testcase_01 AC 74 ms
70,016 KB
testcase_02 AC 74 ms
69,888 KB
testcase_03 AC 73 ms
69,760 KB
testcase_04 AC 71 ms
69,888 KB
testcase_05 AC 245 ms
81,804 KB
testcase_06 AC 246 ms
81,664 KB
testcase_07 AC 198 ms
80,768 KB
testcase_08 AC 181 ms
80,732 KB
testcase_09 AC 153 ms
79,872 KB
testcase_10 AC 124 ms
79,120 KB
testcase_11 AC 177 ms
80,308 KB
testcase_12 AC 161 ms
79,616 KB
testcase_13 AC 215 ms
81,816 KB
testcase_14 AC 163 ms
80,324 KB
testcase_15 AC 677 ms
158,976 KB
testcase_16 AC 733 ms
161,792 KB
testcase_17 AC 796 ms
160,256 KB
testcase_18 AC 772 ms
159,104 KB
testcase_19 AC 949 ms
168,448 KB
testcase_20 AC 705 ms
160,896 KB
testcase_21 AC 871 ms
168,448 KB
testcase_22 AC 748 ms
162,816 KB
testcase_23 AC 854 ms
164,864 KB
testcase_24 AC 864 ms
161,664 KB
testcase_25 AC 170 ms
79,872 KB
testcase_26 AC 165 ms
79,616 KB
testcase_27 AC 157 ms
80,256 KB
testcase_28 AC 152 ms
80,128 KB
testcase_29 AC 156 ms
80,384 KB
testcase_30 AC 156 ms
80,128 KB
testcase_31 AC 167 ms
79,824 KB
testcase_32 AC 167 ms
80,000 KB
testcase_33 AC 162 ms
79,872 KB
testcase_34 AC 165 ms
80,264 KB
testcase_35 AC 162 ms
80,128 KB
testcase_36 AC 155 ms
79,616 KB
testcase_37 AC 157 ms
80,128 KB
testcase_38 AC 174 ms
80,128 KB
testcase_39 AC 160 ms
80,128 KB
testcase_40 AC 164 ms
80,256 KB
testcase_41 AC 159 ms
80,008 KB
testcase_42 AC 151 ms
80,128 KB
testcase_43 AC 150 ms
80,076 KB
testcase_44 AC 155 ms
80,000 KB
testcase_45 AC 658 ms
168,576 KB
testcase_46 AC 673 ms
160,780 KB
testcase_47 AC 681 ms
167,040 KB
testcase_48 AC 575 ms
163,840 KB
testcase_49 AC 608 ms
162,816 KB
testcase_50 AC 648 ms
166,844 KB
testcase_51 AC 720 ms
167,340 KB
testcase_52 AC 668 ms
163,328 KB
testcase_53 AC 665 ms
171,008 KB
testcase_54 AC 722 ms
167,252 KB
testcase_55 AC 764 ms
167,296 KB
testcase_56 AC 757 ms
172,160 KB
testcase_57 AC 695 ms
158,208 KB
testcase_58 AC 720 ms
172,032 KB
testcase_59 AC 663 ms
169,344 KB
testcase_60 AC 668 ms
167,808 KB
testcase_61 AC 684 ms
172,416 KB
testcase_62 AC 647 ms
161,536 KB
testcase_63 AC 752 ms
168,960 KB
testcase_64 AC 746 ms
168,448 KB
testcase_65 AC 1,159 ms
160,512 KB
testcase_66 AC 1,216 ms
166,784 KB
testcase_67 AC 806 ms
164,960 KB
testcase_68 AC 740 ms
166,812 KB
testcase_69 AC 1,129 ms
168,528 KB
testcase_70 AC 888 ms
169,504 KB
testcase_71 AC 124 ms
90,496 KB
testcase_72 AC 134 ms
92,828 KB
testcase_73 AC 131 ms
94,104 KB
testcase_74 AC 133 ms
94,592 KB
testcase_75 AC 959 ms
163,904 KB
testcase_76 AC 850 ms
167,212 KB
testcase_77 AC 1,015 ms
161,928 KB
testcase_78 AC 1,065 ms
169,416 KB
testcase_79 AC 1,039 ms
169,488 KB
testcase_80 AC 849 ms
167,884 KB
testcase_81 AC 949 ms
164,936 KB
testcase_82 AC 1,012 ms
167,748 KB
testcase_83 AC 349 ms
103,544 KB
testcase_84 AC 381 ms
103,264 KB
testcase_85 AC 1,288 ms
175,132 KB
testcase_86 AC 1,198 ms
161,596 KB
testcase_87 AC 296 ms
166,608 KB
testcase_88 AC 278 ms
163,252 KB
testcase_89 AC 78 ms
69,504 KB
testcase_90 AC 74 ms
69,504 KB
testcase_91 AC 72 ms
69,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import List, Tuple, Callable, TypeVar, Optional
import sys
import itertools
import heapq
import bisect
import math
from collections import deque, defaultdict
from functools import lru_cache, cmp_to_key

input = sys.stdin.readline

if __file__ != 'prog.py':
    sys.setrecursionlimit(10 ** 6)


def readints(): return map(int, input().split())
def readlist(): return list(readints())
def readstr(): return input()[:-1]
def readlist1(): return list(map(lambda x: int(x) - 1, input().split()))


class BitVector:
    # reference: https://tiramister.net/blog/posts/bitvector/
    TABLE = bytes([
        0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
        1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
        1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
        2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
        1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
        2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
        2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
        3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
        1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
        2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
        2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
        3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
        2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
        3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
        3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
        4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
    ])

    def __init__(self, N):
        self.cnum = (N + 255) >> 8

        self.bit = bytearray(self.cnum << 5)
        self.chunk = [0] * (self.cnum + 1)
        self.blocks = bytearray(self.cnum << 5)

        self.built = False

    def set(self, pos):
        self.bit[pos >> 3] |= 1 << (pos & 7)

    def access(self, pos):
        return self.bit[pos >> 3] >> (pos & 7) & 1

    def popcount(self, num):
        return self.TABLE[num]

    def build(self):
        for i in range(self.cnum):
            k = i << 5
            for j in range(31):
                self.blocks[k + 1] = self.blocks[k] + self.popcount(self.bit[k])
                k += 1
            self.chunk[i + 1] = self.chunk[i] + self.blocks[k] + self.popcount(self.bit[k])
        self.built = True

    def rank(self, pos):
        assert self.built
        cpos, tmp = pos >> 8, pos & 255
        bpos, offset = tmp >> 3, tmp & 7

        i = cpos << 5 | bpos
        rest = self.bit[i] & ((1 << offset) - 1)
        return self.chunk[cpos] + self.blocks[i] + self.popcount(rest)

    def select(self, num):
        """return minimum i that satisfies rank(i) = num"""
        assert self.built
        if num == 0:
            return 0
        if self.rank(self.N) < num:
            return -1

        l = -1
        r = self.N
        while r - l > 1:
            c = (l + r) >> 1
            if self.rank(c) >= num:
                r = c
            else:
                l = c
        return r


class WaveletMatrix:
    # reference: https://miti-7.hatenablog.com/entry/2018/04/28/152259
    def __init__(self, A):
        self.nums = sorted(set(A))
        self.idx = {a: i for i, a in enumerate(self.nums)}
        self.A = [self.idx[a] for a in A]

        self.digit = (len(self.nums) - 1).bit_length()
        self.B = [None] * self.digit
        self.offset = [None] * self.digit
        self.start_index = [-1] * len(self.nums)

        self.S = [[0 for _ in range(len(self.A) + 1)] for _ in range(self.digit + 1)]
        for i, a in enumerate(self.A):
            self.S[self.digit][i + 1] = self.S[self.digit][i] + self.nums[a]

        T = self.A
        for k in range(self.digit)[::-1]:
            self.B[k] = BitVector(len(T) + 1)
            zeros = []
            ones = []
            for i, a in enumerate(T):
                if a >> k & 1:
                    self.B[k].set(i)
                    ones.append(a)
                else:
                    zeros.append(a)
            self.B[k].build()
            self.offset[k] = len(zeros)
            T = zeros + ones
            for i, a in enumerate(T):
                self.S[k][i + 1] = self.S[k][i] + self.nums[a]
        for i, a in enumerate(T):
            if self.start_index[a] < 0:
                self.start_index[a] = i

    def access(self, i):
        """return i-th value"""
        ret = 0
        cur = i
        for k in range(self.digit)[::-1]:
            if self.B[k].access(cur):
                ret |= (1 << k)
                cur = self.B[k].rank(cur) + self.offset[k]
            else:
                cur -= self.B[k].rank(cur)
        return self.nums[ret]

    def rank(self, i, x):
        """return the number of x's in [0, i) range"""
        x = self.idx.get(x)
        if x is None:
            return 0
        for k in range(self.digit)[::-1]:
            if x >> k & 1:
                i = self.B[k].rank(i) + self.offset[k]
            else:
                i -= self.B[k].rank(i)
        return i - self.start_index[x]

    def quantile(self, l, r, n):
        """return n-th (0-indexed) smallest value in [l, r) range"""
        assert 0 <= n < r - l
        ret = 0
        for k in range(self.digit)[::-1]:
            rank_l = self.B[k].rank(l)
            rank_r = self.B[k].rank(r)
            ones = rank_r - rank_l
            zeros = r - l - ones
            if zeros <= n:
                ret |= 1 << k
                l = rank_l + self.offset[k]
                r = rank_r + self.offset[k]
                n -= zeros
            else:
                l -= rank_l
                r -= rank_r
        return self.nums[ret]

    def range_freq(self, l, r, lower, upper):
        """return the number of values s.t. lower <= x < upper"""
        return self._range_freq_upper(l, r, upper) - self._range_freq_upper(l, r, lower)

    def prev_value(self, l, r, upper):
        """return maximum x s.t. x < upper in [l, r) range if exist, otherwise None"""
        cnt = self._range_freq_upper(l, r, upper)
        if cnt == 0:
            return None
        return self.quantile(l, r, cnt - 1)

    def next_value(self, l, r, lower):
        """return minimum x s.t. x >= lower in [l, r) range if exist, otherwise None"""
        cnt = self._range_freq_upper(l, r, lower)
        if cnt == r - l:
            return None
        return self.quantile(l, r, cnt)

    def range_sum(self, l, r, lower, upper):
        return self._range_sum_upper(l, r, upper) - self._range_sum_upper(l, r, lower)

    def range_sum_topn(self, l, r, n):
        assert 0 <= n < r - l
        if self.digit == 0:
            return self.nums[0] * (n + 1)
        ret = 0
        for k in range(self.digit)[::-1]:
            rank_l = self.B[k].rank(l)
            rank_r = self.B[k].rank(r)
            ones = rank_r - rank_l
            zeros = r - l - ones
            if zeros <= n:
                ret += self.S[k][r - rank_r] - self.S[k][l - rank_l]
                l = rank_l + self.offset[k]
                r = rank_r + self.offset[k]
                n -= zeros
            else:
                l -= rank_l
                r -= rank_r
        ret += self.S[0][l + n + 1] - self.S[0][l]
        return ret

    def _range_freq_upper(self, l, r, upper):
        """return the number of values s.t. x < upper in [l, r) range"""
        if l >= r:
            return 0
        if upper > self.nums[-1]:
            return r - l
        if upper <= self.nums[0]:
            return 0
        upper = bisect.bisect_left(self.nums, upper)
        ret = 0
        for k in range(self.digit)[::-1]:
            rank_l = self.B[k].rank(l)
            rank_r = self.B[k].rank(r)
            ones = rank_r - rank_l
            zeros = r - l - ones
            if upper >> k & 1:
                ret += zeros
                l = rank_l + self.offset[k]
                r = rank_r + self.offset[k]
            else:
                l -= rank_l
                r -= rank_r
        return ret

    def _range_sum_upper(self, l, r, upper):
        if l >= r:
            return 0
        if upper > self.nums[-1]:
            return self.S[self.digit][r] - self.S[self.digit][l]
        if upper <= self.nums[0]:
            return 0
        upper = bisect.bisect_left(self.nums, upper)
        ret = 0
        for k in range(self.digit)[::-1]:
            rank_l = self.B[k].rank(l)
            rank_r = self.B[k].rank(r)
            ones = rank_r - rank_l
            zero = r - l - ones
            if upper >> k & 1:
                ret += self.S[k][r - rank_r] - self.S[k][l - rank_l]
                l = rank_l + self.offset[k]
                r = rank_r + self.offset[k]
            else:
                l -= rank_l
                r -= rank_r
        return ret


N, K = readints()
A = readlist()
W = WaveletMatrix(A)
INF = 1 << 50
ans = INF
for i in range(N - K + 1):
    med = W.quantile(i, i + K, K // 2)
    upper_sum = W.range_sum(i, i + K, med, INF) - med * W.range_freq(i, i + K, med, INF)
    lower_sum = med * W.range_freq(i, i + K, 0, med) - W.range_sum(i, i + K, 0, med)
    ans = min(ans, upper_sum + lower_sum)
print(ans)
0