結果

問題 No.837 Noelちゃんと星々2
ユーザー 👑 rin204rin204
提出日時 2022-01-19 00:40:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 757 ms / 2,000 ms
コード長 2,820 bytes
コンパイル時間 368 ms
コンパイル使用メモリ 87,108 KB
実行使用メモリ 109,032 KB
最終ジャッジ日時 2023-09-02 07:35:04
合計ジャッジ時間 11,340 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 739 ms
109,032 KB
testcase_01 AC 750 ms
108,864 KB
testcase_02 AC 746 ms
108,612 KB
testcase_03 AC 743 ms
107,436 KB
testcase_04 AC 757 ms
108,048 KB
testcase_05 AC 750 ms
107,356 KB
testcase_06 AC 746 ms
108,264 KB
testcase_07 AC 746 ms
108,488 KB
testcase_08 AC 746 ms
108,768 KB
testcase_09 AC 93 ms
75,704 KB
testcase_10 AC 85 ms
71,544 KB
testcase_11 AC 85 ms
71,592 KB
testcase_12 AC 85 ms
71,368 KB
testcase_13 AC 85 ms
71,144 KB
testcase_14 AC 91 ms
75,556 KB
testcase_15 AC 90 ms
75,600 KB
testcase_16 AC 83 ms
71,600 KB
testcase_17 AC 85 ms
71,380 KB
testcase_18 AC 86 ms
71,440 KB
testcase_19 AC 85 ms
71,372 KB
testcase_20 AC 85 ms
71,548 KB
testcase_21 AC 92 ms
75,632 KB
testcase_22 AC 89 ms
75,524 KB
testcase_23 AC 86 ms
71,552 KB
testcase_24 AC 86 ms
71,420 KB
testcase_25 AC 85 ms
71,316 KB
testcase_26 AC 84 ms
71,364 KB
testcase_27 AC 83 ms
71,320 KB
testcase_28 AC 83 ms
71,296 KB
testcase_29 AC 84 ms
71,376 KB
testcase_30 AC 84 ms
71,488 KB
testcase_31 AC 82 ms
71,352 KB
testcase_32 AC 84 ms
71,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

class Heapq:
    def __init__(self, lst = [], reverse = False):
        if reverse:
            self.pm = -1
            self.hq = [-l for l in lst]
        else:
            self.pm = 1
            self.hq = lst.copy()
        heapq.heapify(self.hq)
        self.tot = sum(lst)
        self.rm = []
        self.length = len(lst)
            
    def push(self, x):
        self.length += 1
        heapq.heappush(self.hq, x * self.pm)
        self.tot += x
    
    def pop(self):
        self.length -= 1
        ret = heapq.heappop(self.hq)
        self.tot -= self.pm * ret
        self.delete()
        return self.pm * ret
    
    def front(self):
        return self.pm * self.hq[0]
    
    def remove(self, x): # 存在しないものを消そうとするとバグる
        self.length -= 1
        self.tot -= x
        heapq.heappush(self.rm, self.pm * x)
        self.delete()
            
    def delete(self):
        while self.rm and self.rm[0] == self.hq[0]:
            heapq.heappop(self.rm)
            heapq.heappop(self.hq)

class Med_lst:
    def __init__(self, lst = []):
        inf = 10 ** 20
        l = len(lst)
        self.low = Heapq(lst = [-inf] + lst[:(l + 1) // 2], reverse = True)
        self.upp = Heapq(lst = [inf] + lst[(l + 1) // 2:])
        self.low.tot += inf
        self.upp.tot -= inf
        self.low.length -= 1
        self.upp.length -= 1
        
#    def get(self):
#        return self.low.front()
    
    def get(self):
        if self.low.length == self.upp.length:
            return (self.low.front() + self.upp.front()) // 2
        else:
            return self.low.front()
    
    def abs_sum(self):
        med = self.low.front()
        ret = med * self.low.length - self.low.tot
        ret += self.upp.tot - med * self.upp.length
        return ret
        
    def push(self, x):
        lst = [self.low.pop(), x, self.upp.pop()]
        lst.sort()
        if self.low.length == self.upp.length:
            self.low.push(lst[0])
            self.low.push(lst[1])
            self.upp.push(lst[2])
        else:
            self.low.push(lst[0])
            self.upp.push(lst[1])
            self.upp.push(lst[2])
        
    def remove(self, x):
        med = self.get()
        if x <= med:
            self.low.remove(x)
            if self.low.length < self.upp.length:
                self.low.push(self.upp.pop())
        else:
            self.upp.remove(x)
            if self.low.length > self.upp.length + 1:
                self.upp.push(self.low.pop())

n = int(input())
Y = list(map(int, input().split()))
if len(set(Y)) == 1:
    print(1)
    exit()
Y.sort()
m1 = Med_lst()
m2 = Med_lst(Y)
ans = 1 << 60
for i in range(n):
    m1.push(Y[i])
    m2.remove(Y[i])
    ans = min(ans, m1.abs_sum() + m2.abs_sum())
print(ans)
0