結果

問題 No.1438 Broken Drawers
ユーザー aqua_tenhou
提出日時 2021-03-26 21:31:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 562 ms / 2,000 ms
コード長 1,079 bytes
コンパイル時間 176 ms
コンパイル使用メモリ 82,400 KB
実行使用メモリ 158,280 KB
最終ジャッジ日時 2024-11-28 22:04:24
合計ジャッジ時間 10,336 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
from sys import exit
class HeapDict:
    def __init__(self):
        self.h=[]
        self.d=dict()

    def insert(self,x):
        heapq.heappush(self.h,x)
        if x not in self.d:
            self.d[x]=1
        else:
            self.d[x]+=1

    def erase(self,x):
        if x not in self.d or self.d[x]==0:
            print(x,"is not in HeapDict")
            exit()
        else:
            self.d[x]-=1

        while len(self.h)!=0:
            if self.d[self.h[0]]==0:
                heapq.heappop(self.h)
            else:
                break

    def is_exist(self,x):
        if x in self.d and self.d[x]!=0:
            return True
        else:
            return False

    def get_min(self):
        return self.h[0]


n = int(input())
a = list(map(int,input().split()))

d = {}
hq = HeapDict()
for i,x in enumerate(a):
    d[x] = i
    hq.insert(x)

ans = 0
k = 0
while k < n:
    m = hq.get_min()
    k += 1
    ans += 1
    hq.erase(m)
    if 0 <= d[m]-1 and hq.is_exist(a[d[m]-1]):
        hq.erase(a[d[m]-1])
        k += 1
print(ans)
0