結果

問題 No.2304 Distinct Elements
ユーザー ShirotsumeShirotsume
提出日時 2023-04-23 02:17:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 652 ms / 3,000 ms
コード長 1,948 bytes
コンパイル時間 450 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 129,044 KB
最終ジャッジ日時 2024-04-26 21:27:55
合計ジャッジ時間 16,992 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
54,528 KB
testcase_01 AC 45 ms
54,912 KB
testcase_02 AC 45 ms
54,400 KB
testcase_03 AC 45 ms
54,912 KB
testcase_04 AC 392 ms
107,484 KB
testcase_05 AC 393 ms
107,944 KB
testcase_06 AC 388 ms
107,520 KB
testcase_07 AC 389 ms
107,552 KB
testcase_08 AC 395 ms
107,520 KB
testcase_09 AC 263 ms
128,748 KB
testcase_10 AC 265 ms
128,272 KB
testcase_11 AC 266 ms
128,792 KB
testcase_12 AC 264 ms
127,772 KB
testcase_13 AC 269 ms
129,044 KB
testcase_14 AC 70 ms
66,048 KB
testcase_15 AC 62 ms
62,976 KB
testcase_16 AC 59 ms
61,952 KB
testcase_17 AC 51 ms
55,808 KB
testcase_18 AC 52 ms
55,936 KB
testcase_19 AC 47 ms
54,528 KB
testcase_20 AC 49 ms
54,784 KB
testcase_21 AC 49 ms
54,784 KB
testcase_22 AC 48 ms
54,272 KB
testcase_23 AC 48 ms
54,656 KB
testcase_24 AC 52 ms
55,168 KB
testcase_25 AC 52 ms
55,168 KB
testcase_26 AC 57 ms
60,672 KB
testcase_27 AC 51 ms
55,424 KB
testcase_28 AC 51 ms
55,680 KB
testcase_29 AC 332 ms
101,632 KB
testcase_30 AC 171 ms
78,092 KB
testcase_31 AC 294 ms
96,512 KB
testcase_32 AC 241 ms
115,984 KB
testcase_33 AC 96 ms
79,616 KB
testcase_34 AC 245 ms
125,448 KB
testcase_35 AC 257 ms
126,596 KB
testcase_36 AC 97 ms
79,104 KB
testcase_37 AC 226 ms
83,968 KB
testcase_38 AC 403 ms
108,288 KB
testcase_39 AC 411 ms
107,904 KB
testcase_40 AC 314 ms
96,640 KB
testcase_41 AC 231 ms
84,736 KB
testcase_42 AC 372 ms
105,856 KB
testcase_43 AC 388 ms
110,964 KB
testcase_44 AC 232 ms
114,620 KB
testcase_45 AC 236 ms
100,932 KB
testcase_46 AC 203 ms
100,652 KB
testcase_47 AC 70 ms
66,432 KB
testcase_48 AC 70 ms
66,432 KB
testcase_49 AC 393 ms
109,056 KB
testcase_50 AC 397 ms
106,036 KB
testcase_51 AC 393 ms
105,392 KB
testcase_52 AC 652 ms
106,880 KB
testcase_53 AC 330 ms
118,064 KB
testcase_54 AC 559 ms
105,984 KB
testcase_55 AC 521 ms
105,728 KB
testcase_56 AC 508 ms
105,472 KB
testcase_57 AC 508 ms
108,032 KB
testcase_58 AC 477 ms
107,776 KB
testcase_59 AC 409 ms
105,984 KB
testcase_60 AC 51 ms
54,016 KB
testcase_61 AC 50 ms
54,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)


0