結果

問題 No.2304 Distinct Elements
ユーザー ShirotsumeShirotsume
提出日時 2023-04-23 02:25:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 643 ms / 3,000 ms
コード長 1,907 bytes
コンパイル時間 441 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 130,088 KB
最終ジャッジ日時 2024-04-26 21:27:01
合計ジャッジ時間 17,985 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
54,272 KB
testcase_01 AC 46 ms
54,528 KB
testcase_02 AC 47 ms
54,656 KB
testcase_03 AC 48 ms
54,144 KB
testcase_04 AC 398 ms
107,648 KB
testcase_05 AC 395 ms
107,648 KB
testcase_06 AC 398 ms
107,264 KB
testcase_07 AC 393 ms
107,392 KB
testcase_08 AC 392 ms
107,520 KB
testcase_09 AC 267 ms
129,188 KB
testcase_10 AC 263 ms
129,208 KB
testcase_11 AC 264 ms
129,972 KB
testcase_12 AC 258 ms
128,696 KB
testcase_13 AC 262 ms
130,088 KB
testcase_14 AC 72 ms
65,536 KB
testcase_15 AC 63 ms
62,592 KB
testcase_16 AC 61 ms
61,440 KB
testcase_17 AC 52 ms
55,424 KB
testcase_18 AC 53 ms
55,936 KB
testcase_19 AC 48 ms
54,144 KB
testcase_20 AC 48 ms
54,400 KB
testcase_21 AC 49 ms
54,016 KB
testcase_22 AC 49 ms
54,272 KB
testcase_23 AC 48 ms
54,400 KB
testcase_24 AC 53 ms
55,296 KB
testcase_25 AC 51 ms
55,168 KB
testcase_26 AC 57 ms
60,544 KB
testcase_27 AC 51 ms
54,912 KB
testcase_28 AC 52 ms
55,168 KB
testcase_29 AC 333 ms
101,632 KB
testcase_30 AC 175 ms
78,224 KB
testcase_31 AC 294 ms
96,640 KB
testcase_32 AC 243 ms
118,016 KB
testcase_33 AC 99 ms
79,488 KB
testcase_34 AC 245 ms
125,356 KB
testcase_35 AC 250 ms
125,268 KB
testcase_36 AC 99 ms
79,104 KB
testcase_37 AC 230 ms
83,840 KB
testcase_38 AC 407 ms
108,288 KB
testcase_39 AC 413 ms
107,904 KB
testcase_40 AC 315 ms
96,256 KB
testcase_41 AC 238 ms
84,992 KB
testcase_42 AC 371 ms
105,472 KB
testcase_43 AC 397 ms
110,736 KB
testcase_44 AC 233 ms
114,240 KB
testcase_45 AC 241 ms
101,192 KB
testcase_46 AC 195 ms
93,684 KB
testcase_47 AC 72 ms
66,304 KB
testcase_48 AC 72 ms
66,560 KB
testcase_49 AC 396 ms
108,672 KB
testcase_50 AC 402 ms
105,544 KB
testcase_51 AC 392 ms
105,804 KB
testcase_52 AC 643 ms
107,136 KB
testcase_53 AC 317 ms
117,768 KB
testcase_54 AC 551 ms
105,856 KB
testcase_55 AC 531 ms
105,472 KB
testcase_56 AC 514 ms
105,472 KB
testcase_57 AC 507 ms
107,904 KB
testcase_58 AC 481 ms
107,704 KB
testcase_59 AC 395 ms
105,856 KB
testcase_60 AC 49 ms
54,272 KB
testcase_61 AC 49 ms
54,144 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]
        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 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()
    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