結果

問題 No.738 平らな農地
ユーザー shotoyooshotoyoo
提出日時 2021-06-16 23:14:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 395 ms / 2,000 ms
コード長 2,847 bytes
コンパイル時間 346 ms
コンパイル使用メモリ 87,300 KB
実行使用メモリ 108,924 KB
最終ジャッジ日時 2023-08-30 04:38:34
合計ジャッジ時間 28,267 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,372 KB
testcase_01 AC 73 ms
71,556 KB
testcase_02 AC 74 ms
71,260 KB
testcase_03 AC 74 ms
71,396 KB
testcase_04 AC 74 ms
71,476 KB
testcase_05 AC 129 ms
78,564 KB
testcase_06 AC 130 ms
78,536 KB
testcase_07 AC 136 ms
78,768 KB
testcase_08 AC 109 ms
77,240 KB
testcase_09 AC 95 ms
76,180 KB
testcase_10 AC 89 ms
76,032 KB
testcase_11 AC 108 ms
77,468 KB
testcase_12 AC 95 ms
75,952 KB
testcase_13 AC 128 ms
78,596 KB
testcase_14 AC 99 ms
76,152 KB
testcase_15 AC 315 ms
99,536 KB
testcase_16 AC 350 ms
102,288 KB
testcase_17 AC 387 ms
102,320 KB
testcase_18 AC 321 ms
99,652 KB
testcase_19 AC 385 ms
102,548 KB
testcase_20 AC 336 ms
102,092 KB
testcase_21 AC 375 ms
102,644 KB
testcase_22 AC 338 ms
102,108 KB
testcase_23 AC 374 ms
102,380 KB
testcase_24 AC 391 ms
102,156 KB
testcase_25 AC 98 ms
76,128 KB
testcase_26 AC 97 ms
76,392 KB
testcase_27 AC 98 ms
76,128 KB
testcase_28 AC 98 ms
76,140 KB
testcase_29 AC 101 ms
76,136 KB
testcase_30 AC 97 ms
76,124 KB
testcase_31 AC 99 ms
76,416 KB
testcase_32 AC 95 ms
76,120 KB
testcase_33 AC 98 ms
76,040 KB
testcase_34 AC 96 ms
76,128 KB
testcase_35 AC 95 ms
76,164 KB
testcase_36 AC 96 ms
75,948 KB
testcase_37 AC 96 ms
76,088 KB
testcase_38 AC 97 ms
76,124 KB
testcase_39 AC 102 ms
76,756 KB
testcase_40 AC 96 ms
76,116 KB
testcase_41 AC 98 ms
76,124 KB
testcase_42 AC 98 ms
76,076 KB
testcase_43 AC 97 ms
76,132 KB
testcase_44 AC 99 ms
76,120 KB
testcase_45 AC 343 ms
104,260 KB
testcase_46 AC 343 ms
101,108 KB
testcase_47 AC 336 ms
103,980 KB
testcase_48 AC 294 ms
103,436 KB
testcase_49 AC 319 ms
102,440 KB
testcase_50 AC 323 ms
103,528 KB
testcase_51 AC 321 ms
103,632 KB
testcase_52 AC 354 ms
102,800 KB
testcase_53 AC 313 ms
103,676 KB
testcase_54 AC 340 ms
103,840 KB
testcase_55 AC 318 ms
103,652 KB
testcase_56 AC 364 ms
103,768 KB
testcase_57 AC 308 ms
100,936 KB
testcase_58 AC 328 ms
103,576 KB
testcase_59 AC 312 ms
103,984 KB
testcase_60 AC 340 ms
104,516 KB
testcase_61 AC 326 ms
104,248 KB
testcase_62 AC 293 ms
100,752 KB
testcase_63 AC 325 ms
104,200 KB
testcase_64 AC 323 ms
104,144 KB
testcase_65 AC 355 ms
102,024 KB
testcase_66 AC 366 ms
102,324 KB
testcase_67 AC 308 ms
104,724 KB
testcase_68 AC 304 ms
108,096 KB
testcase_69 AC 359 ms
108,096 KB
testcase_70 AC 334 ms
108,016 KB
testcase_71 AC 207 ms
91,172 KB
testcase_72 AC 198 ms
91,584 KB
testcase_73 AC 194 ms
93,148 KB
testcase_74 AC 209 ms
93,164 KB
testcase_75 AC 328 ms
105,460 KB
testcase_76 AC 306 ms
108,224 KB
testcase_77 AC 338 ms
105,396 KB
testcase_78 AC 355 ms
108,816 KB
testcase_79 AC 382 ms
108,400 KB
testcase_80 AC 325 ms
107,936 KB
testcase_81 AC 365 ms
108,068 KB
testcase_82 AC 358 ms
107,888 KB
testcase_83 AC 281 ms
92,532 KB
testcase_84 AC 254 ms
92,544 KB
testcase_85 AC 395 ms
108,924 KB
testcase_86 AC 365 ms
105,052 KB
testcase_87 AC 233 ms
108,192 KB
testcase_88 AC 220 ms
104,648 KB
testcase_89 AC 74 ms
71,392 KB
testcase_90 AC 73 ms
71,296 KB
testcase_91 AC 74 ms
71,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda : sys.stdin.readline().rstrip()

