結果
問題 | No.738 平らな農地 |
ユーザー | しらっ亭 |
提出日時 | 2018-09-28 20:07:53 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,331 bytes |
コンパイル時間 | 835 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 106,880 KB |
最終ジャッジ日時 | 2024-10-12 05:28:41 |
合計ジャッジ時間 | 25,370 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 37 ms
53,248 KB |
testcase_01 | AC | 37 ms
53,504 KB |
testcase_02 | AC | 38 ms
52,736 KB |
testcase_03 | AC | 38 ms
53,504 KB |
testcase_04 | AC | 36 ms
53,248 KB |
testcase_05 | AC | 142 ms
78,608 KB |
testcase_06 | AC | 155 ms
79,776 KB |
testcase_07 | AC | 140 ms
78,416 KB |
testcase_08 | AC | 106 ms
77,568 KB |
testcase_09 | AC | 62 ms
69,888 KB |
testcase_10 | AC | 55 ms
65,152 KB |
testcase_11 | AC | 97 ms
77,056 KB |
testcase_12 | AC | 63 ms
71,040 KB |
testcase_13 | AC | 120 ms
77,444 KB |
testcase_14 | AC | 69 ms
76,160 KB |
testcase_15 | AC | 395 ms
97,176 KB |
testcase_16 | AC | 399 ms
99,308 KB |
testcase_17 | AC | 426 ms
99,072 KB |
testcase_18 | AC | 413 ms
96,896 KB |
testcase_19 | AC | 466 ms
99,712 KB |
testcase_20 | AC | 397 ms
99,712 KB |
testcase_21 | AC | 450 ms
99,456 KB |
testcase_22 | AC | 414 ms
99,456 KB |
testcase_23 | AC | 434 ms
99,200 KB |
testcase_24 | AC | 445 ms
99,320 KB |
testcase_25 | AC | 64 ms
73,600 KB |
testcase_26 | AC | 64 ms
73,472 KB |
testcase_27 | AC | 67 ms
74,112 KB |
testcase_28 | AC | 67 ms
73,728 KB |
testcase_29 | AC | 66 ms
73,856 KB |
testcase_30 | AC | 66 ms
73,472 KB |
testcase_31 | AC | 65 ms
73,216 KB |
testcase_32 | AC | 64 ms
72,704 KB |
testcase_33 | AC | 66 ms
74,240 KB |
testcase_34 | AC | 67 ms
72,576 KB |
testcase_35 | AC | 67 ms
72,704 KB |
testcase_36 | AC | 64 ms
71,936 KB |
testcase_37 | AC | 64 ms
72,576 KB |
testcase_38 | AC | 65 ms
73,984 KB |
testcase_39 | AC | 66 ms
74,368 KB |
testcase_40 | AC | 68 ms
74,240 KB |
testcase_41 | AC | 72 ms
76,024 KB |
testcase_42 | AC | 65 ms
72,576 KB |
testcase_43 | AC | 65 ms
72,320 KB |
testcase_44 | AC | 65 ms
73,216 KB |
testcase_45 | AC | 399 ms
101,376 KB |
testcase_46 | AC | 401 ms
97,792 KB |
testcase_47 | AC | 399 ms
101,228 KB |
testcase_48 | AC | 370 ms
100,480 KB |
testcase_49 | AC | 373 ms
99,584 KB |
testcase_50 | AC | 390 ms
100,864 KB |
testcase_51 | AC | 409 ms
100,736 KB |
testcase_52 | AC | 386 ms
99,712 KB |
testcase_53 | AC | 388 ms
100,608 KB |
testcase_54 | AC | 400 ms
100,864 KB |
testcase_55 | AC | 417 ms
100,736 KB |
testcase_56 | AC | 401 ms
100,736 KB |
testcase_57 | AC | 399 ms
98,176 KB |
testcase_58 | AC | 392 ms
100,608 KB |
testcase_59 | AC | 391 ms
101,236 KB |
testcase_60 | AC | 389 ms
101,376 KB |
testcase_61 | AC | 385 ms
100,864 KB |
testcase_62 | AC | 379 ms
98,304 KB |
testcase_63 | AC | 407 ms
101,372 KB |
testcase_64 | AC | 418 ms
101,064 KB |
testcase_65 | AC | 60 ms
78,848 KB |
testcase_66 | AC | 61 ms
79,104 KB |
testcase_67 | AC | 335 ms
103,168 KB |
testcase_68 | AC | 301 ms
106,496 KB |
testcase_69 | AC | 401 ms
106,112 KB |
testcase_70 | AC | 363 ms
106,112 KB |
testcase_71 | AC | 213 ms
88,192 KB |
testcase_72 | AC | 210 ms
89,216 KB |
testcase_73 | AC | 196 ms
90,436 KB |
testcase_74 | AC | 214 ms
90,240 KB |
testcase_75 | WA | - |
testcase_76 | WA | - |
testcase_77 | AC | 414 ms
103,384 KB |
testcase_78 | AC | 444 ms
106,816 KB |
testcase_79 | WA | - |
testcase_80 | WA | - |
testcase_81 | WA | - |
testcase_82 | WA | - |
testcase_83 | AC | 258 ms
89,692 KB |
testcase_84 | AC | 249 ms
89,728 KB |
testcase_85 | AC | 485 ms
106,720 KB |
testcase_86 | AC | 454 ms
102,912 KB |
testcase_87 | WA | - |
testcase_88 | WA | - |
testcase_89 | AC | 38 ms
52,992 KB |
testcase_90 | AC | 38 ms
53,376 KB |
testcase_91 | AC | 37 ms
52,736 KB |
ソースコード
import bisect import math class FenwickTree: def __init__(self, n): # 0-indexed self.N = n self.bit = [0] * n self.M = 1 << (31 - 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 = compress(A[i]) ft_cnt.add(c, 1) ft_sum.add(c, A[i]) if i >= k: r = compress(A[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())