結果
問題 | No.738 平らな農地 |
ユーザー | maspy |
提出日時 | 2020-03-27 08:57:24 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 339 ms / 2,000 ms |
コード長 | 2,163 bytes |
コンパイル時間 | 156 ms |
コンパイル使用メモリ | 82,016 KB |
実行使用メモリ | 126,532 KB |
最終ジャッジ日時 | 2024-06-10 16:03:43 |
合計ジャッジ時間 | 18,568 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 35 ms
52,800 KB |
testcase_01 | AC | 37 ms
52,276 KB |
testcase_02 | AC | 35 ms
53,148 KB |
testcase_03 | AC | 37 ms
53,660 KB |
testcase_04 | AC | 35 ms
54,088 KB |
testcase_05 | AC | 74 ms
76,684 KB |
testcase_06 | AC | 76 ms
76,900 KB |
testcase_07 | AC | 79 ms
76,768 KB |
testcase_08 | AC | 60 ms
70,740 KB |
testcase_09 | AC | 53 ms
65,084 KB |
testcase_10 | AC | 45 ms
61,716 KB |
testcase_11 | AC | 59 ms
69,376 KB |
testcase_12 | AC | 53 ms
66,288 KB |
testcase_13 | AC | 74 ms
76,696 KB |
testcase_14 | AC | 54 ms
67,188 KB |
testcase_15 | AC | 228 ms
109,540 KB |
testcase_16 | AC | 242 ms
112,804 KB |
testcase_17 | AC | 270 ms
112,468 KB |
testcase_18 | AC | 242 ms
108,976 KB |
testcase_19 | AC | 299 ms
113,232 KB |
testcase_20 | AC | 260 ms
112,864 KB |
testcase_21 | AC | 290 ms
113,400 KB |
testcase_22 | AC | 263 ms
112,796 KB |
testcase_23 | AC | 290 ms
113,052 KB |
testcase_24 | AC | 288 ms
112,652 KB |
testcase_25 | AC | 54 ms
66,800 KB |
testcase_26 | AC | 52 ms
67,960 KB |
testcase_27 | AC | 54 ms
66,776 KB |
testcase_28 | AC | 53 ms
66,772 KB |
testcase_29 | AC | 55 ms
66,968 KB |
testcase_30 | AC | 55 ms
66,232 KB |
testcase_31 | AC | 52 ms
65,396 KB |
testcase_32 | AC | 52 ms
66,540 KB |
testcase_33 | AC | 54 ms
66,476 KB |
testcase_34 | AC | 55 ms
66,036 KB |
testcase_35 | AC | 56 ms
66,348 KB |
testcase_36 | AC | 56 ms
65,348 KB |
testcase_37 | AC | 58 ms
67,608 KB |
testcase_38 | AC | 53 ms
67,400 KB |
testcase_39 | AC | 53 ms
66,556 KB |
testcase_40 | AC | 55 ms
67,508 KB |
testcase_41 | AC | 54 ms
67,064 KB |
testcase_42 | AC | 51 ms
65,952 KB |
testcase_43 | AC | 52 ms
65,848 KB |
testcase_44 | AC | 53 ms
66,332 KB |
testcase_45 | AC | 261 ms
116,440 KB |
testcase_46 | AC | 258 ms
111,432 KB |
testcase_47 | AC | 255 ms
116,068 KB |
testcase_48 | AC | 220 ms
115,584 KB |
testcase_49 | AC | 237 ms
113,360 KB |
testcase_50 | AC | 241 ms
116,132 KB |
testcase_51 | AC | 243 ms
115,760 KB |
testcase_52 | AC | 244 ms
113,352 KB |
testcase_53 | AC | 230 ms
115,624 KB |
testcase_54 | AC | 258 ms
115,876 KB |
testcase_55 | AC | 241 ms
116,120 KB |
testcase_56 | AC | 259 ms
115,888 KB |
testcase_57 | AC | 230 ms
111,288 KB |
testcase_58 | AC | 256 ms
116,016 KB |
testcase_59 | AC | 241 ms
117,032 KB |
testcase_60 | AC | 261 ms
116,356 KB |
testcase_61 | AC | 264 ms
116,160 KB |
testcase_62 | AC | 227 ms
111,148 KB |
testcase_63 | AC | 246 ms
116,672 KB |
testcase_64 | AC | 243 ms
116,100 KB |
testcase_65 | AC | 269 ms
113,404 KB |
testcase_66 | AC | 276 ms
113,188 KB |
testcase_67 | AC | 243 ms
120,372 KB |
testcase_68 | AC | 246 ms
125,380 KB |
testcase_69 | AC | 294 ms
125,668 KB |
testcase_70 | AC | 271 ms
125,948 KB |
testcase_71 | AC | 172 ms
91,832 KB |
testcase_72 | AC | 157 ms
91,608 KB |
testcase_73 | AC | 150 ms
93,344 KB |
testcase_74 | AC | 166 ms
93,236 KB |
testcase_75 | AC | 267 ms
120,488 KB |
testcase_76 | AC | 253 ms
125,912 KB |
testcase_77 | AC | 277 ms
120,480 KB |
testcase_78 | AC | 290 ms
125,904 KB |
testcase_79 | AC | 286 ms
125,544 KB |
testcase_80 | AC | 263 ms
125,804 KB |
testcase_81 | AC | 271 ms
125,152 KB |
testcase_82 | AC | 255 ms
125,316 KB |
testcase_83 | AC | 216 ms
93,092 KB |
testcase_84 | AC | 170 ms
93,288 KB |
testcase_85 | AC | 339 ms
126,532 KB |
testcase_86 | AC | 319 ms
120,756 KB |
testcase_87 | AC | 174 ms
126,060 KB |
testcase_88 | AC | 168 ms
120,780 KB |
testcase_89 | AC | 35 ms
53,364 KB |
testcase_90 | AC | 35 ms
52,624 KB |
testcase_91 | AC | 35 ms
52,572 KB |
ソースコード
#!/usr/bin/ python3.8 import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines class BinaryIndexedTree(): def __init__(self, seq): self.size = len(seq) self.depth = self.size.bit_length() self.build(seq) def build(self, seq): data = seq size = self.size for i, x in enumerate(data): j = i + (i & (-i)) if j < size: data[j] += data[i] self.data = data def __repr__(self): return self.data.__repr__() def get_sum(self, i): data = self.data s = 0 while i: s += data[i] i -= i & -i return s def add(self, i, x): data = self.data size = self.size while i < size: data[i] += x i += i & -i def find_kth_element(self, k): data = self.data size = self.size x, sx = 0, 0 dx = 1 << (self.depth) for i in range(self.depth - 1, -1, -1): dx = (1 << i) if x + dx >= size: continue y = x + dx sy = sx + data[y] if sy < k: x, sx = y, sy return x + 1 N, K, *A = map(int, read().split()) sortedA = sorted(set(A)) elem_rank = {x: i for i, x in enumerate(sortedA, 1)} bit_num = BinaryIndexedTree([0] * (N + 10)) bit_sum = BinaryIndexedTree([0] * (N + 10)) total = 0 for x in A[:K - 1]: r = elem_rank[x] bit_num.add(r, 1) bit_sum.add(r, x) total += x answer = 10 ** 15 for add_x, rm_x in zip(A[K - 1:], A): add_r = elem_rank[add_x] rm_r = elem_rank[rm_x] bit_num.add(add_r, 1) bit_sum.add(add_r, add_x) total += add_x n = (K + 1) // 2 median_r = bit_num.find_kth_element(n) median = sortedA[median_r - 1] nl = bit_num.get_sum(median_r) nr = K - nl sl = bit_sum.get_sum(median_r) sr = total - sl cost = (nl * median - sl) + (sr - nr * median) if answer > cost: answer = cost bit_num.add(rm_r, -1) bit_sum.add(rm_r, -rm_x) total -= rm_x print(answer)