sys.setrecursionlimit(2*10**5+10)
write = lambda x: sys.stdout.write(x+"\n")
debug = lambda x: sys.stderr.write(x+"\n")
writef = lambda x: print("{:.12f}".format(x))


### 座標圧縮 (一次元)
from bisect import bisect_left
def press(l):
    """xs: ソートした座標値
    inds: lの各要素のxsにおけるインデックス
    """
    # xs[inds[i]]==l[i]となる
    xs = sorted(set(l))
    inds = [None] * len(l)
#     d = {}
    for i,item in enumerate(l):
        inds[i] = bisect_left(xs, item)
#         d[item] = inds[i]
    return xs, inds
class BIT:
    ### BIT binary
    def __init__(self, n, values=None):
        self.bit = [0]*(n+1)
        self.n = n
        self.total = 0
        if values is not None:
            for i,v in enumerate(values):
                self.add(i,v)
                self.total += v
    def check(self):
        l = []
        prv = 0
        for i in range(1,self.n+1):
            val = self.query(i)
            l.append(val-prv)
            prv = val
        print(" ".join(map(str, l)))
    #a1 ~ aiまでの和 O(logn)
    def query(self,i):
        res = 0
        while i > 0:
            res += self.bit[i]
            # res %= M
            i -= i&(-i)
        return res
    def get(self,i):
        return self.query(i+1) - self.query(i)
    #ai += x(logN)
    def add(self,i,x):
        i += 1
        if i==0:
            raise RuntimeError
        self.total += x
        while i <= self.n:
            self.bit[i] += x
            # self.bit[i] %= M
            i += i&(-i)
    def index(self, v):
        """a0,...,aiの和がv以上になる最小のindexを求める
        存在しないとき配列サイズを返す
        """
        if v <= 0:
            return 0
        if self.total<v:
            return self.n
        x = 0
        r = 1
        while r < self.n:
            r = r << 1;
        ll = r
        while ll>0:
            if x+ll<self.n and self.bit[x+ll]<v:
                v -= self.bit[x+ll]
                x += ll
            ll = ll>>1
        return x

n,k = list(map(int, input().split()))
a = list(map(int, input().split()))
xs, inds = press(a)
m = len(xs)
bit = BIT(m)
bit2 = BIT(m)
total = 0
for i in range(k):
    bit.add(inds[i], 1)
    bit2.add(inds[i], xs[inds[i]])
    total += a[i]
ans = float("inf")
for i in range(k,n+1):
    v = bit.index((k+1)//2)
    left = bit2.query(v)
    lnum = bit.query(v)
    right = total - left - bit2.get(v)
    rnum = k - lnum - bit.get(v)
    val = (xs[v]*lnum - left) + (right - xs[v]*rnum)
    ans = min(ans, val)
#     bit2.check()
#     print(left, right)
    if i==n:
        break
    bit.add(inds[i], 1)
    bit2.add(inds[i], a[i])
    bit.add(inds[i-k], -1)
    bit2.add(inds[i-k], -a[i-k])
    total += a[i] - a[i-k]
print(ans)
0