結果
問題 | No.2304 Distinct Elements |
ユーザー | Shirotsume |
提出日時 | 2023-04-23 02:17:16 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 579 ms / 3,000 ms |
コード長 | 1,948 bytes |
コンパイル時間 | 386 ms |
コンパイル使用メモリ | 82,700 KB |
実行使用メモリ | 129,388 KB |
最終ジャッジ日時 | 2024-11-14 12:49:01 |
合計ジャッジ時間 | 15,209 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 43 ms
55,768 KB |
testcase_01 | AC | 42 ms
55,140 KB |
testcase_02 | AC | 43 ms
55,260 KB |
testcase_03 | AC | 42 ms
54,632 KB |
testcase_04 | AC | 351 ms
107,460 KB |
testcase_05 | AC | 355 ms
107,800 KB |
testcase_06 | AC | 347 ms
107,420 KB |
testcase_07 | AC | 352 ms
108,064 KB |
testcase_08 | AC | 350 ms
107,560 KB |
testcase_09 | AC | 242 ms
128,752 KB |
testcase_10 | AC | 243 ms
128,112 KB |
testcase_11 | AC | 244 ms
129,376 KB |
testcase_12 | AC | 241 ms
128,216 KB |
testcase_13 | AC | 241 ms
129,388 KB |
testcase_14 | AC | 60 ms
65,676 KB |
testcase_15 | AC | 53 ms
62,904 KB |
testcase_16 | AC | 52 ms
62,396 KB |
testcase_17 | AC | 44 ms
56,792 KB |
testcase_18 | AC | 47 ms
55,888 KB |
testcase_19 | AC | 40 ms
55,324 KB |
testcase_20 | AC | 42 ms
55,008 KB |
testcase_21 | AC | 42 ms
54,916 KB |
testcase_22 | AC | 43 ms
54,644 KB |
testcase_23 | AC | 43 ms
56,036 KB |
testcase_24 | AC | 45 ms
56,232 KB |
testcase_25 | AC | 44 ms
55,244 KB |
testcase_26 | AC | 48 ms
62,160 KB |
testcase_27 | AC | 45 ms
55,800 KB |
testcase_28 | AC | 46 ms
56,448 KB |
testcase_29 | AC | 299 ms
102,036 KB |
testcase_30 | AC | 156 ms
78,340 KB |
testcase_31 | AC | 268 ms
96,296 KB |
testcase_32 | AC | 224 ms
115,944 KB |
testcase_33 | AC | 82 ms
79,520 KB |
testcase_34 | AC | 224 ms
125,464 KB |
testcase_35 | AC | 232 ms
126,376 KB |
testcase_36 | AC | 80 ms
79,316 KB |
testcase_37 | AC | 201 ms
83,824 KB |
testcase_38 | AC | 357 ms
108,428 KB |
testcase_39 | AC | 362 ms
108,228 KB |
testcase_40 | AC | 273 ms
96,184 KB |
testcase_41 | AC | 212 ms
84,584 KB |
testcase_42 | AC | 328 ms
105,388 KB |
testcase_43 | AC | 344 ms
111,240 KB |
testcase_44 | AC | 215 ms
114,704 KB |
testcase_45 | AC | 217 ms
100,992 KB |
testcase_46 | AC | 185 ms
100,868 KB |
testcase_47 | AC | 62 ms
67,032 KB |
testcase_48 | AC | 61 ms
67,020 KB |
testcase_49 | AC | 349 ms
108,996 KB |
testcase_50 | AC | 356 ms
105,456 KB |
testcase_51 | AC | 355 ms
105,216 KB |
testcase_52 | AC | 579 ms
106,896 KB |
testcase_53 | AC | 303 ms
118,016 KB |
testcase_54 | AC | 511 ms
106,416 KB |
testcase_55 | AC | 476 ms
105,560 KB |
testcase_56 | AC | 463 ms
105,524 KB |
testcase_57 | AC | 471 ms
107,968 KB |
testcase_58 | AC | 433 ms
107,512 KB |
testcase_59 | AC | 362 ms
105,860 KB |
testcase_60 | AC | 42 ms
54,808 KB |
testcase_61 | AC | 42 ms
56,172 KB |
ソースコード
import sys from collections import deque, Counter input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) inf = 2 ** 63 - 1 mod = 998244353 import heapq class MedianHeap(): def __init__(self): self.top = [] self.bottom = [] def __len__(self): return len(self.top) + len(self.bottom) def add(self, x): heapq.heappush(self.top, x) while len(self.top) - 2 >= len(self.bottom): heapq.heappush(self.bottom, -heapq.heappop(self.top)) while self.top and self.bottom and self.top[0] < -self.bottom[0]: x = heapq.heappop(self.top) y = heapq.heappop(self.bottom) heapq.heappush(self.top, -y) heapq.heappush(self.bottom, -x) def med(self): if len(self) % 2: return self.top[0], self.top[0] else: return -self.bottom[0], self.top[0] n = ii() a = li() a.sort() a = [a[i] - i for i in range(n)] stack = [] for V in a: M = MedianHeap() M.add(V) stack.append(M) while len(stack) >= 2: X = stack.pop() Y = stack.pop() m2, M2 = X.med() m1, M1 = Y.med() if M1 <= M2: stack.append(Y) stack.append(X) break else: if len(X) < len(Y): for v in X.top: Y.add(v) for v in X.bottom: Y.add(-v) stack.append(Y) else: for v in Y.top: X.add(v) for v in Y.bottom: X.add(-v) stack.append(X) P = [len(stack[i]) for i in range(len(stack))] cnt = 0 b = [0] * n for i, v in enumerate(P): med = stack[i].med()[0] for x in range(v): b[cnt] = med cnt += 1 ans = sum([abs(a[i] - b[i]) for i in range(n)]) print(ans)