結果
問題 | No.738 平らな農地 |
ユーザー | しらっ亭 |
提出日時 | 2018-09-28 20:12:23 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 358 ms / 2,000 ms |
コード長 | 2,304 bytes |
コンパイル時間 | 154 ms |
コンパイル使用メモリ | 82,404 KB |
実行使用メモリ | 106,976 KB |
最終ジャッジ日時 | 2024-10-12 05:30:59 |
合計ジャッジ時間 | 19,106 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 37 ms
53,032 KB |
testcase_01 | AC | 41 ms
53,032 KB |
testcase_02 | AC | 39 ms
53,528 KB |
testcase_03 | AC | 36 ms
53,788 KB |
testcase_04 | AC | 37 ms
54,004 KB |
testcase_05 | AC | 92 ms
77,692 KB |
testcase_06 | AC | 97 ms
77,652 KB |
testcase_07 | AC | 97 ms
77,468 KB |
testcase_08 | AC | 84 ms
76,888 KB |
testcase_09 | AC | 55 ms
68,344 KB |
testcase_10 | AC | 48 ms
63,128 KB |
testcase_11 | AC | 81 ms
77,224 KB |
testcase_12 | AC | 57 ms
70,388 KB |
testcase_13 | AC | 94 ms
77,660 KB |
testcase_14 | AC | 59 ms
71,556 KB |
testcase_15 | AC | 273 ms
96,964 KB |
testcase_16 | AC | 274 ms
99,244 KB |
testcase_17 | AC | 296 ms
99,744 KB |
testcase_18 | AC | 287 ms
97,056 KB |
testcase_19 | AC | 329 ms
99,604 KB |
testcase_20 | AC | 274 ms
99,440 KB |
testcase_21 | AC | 323 ms
99,712 KB |
testcase_22 | AC | 283 ms
99,892 KB |
testcase_23 | AC | 313 ms
99,376 KB |
testcase_24 | AC | 306 ms
99,680 KB |
testcase_25 | AC | 57 ms
70,824 KB |
testcase_26 | AC | 57 ms
70,864 KB |
testcase_27 | AC | 58 ms
71,316 KB |
testcase_28 | AC | 58 ms
70,616 KB |
testcase_29 | AC | 61 ms
71,088 KB |
testcase_30 | AC | 59 ms
70,312 KB |
testcase_31 | AC | 58 ms
72,144 KB |
testcase_32 | AC | 57 ms
71,360 KB |
testcase_33 | AC | 59 ms
71,908 KB |
testcase_34 | AC | 57 ms
70,268 KB |
testcase_35 | AC | 57 ms
69,612 KB |
testcase_36 | AC | 57 ms
70,052 KB |
testcase_37 | AC | 56 ms
70,428 KB |
testcase_38 | AC | 57 ms
70,816 KB |
testcase_39 | AC | 60 ms
72,684 KB |
testcase_40 | AC | 59 ms
72,636 KB |
testcase_41 | AC | 60 ms
71,924 KB |
testcase_42 | AC | 56 ms
70,380 KB |
testcase_43 | AC | 57 ms
69,808 KB |
testcase_44 | AC | 58 ms
71,072 KB |
testcase_45 | AC | 272 ms
101,808 KB |
testcase_46 | AC | 264 ms
98,000 KB |
testcase_47 | AC | 271 ms
101,460 KB |
testcase_48 | AC | 252 ms
100,720 KB |
testcase_49 | AC | 243 ms
99,864 KB |
testcase_50 | AC | 244 ms
100,832 KB |
testcase_51 | AC | 289 ms
101,372 KB |
testcase_52 | AC | 257 ms
100,080 KB |
testcase_53 | AC | 266 ms
101,160 KB |
testcase_54 | AC | 276 ms
101,296 KB |
testcase_55 | AC | 276 ms
101,388 KB |
testcase_56 | AC | 282 ms
101,048 KB |
testcase_57 | AC | 266 ms
98,172 KB |
testcase_58 | AC | 275 ms
100,816 KB |
testcase_59 | AC | 277 ms
101,716 KB |
testcase_60 | AC | 278 ms
101,248 KB |
testcase_61 | AC | 272 ms
101,188 KB |
testcase_62 | AC | 264 ms
98,036 KB |
testcase_63 | AC | 291 ms
101,496 KB |
testcase_64 | AC | 286 ms
101,456 KB |
testcase_65 | AC | 60 ms
80,400 KB |
testcase_66 | AC | 62 ms
80,696 KB |
testcase_67 | AC | 254 ms
103,500 KB |
testcase_68 | AC | 253 ms
106,260 KB |
testcase_69 | AC | 298 ms
106,264 KB |
testcase_70 | AC | 269 ms
106,596 KB |
testcase_71 | AC | 172 ms
88,384 KB |
testcase_72 | AC | 164 ms
89,320 KB |
testcase_73 | AC | 160 ms
90,592 KB |
testcase_74 | AC | 171 ms
90,708 KB |
testcase_75 | AC | 287 ms
103,228 KB |
testcase_76 | AC | 264 ms
106,752 KB |
testcase_77 | AC | 297 ms
103,312 KB |
testcase_78 | AC | 306 ms
106,824 KB |
testcase_79 | AC | 302 ms
106,976 KB |
testcase_80 | AC | 266 ms
106,880 KB |
testcase_81 | AC | 279 ms
106,792 KB |
testcase_82 | AC | 306 ms
106,580 KB |
testcase_83 | AC | 218 ms
89,956 KB |
testcase_84 | AC | 220 ms
89,688 KB |
testcase_85 | AC | 358 ms
106,784 KB |
testcase_86 | AC | 337 ms
103,320 KB |
testcase_87 | AC | 200 ms
106,516 KB |
testcase_88 | AC | 191 ms
102,852 KB |
testcase_89 | AC | 38 ms
54,152 KB |
testcase_90 | AC | 38 ms
53,468 KB |
testcase_91 | AC | 38 ms
54,324 KB |
ソースコード
import bisect import math class FenwickTree: def __init__(self, n): # 0-indexed self.N = n self.bit = [0] * n self.M = 1 << int(math.ceil(math.log2(self.N))) 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 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 >>= 1 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) C = [compress(a) for a in A] m = len(compressor) counter = FenwickTree(m) for i in range(k): counter.add(C[i], 1) k2 = k // 2 + 1 medians = [] medians.append(counter.count_index(k2)) for i in range(k, n): counter.add(C[i], 1) counter.add(C[i-k], -1) medians.append(counter.count_index(k2)) ans = 1e18 ft_cnt = FenwickTree(m) ft_sum = FenwickTree(m) for i in range(n): c = C[i] ft_cnt.add(c, 1) ft_sum.add(c, A[i]) if i >= k: r = C[i - k] ft_cnt.add(r, -1) ft_sum.add(r, -A[i - k]) if i >= k - 1: median_c = medians[i - k + 1] median = compressor[median_c] 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())