結果
問題 | No.738 平らな農地 |
ユーザー | tktk_snsn |
提出日時 | 2020-12-10 23:47:09 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 366 ms / 2,000 ms |
コード長 | 1,802 bytes |
コンパイル時間 | 271 ms |
コンパイル使用メモリ | 82,452 KB |
実行使用メモリ | 124,256 KB |
最終ジャッジ日時 | 2024-09-19 20:50:59 |
合計ジャッジ時間 | 19,597 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 41 ms
52,352 KB |
testcase_01 | AC | 40 ms
52,480 KB |
testcase_02 | AC | 37 ms
52,480 KB |
testcase_03 | AC | 36 ms
52,736 KB |
testcase_04 | AC | 38 ms
52,736 KB |
testcase_05 | AC | 87 ms
77,056 KB |
testcase_06 | AC | 88 ms
77,708 KB |
testcase_07 | AC | 86 ms
77,260 KB |
testcase_08 | AC | 61 ms
73,344 KB |
testcase_09 | AC | 55 ms
67,072 KB |
testcase_10 | AC | 49 ms
62,848 KB |
testcase_11 | AC | 62 ms
73,600 KB |
testcase_12 | AC | 55 ms
66,560 KB |
testcase_13 | AC | 84 ms
77,184 KB |
testcase_14 | AC | 57 ms
69,376 KB |
testcase_15 | AC | 245 ms
107,516 KB |
testcase_16 | AC | 255 ms
110,976 KB |
testcase_17 | AC | 289 ms
110,940 KB |
testcase_18 | AC | 276 ms
107,136 KB |
testcase_19 | AC | 295 ms
111,204 KB |
testcase_20 | AC | 268 ms
111,232 KB |
testcase_21 | AC | 306 ms
111,488 KB |
testcase_22 | AC | 275 ms
110,848 KB |
testcase_23 | AC | 295 ms
111,104 KB |
testcase_24 | AC | 298 ms
110,848 KB |
testcase_25 | AC | 54 ms
67,328 KB |
testcase_26 | AC | 55 ms
68,096 KB |
testcase_27 | AC | 57 ms
68,736 KB |
testcase_28 | AC | 59 ms
68,480 KB |
testcase_29 | AC | 59 ms
69,788 KB |
testcase_30 | AC | 55 ms
68,224 KB |
testcase_31 | AC | 54 ms
67,328 KB |
testcase_32 | AC | 58 ms
68,480 KB |
testcase_33 | AC | 56 ms
68,608 KB |
testcase_34 | AC | 55 ms
66,816 KB |
testcase_35 | AC | 54 ms
66,048 KB |
testcase_36 | AC | 56 ms
67,072 KB |
testcase_37 | AC | 61 ms
68,096 KB |
testcase_38 | AC | 57 ms
68,480 KB |
testcase_39 | AC | 58 ms
69,504 KB |
testcase_40 | AC | 58 ms
69,248 KB |
testcase_41 | AC | 58 ms
69,248 KB |
testcase_42 | AC | 53 ms
66,176 KB |
testcase_43 | AC | 54 ms
65,920 KB |
testcase_44 | AC | 55 ms
67,072 KB |
testcase_45 | AC | 267 ms
114,596 KB |
testcase_46 | AC | 281 ms
109,556 KB |
testcase_47 | AC | 264 ms
113,832 KB |
testcase_48 | AC | 246 ms
113,796 KB |
testcase_49 | AC | 242 ms
111,360 KB |
testcase_50 | AC | 242 ms
113,792 KB |
testcase_51 | AC | 272 ms
113,900 KB |
testcase_52 | AC | 259 ms
111,488 KB |
testcase_53 | AC | 263 ms
114,008 KB |
testcase_54 | AC | 261 ms
113,840 KB |
testcase_55 | AC | 262 ms
113,972 KB |
testcase_56 | AC | 277 ms
114,048 KB |
testcase_57 | AC | 250 ms
109,696 KB |
testcase_58 | AC | 253 ms
113,792 KB |
testcase_59 | AC | 265 ms
114,176 KB |
testcase_60 | AC | 253 ms
114,384 KB |
testcase_61 | AC | 262 ms
114,304 KB |
testcase_62 | AC | 248 ms
109,696 KB |
testcase_63 | AC | 276 ms
114,176 KB |
testcase_64 | AC | 279 ms
114,176 KB |
testcase_65 | AC | 60 ms
79,104 KB |
testcase_66 | AC | 60 ms
79,616 KB |
testcase_67 | AC | 225 ms
118,768 KB |
testcase_68 | AC | 227 ms
123,264 KB |
testcase_69 | AC | 277 ms
123,652 KB |
testcase_70 | AC | 252 ms
124,032 KB |
testcase_71 | AC | 181 ms
89,856 KB |
testcase_72 | AC | 174 ms
90,860 KB |
testcase_73 | AC | 161 ms
91,824 KB |
testcase_74 | AC | 184 ms
92,544 KB |
testcase_75 | AC | 261 ms
118,912 KB |
testcase_76 | AC | 248 ms
123,648 KB |
testcase_77 | AC | 270 ms
119,296 KB |
testcase_78 | AC | 303 ms
124,068 KB |
testcase_79 | AC | 288 ms
124,032 KB |
testcase_80 | AC | 254 ms
123,392 KB |
testcase_81 | AC | 279 ms
123,392 KB |
testcase_82 | AC | 285 ms
123,776 KB |
testcase_83 | AC | 238 ms
91,648 KB |
testcase_84 | AC | 194 ms
91,128 KB |
testcase_85 | AC | 366 ms
124,256 KB |
testcase_86 | AC | 321 ms
119,060 KB |
testcase_87 | AC | 169 ms
123,776 KB |
testcase_88 | AC | 163 ms
118,784 KB |
testcase_89 | AC | 37 ms
52,352 KB |
testcase_90 | AC | 38 ms
52,352 KB |
testcase_91 | AC | 37 ms
52,224 KB |
ソースコード
class fenwick_tree(object): def __init__(self, n): self.n = n self.log = n.bit_length() self.data = [0] * n def __sum(self, r): s = 0 while r > 0: s += self.data[r - 1] r -= r & -r return s def add(self, p, x): """ a[p] += xを行う""" p += 1 while p <= self.n: self.data[p - 1] += x p += p & -p def sum(self, l, r): """a[l] + a[l+1] + .. + a[r-1]を返す""" return self.__sum(r) - self.__sum(l) def lower_bound(self, x): """a[0] + a[1] + .. a[i] >= x となる最小のiを返す""" if x <= 0: return -1 i = 0 k = 1 << self.log while k: if i + k <= self.n and self.data[i + k - 1] < x: x -= self.data[i + k - 1] i += k k >>= 1 return i def show(self): for i in range(self.n): print(self.__sum(i+1) if not i else self.sum(i, i+1), end=" ") print() N, K = map(int, input().split()) A = list(map(int, input().split())) if K == 1: print(0) exit() Aset = sorted(set(A)) atoi = {a: i for i, a in enumerate(Aset)} bit_cnt = fenwick_tree(N) bit_val = fenwick_tree(N) for i in range(K - 1): a = A[i] p = atoi[a] bit_cnt.add(p, 1) bit_val.add(p, a) ans = 10 ** 18 for i in range(K - 1, N): a = A[i] p = atoi[a] bit_cnt.add(p, 1) bit_val.add(p, a) mid = bit_cnt.lower_bound((K + 1) // 2) a = Aset[mid] cost = a * bit_cnt.sum(0, mid) - bit_val.sum(0, mid) cost += bit_val.sum(mid + 1, N) - a * bit_cnt.sum(mid + 1, N) ans = min(ans, cost) a = A[i - K + 1] p = atoi[a] bit_cnt.add(p, -1) bit_val.add(p, -a) print(ans)