結果

問題 No.738 平らな農地
ユーザー mkawa2mkawa2
提出日時 2022-01-31 18:40:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 974 ms / 2,000 ms
コード長 2,434 bytes
コンパイル時間 352 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 100,136 KB
最終ジャッジ日時 2024-06-11 09:03:07
合計ジャッジ時間 37,015 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
53,248 KB
testcase_01 AC 44 ms
52,992 KB
testcase_02 AC 42 ms
52,992 KB
testcase_03 AC 43 ms
52,992 KB
testcase_04 AC 43 ms
53,248 KB
testcase_05 AC 148 ms
77,440 KB
testcase_06 AC 161 ms
77,696 KB
testcase_07 AC 117 ms
77,568 KB
testcase_08 AC 89 ms
73,088 KB
testcase_09 AC 68 ms
64,512 KB
testcase_10 AC 48 ms
53,760 KB
testcase_11 AC 80 ms
70,016 KB
testcase_12 AC 60 ms
62,080 KB
testcase_13 AC 145 ms
77,824 KB
testcase_14 AC 78 ms
68,992 KB
testcase_15 AC 747 ms
97,064 KB
testcase_16 AC 754 ms
96,956 KB
testcase_17 AC 757 ms
98,096 KB
testcase_18 AC 775 ms
97,524 KB
testcase_19 AC 935 ms
98,680 KB
testcase_20 AC 738 ms
97,012 KB
testcase_21 AC 838 ms
97,224 KB
testcase_22 AC 783 ms
96,448 KB
testcase_23 AC 825 ms
96,532 KB
testcase_24 AC 882 ms
98,048 KB
testcase_25 AC 70 ms
64,768 KB
testcase_26 AC 66 ms
64,384 KB
testcase_27 AC 71 ms
65,792 KB
testcase_28 AC 67 ms
64,512 KB
testcase_29 AC 66 ms
64,384 KB
testcase_30 AC 67 ms
64,384 KB
testcase_31 AC 66 ms
64,128 KB
testcase_32 AC 63 ms
64,256 KB
testcase_33 AC 69 ms
65,920 KB
testcase_34 AC 63 ms
63,872 KB
testcase_35 AC 63 ms
63,232 KB
testcase_36 AC 65 ms
63,232 KB
testcase_37 AC 63 ms
63,616 KB
testcase_38 AC 68 ms
65,152 KB
testcase_39 AC 72 ms
67,072 KB
testcase_40 AC 79 ms
68,480 KB
testcase_41 AC 71 ms
67,072 KB
testcase_42 AC 67 ms
64,896 KB
testcase_43 AC 64 ms
63,872 KB
testcase_44 AC 66 ms
64,000 KB
testcase_45 AC 753 ms
98,368 KB
testcase_46 AC 730 ms
96,076 KB
testcase_47 AC 753 ms
98,520 KB
testcase_48 AC 739 ms
98,304 KB
testcase_49 AC 707 ms
98,712 KB
testcase_50 AC 668 ms
97,120 KB
testcase_51 AC 794 ms
99,288 KB
testcase_52 AC 752 ms
98,540 KB
testcase_53 AC 767 ms
98,808 KB
testcase_54 AC 759 ms
98,700 KB
testcase_55 AC 768 ms
98,876 KB
testcase_56 AC 754 ms
98,804 KB
testcase_57 AC 757 ms
99,528 KB
testcase_58 AC 698 ms
98,888 KB
testcase_59 AC 710 ms
98,436 KB
testcase_60 AC 699 ms
98,060 KB
testcase_61 AC 731 ms
98,520 KB
testcase_62 AC 974 ms
100,136 KB
testcase_63 AC 787 ms
99,480 KB
testcase_64 AC 749 ms
99,824 KB
testcase_65 AC 321 ms
89,216 KB
testcase_66 AC 325 ms
88,704 KB
testcase_67 AC 223 ms
89,344 KB
testcase_68 AC 227 ms
91,008 KB
testcase_69 AC 290 ms
89,856 KB
testcase_70 AC 299 ms
90,624 KB
testcase_71 AC 149 ms
88,576 KB
testcase_72 AC 145 ms
88,704 KB
testcase_73 AC 148 ms
90,880 KB
testcase_74 AC 150 ms
89,728 KB
testcase_75 AC 537 ms
90,924 KB
testcase_76 AC 432 ms
91,384 KB
testcase_77 AC 562 ms
90,208 KB
testcase_78 AC 567 ms
92,072 KB
testcase_79 AC 305 ms
90,404 KB
testcase_80 AC 300 ms
91,704 KB
testcase_81 AC 232 ms
89,728 KB
testcase_82 AC 163 ms
89,984 KB
testcase_83 AC 225 ms
89,600 KB
testcase_84 AC 263 ms
89,856 KB
testcase_85 AC 649 ms
89,088 KB
testcase_86 AC 572 ms
87,168 KB
testcase_87 AC 140 ms
92,876 KB
testcase_88 AC 134 ms
91,108 KB
testcase_89 AC 44 ms
52,736 KB
testcase_90 AC 43 ms
52,992 KB
testcase_91 AC 44 ms
52,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
# inf = (1<<63)-1
inf = (1 << 32)-1
# md = 10**9+7
md = 998244353

from heapq import *

class RemovableHeap:
    def __init__(self):
        self.hp = []
        self.rem = []
        self.cnt = {}
        self.size = 0
        self.s = 0

    def __len__(self): return self.size

    def __getitem__(self, i):
        assert i == 0
        self.__trim()
        return self.hp[0]

    def push(self, val):
        heappush(self.hp, val)
        self.s += val
        if val in self.cnt: self.cnt[val] += 1
        else: self.cnt[val] = 1
        self.size += 1
        return True

    def pop(self):
        self.__trim()
        assert self.hp
        self.size -= 1
        val = heappop(self.hp)
        self.s -= val
        if self.cnt[val] == 1: del self.cnt[val]
        else: self.cnt[val] -= 1
        return val

    def remove(self, val):
        if val not in self.cnt: return False
        self.size -= 1
        self.s -= val
        if self.cnt[val] == 1: del self.cnt[val]
        else: self.cnt[val] -= 1
        heappush(self.rem, val)
        return True

    def __trim(self):
        while self.rem and self.rem[0] == self.hp[0]:
            heappop(self.hp)
            heappop(self.rem)

def cost():
    mid = rr[0]
    return mid*(k//2)+ll.s+rr.s-mid*(k-k//2)

n, k = LI()
aa = LI()

ll = RemovableHeap()
rr = RemovableHeap()

pre = sorted(aa[:k])
for a in pre[:k//2]:
    ll.push(-a)
for a in pre[k//2:]:
    rr.push(a)

ans = cost()
for i in range(k, n):
    a = aa[i]
    if a < rr[0]:
        ll.push(-a)
    else:
        rr.push(a)

    a = aa[i-k]
    if a < rr[0]:
        ll.remove(-a)
    else:
        rr.remove(a)

    if len(ll) < k//2: ll.push(-rr.pop())
    if len(ll) > k//2: rr.push(-ll.pop())

    ans = min(ans, cost())

print(ans)
0