結果

問題 No.318 学学学学学
ユーザー 👑 rin204rin204
提出日時 2022-07-04 18:10:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 325 ms / 2,000 ms
コード長 1,947 bytes
コンパイル時間 2,013 ms
コンパイル使用メモリ 86,868 KB
実行使用メモリ 113,784 KB
最終ジャッジ日時 2023-08-21 01:57:00
合計ジャッジ時間 10,094 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 112 ms
79,072 KB
testcase_01 AC 118 ms
82,112 KB
testcase_02 AC 181 ms
84,116 KB
testcase_03 AC 117 ms
81,372 KB
testcase_04 AC 151 ms
83,244 KB
testcase_05 AC 325 ms
113,520 KB
testcase_06 AC 240 ms
98,748 KB
testcase_07 AC 218 ms
92,072 KB
testcase_08 AC 197 ms
91,648 KB
testcase_09 AC 184 ms
91,528 KB
testcase_10 AC 150 ms
91,528 KB
testcase_11 AC 315 ms
113,784 KB
testcase_12 AC 294 ms
97,928 KB
testcase_13 AC 241 ms
91,748 KB
testcase_14 AC 238 ms
91,504 KB
testcase_15 AC 220 ms
91,576 KB
testcase_16 AC 150 ms
91,752 KB
testcase_17 AC 217 ms
97,656 KB
testcase_18 AC 239 ms
97,828 KB
testcase_19 AC 201 ms
103,184 KB
testcase_20 AC 164 ms
92,124 KB
testcase_21 AC 76 ms
71,224 KB
testcase_22 AC 75 ms
70,880 KB
testcase_23 AC 75 ms
71,248 KB
testcase_24 AC 77 ms
71,288 KB
testcase_25 AC 77 ms
71,260 KB
testcase_26 AC 81 ms
71,148 KB
testcase_27 AC 80 ms
71,160 KB
testcase_28 AC 81 ms
71,232 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.cnt = {}
        self.length = len(lst)
    
    def __bool__(self):
        return self.length > 0
        
    def __len__(self):
        return self.length
    
    def __getitem__(self, i):
        if i == 0:
            return self.top()
        else:
            assert False
    
    def push(self, x):
        self.length += 1
        self.cnt[x * self.pm] = self.cnt.get(x * self.pm, 0) + 1
        heapq.heappush(self.hq, x * self.pm)
        self.tot += x
    
    def pop(self):
        if self.length == 0:
            return None
        self.length -= 1
        ret = heapq.heappop(self.hq)
        self.tot -= self.pm * ret
        self.cnt[ret] -= 1
        self.delete()
        return self.pm * ret
    
    def top(self):
        if self.hq:
            return self.pm * self.hq[0]
        else:
            return None
    
    def remove(self, x):
        if self.cnt.get(x * self.pm, 0) == 0:
            return False
        self.cnt[x * self.pm] -= 1
        self.length -= 1
        self.tot -= x
        self.delete()
        return True
            
    def delete(self):
        while self.hq and self.cnt.get(self.hq[0], 0) == 0:
            heapq.heappop(self.hq)

n = int(input())
A = list(map(int, input().split()))
L = {}
R = {}
for i, a in enumerate(A):
    if a not in L:
        L[a] = i
    R[a] = i

add = [-1] * n
remove = [-1] * n
for a, i in L.items():
    add[i] = a
for a, i in R.items():
    remove[i] = a

hq = Heapq(reverse=True)
ans = [0] * n
for i in range(n):
    if add[i] != -1:
        hq.push(add[i])
    ans[i] = hq.top()
    if remove[i] != -1:
        hq.remove(remove[i])
print(*ans)
0