結果
問題 | No.738 平らな農地 |
ユーザー | しらっ亭 |
提出日時 | 2018-09-28 19:30:42 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,546 bytes |
コンパイル時間 | 2,337 ms |
コンパイル使用メモリ | 82,216 KB |
実行使用メモリ | 111,912 KB |
最終ジャッジ日時 | 2024-10-12 05:26:10 |
合計ジャッジ時間 | 25,909 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 38 ms
52,864 KB |
testcase_01 | AC | 38 ms
52,992 KB |
testcase_02 | AC | 39 ms
53,248 KB |
testcase_03 | AC | 38 ms
53,248 KB |
testcase_04 | AC | 39 ms
52,992 KB |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | AC | 379 ms
99,052 KB |
testcase_16 | AC | 388 ms
101,760 KB |
testcase_17 | AC | 425 ms
101,740 KB |
testcase_18 | AC | 399 ms
99,200 KB |
testcase_19 | AC | 448 ms
101,244 KB |
testcase_20 | AC | 387 ms
101,332 KB |
testcase_21 | AC | 450 ms
101,632 KB |
testcase_22 | AC | 395 ms
101,888 KB |
testcase_23 | AC | 429 ms
101,380 KB |
testcase_24 | AC | 433 ms
101,832 KB |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | WA | - |
testcase_35 | WA | - |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | WA | - |
testcase_39 | WA | - |
testcase_40 | WA | - |
testcase_41 | WA | - |
testcase_42 | WA | - |
testcase_43 | WA | - |
testcase_44 | WA | - |
testcase_45 | WA | - |
testcase_46 | WA | - |
testcase_47 | WA | - |
testcase_48 | WA | - |
testcase_49 | WA | - |
testcase_50 | WA | - |
testcase_51 | WA | - |
testcase_52 | WA | - |
testcase_53 | WA | - |
testcase_54 | WA | - |
testcase_55 | WA | - |
testcase_56 | WA | - |
testcase_57 | WA | - |
testcase_58 | WA | - |
testcase_59 | WA | - |
testcase_60 | WA | - |
testcase_61 | WA | - |
testcase_62 | WA | - |
testcase_63 | WA | - |
testcase_64 | WA | - |
testcase_65 | AC | 62 ms
79,488 KB |
testcase_66 | AC | 62 ms
79,744 KB |
testcase_67 | WA | - |
testcase_68 | WA | - |
testcase_69 | WA | - |
testcase_70 | WA | - |
testcase_71 | AC | 188 ms
93,696 KB |
testcase_72 | WA | - |
testcase_73 | WA | - |
testcase_74 | WA | - |
testcase_75 | WA | - |
testcase_76 | WA | - |
testcase_77 | WA | - |
testcase_78 | WA | - |
testcase_79 | WA | - |
testcase_80 | WA | - |
testcase_81 | WA | - |
testcase_82 | WA | - |
testcase_83 | AC | 252 ms
90,240 KB |
testcase_84 | WA | - |
testcase_85 | AC | 469 ms
110,976 KB |
testcase_86 | AC | 428 ms
108,684 KB |
testcase_87 | WA | - |
testcase_88 | WA | - |
testcase_89 | AC | 40 ms
52,864 KB |
testcase_90 | WA | - |
testcase_91 | AC | 40 ms
52,736 KB |
ソースコード
import bisect import math class FenwickTree: def __init__(self, a_list): # 0-indexed self.N = len(a_list) self.M = 1 << int(math.ceil(math.log(self.N, 2))) self.bit = a_list[:] for _ in range(self.N, self.M): self.bit.append(0) for i in range(self.N-1): self.bit[i | (i+1)] += self.bit[i] 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 index(self, k): return self.sum(k + 1) - self.sum(k) 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 //= 2 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) m = len(compressor) counter = FenwickTree([0] * m) for i in range(k): counter.add(A[i], 1) k2 = (k + 1) // 2 medians = [] medians.append(counter.count_index(k2)) for i in range(k, n): counter.add(A[i], 1) counter.add(A[i-k], -1) medians.append(counter.count_index(k2)) ans = 1e18 ft_cnt = FenwickTree([0] * m) ft_sum = FenwickTree([0] * 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 = medians[i - k + 1] median_c = compress(median) 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())