結果

問題 No.738 平らな農地
ユーザー mkawa2mkawa2
提出日時 2022-01-31 18:40:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 939 ms / 2,000 ms
コード長 2,434 bytes
コンパイル時間 473 ms
コンパイル使用メモリ 87,096 KB
実行使用メモリ 106,552 KB
最終ジャッジ日時 2023-09-02 02:23:38
合計ジャッジ時間 39,778 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,332 KB
testcase_01 AC 74 ms
71,144 KB
testcase_02 AC 72 ms
71,332 KB
testcase_03 AC 72 ms
71,456 KB
testcase_04 AC 73 ms
71,452 KB
testcase_05 AC 166 ms
78,416 KB
testcase_06 AC 175 ms
78,676 KB
testcase_07 AC 129 ms
78,608 KB
testcase_08 AC 106 ms
77,548 KB
testcase_09 AC 88 ms
76,272 KB
testcase_10 AC 75 ms
71,640 KB
testcase_11 AC 101 ms
77,572 KB
testcase_12 AC 88 ms
75,708 KB
testcase_13 AC 161 ms
78,896 KB
testcase_14 AC 98 ms
76,248 KB
testcase_15 AC 753 ms
101,000 KB
testcase_16 AC 765 ms
102,312 KB
testcase_17 AC 772 ms
101,856 KB
testcase_18 AC 783 ms
101,528 KB
testcase_19 AC 936 ms
103,220 KB
testcase_20 AC 743 ms
102,056 KB
testcase_21 AC 825 ms
102,460 KB
testcase_22 AC 772 ms
103,656 KB
testcase_23 AC 819 ms
102,236 KB
testcase_24 AC 840 ms
102,544 KB
testcase_25 AC 90 ms
75,932 KB
testcase_26 AC 87 ms
76,008 KB
testcase_27 AC 92 ms
76,232 KB
testcase_28 AC 89 ms
76,012 KB
testcase_29 AC 87 ms
76,020 KB
testcase_30 AC 88 ms
75,900 KB
testcase_31 AC 87 ms
75,824 KB
testcase_32 AC 87 ms
75,748 KB
testcase_33 AC 91 ms
76,028 KB
testcase_34 AC 88 ms
75,512 KB
testcase_35 AC 86 ms
75,760 KB
testcase_36 AC 87 ms
75,764 KB
testcase_37 AC 87 ms
75,876 KB
testcase_38 AC 89 ms
76,172 KB
testcase_39 AC 93 ms
76,228 KB
testcase_40 AC 95 ms
76,472 KB
testcase_41 AC 93 ms
76,296 KB
testcase_42 AC 88 ms
76,012 KB
testcase_43 AC 88 ms
75,988 KB
testcase_44 AC 90 ms
75,800 KB
testcase_45 AC 742 ms
103,676 KB
testcase_46 AC 731 ms
102,528 KB
testcase_47 AC 754 ms
105,744 KB
testcase_48 AC 714 ms
102,944 KB
testcase_49 AC 704 ms
104,936 KB
testcase_50 AC 683 ms
103,156 KB
testcase_51 AC 805 ms
103,800 KB
testcase_52 AC 747 ms
104,100 KB
testcase_53 AC 753 ms
103,080 KB
testcase_54 AC 753 ms
102,820 KB
testcase_55 AC 753 ms
103,124 KB
testcase_56 AC 760 ms
103,780 KB
testcase_57 AC 751 ms
104,220 KB
testcase_58 AC 708 ms
104,560 KB
testcase_59 AC 734 ms
102,236 KB
testcase_60 AC 695 ms
103,392 KB
testcase_61 AC 715 ms
105,104 KB
testcase_62 AC 939 ms
106,520 KB
testcase_63 AC 798 ms
106,552 KB
testcase_64 AC 757 ms
105,664 KB
testcase_65 AC 327 ms
89,980 KB
testcase_66 AC 341 ms
89,952 KB
testcase_67 AC 237 ms
91,384 KB
testcase_68 AC 232 ms
92,624 KB
testcase_69 AC 293 ms
92,068 KB
testcase_70 AC 299 ms
91,420 KB
testcase_71 AC 162 ms
89,996 KB
testcase_72 AC 156 ms
90,176 KB
testcase_73 AC 157 ms
91,680 KB
testcase_74 AC 159 ms
91,540 KB
testcase_75 AC 529 ms
94,136 KB
testcase_76 AC 435 ms
94,076 KB
testcase_77 AC 574 ms
95,280 KB
testcase_78 AC 574 ms
94,388 KB
testcase_79 AC 297 ms
92,448 KB
testcase_80 AC 289 ms
93,240 KB
testcase_81 AC 241 ms
90,516 KB
testcase_82 AC 169 ms
90,576 KB
testcase_83 AC 237 ms
90,644 KB
testcase_84 AC 270 ms
91,108 KB
testcase_85 AC 676 ms
90,268 KB
testcase_86 AC 590 ms
88,252 KB
testcase_87 AC 153 ms
96,568 KB
testcase_88 AC 146 ms
94,680 KB
testcase_89 AC 70 ms
71,304 KB
testcase_90 AC 70 ms
71,504 KB
testcase_91 AC 72 ms
71,336 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