結果

問題 No.2304 Distinct Elements
ユーザー ShirotsumeShirotsume
提出日時 2023-04-23 02:43:59
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,893 bytes
コンパイル時間 222 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 205,896 KB
最終ジャッジ日時 2024-04-26 21:29:13
合計ジャッジ時間 18,332 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
205,896 KB
testcase_01 AC 49 ms
54,272 KB
testcase_02 AC 44 ms
54,656 KB
testcase_03 AC 46 ms
54,528 KB
testcase_04 AC 360 ms
107,264 KB
testcase_05 AC 358 ms
108,032 KB
testcase_06 AC 361 ms
107,776 KB
testcase_07 AC 359 ms
107,648 KB
testcase_08 AC 364 ms
107,392 KB
testcase_09 AC 256 ms
129,336 KB
testcase_10 AC 261 ms
129,460 KB
testcase_11 AC 262 ms
130,220 KB
testcase_12 AC 255 ms
128,952 KB
testcase_13 AC 258 ms
130,328 KB
testcase_14 AC 79 ms
69,248 KB
testcase_15 AC 66 ms
64,768 KB
testcase_16 AC 66 ms
64,640 KB
testcase_17 AC 52 ms
55,808 KB
testcase_18 AC 53 ms
56,192 KB
testcase_19 AC 47 ms
54,528 KB
testcase_20 AC 46 ms
54,528 KB
testcase_21 AC 46 ms
54,528 KB
testcase_22 AC 45 ms
54,784 KB
testcase_23 AC 47 ms
54,528 KB
testcase_24 AC 51 ms
55,552 KB
testcase_25 AC 50 ms
54,912 KB
testcase_26 AC 54 ms
60,544 KB
testcase_27 AC 49 ms
55,168 KB
testcase_28 AC 48 ms
55,040 KB
testcase_29 AC 303 ms
101,504 KB
testcase_30 AC 160 ms
78,080 KB
testcase_31 AC 273 ms
96,256 KB
testcase_32 AC 236 ms
118,268 KB
testcase_33 AC 93 ms
80,000 KB
testcase_34 AC 240 ms
125,732 KB
testcase_35 AC 246 ms
125,648 KB
testcase_36 AC 93 ms
79,488 KB
testcase_37 AC 205 ms
83,968 KB
testcase_38 AC 373 ms
108,544 KB
testcase_39 AC 368 ms
108,032 KB
testcase_40 AC 284 ms
96,512 KB
testcase_41 AC 219 ms
85,120 KB
testcase_42 AC 346 ms
105,600 KB
testcase_43 AC 348 ms
110,616 KB
testcase_44 AC 219 ms
114,520 KB
testcase_45 AC 239 ms
101,976 KB
testcase_46 AC 199 ms
99,740 KB
testcase_47 AC 69 ms
65,792 KB
testcase_48 AC 68 ms
66,048 KB
testcase_49 AC 358 ms
109,184 KB
testcase_50 AC 354 ms
105,612 KB
testcase_51 AC 358 ms
106,024 KB
testcase_52 AC 623 ms
107,264 KB
testcase_53 AC 313 ms
118,196 KB
testcase_54 AC 533 ms
106,368 KB
testcase_55 AC 520 ms
105,600 KB
testcase_56 AC 489 ms
105,728 KB
testcase_57 TLE -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
testcase_61 -- -
権限があれば一括ダウンロードができます

ソースコード

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]
        else:
            return 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 = X.med()
        m1 = Y.med()
        if m1 <= m2:
            stack.append(Y)
            stack.append(X)
            break
        else:
            if 1:
                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()
    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