結果

問題 No.1438 Broken Drawers
ユーザー aqua_tenhouaqua_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
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
53,972 KB
testcase_01 AC 43 ms
53,392 KB
testcase_02 AC 41 ms
52,804 KB
testcase_03 AC 40 ms
52,872 KB
testcase_04 AC 40 ms
52,820 KB
testcase_05 AC 41 ms
54,216 KB
testcase_06 AC 327 ms
158,280 KB
testcase_07 AC 351 ms
122,704 KB
testcase_08 AC 44 ms
54,020 KB
testcase_09 AC 47 ms
60,184 KB
testcase_10 AC 42 ms
53,816 KB
testcase_11 AC 136 ms
78,336 KB
testcase_12 AC 261 ms
96,988 KB
testcase_13 AC 350 ms
104,612 KB
testcase_14 AC 224 ms
96,620 KB
testcase_15 AC 508 ms
112,416 KB
testcase_16 AC 523 ms
112,848 KB
testcase_17 AC 541 ms
117,540 KB
testcase_18 AC 541 ms
118,052 KB
testcase_19 AC 512 ms
117,704 KB
testcase_20 AC 529 ms
118,020 KB
testcase_21 AC 523 ms
117,692 KB
testcase_22 AC 523 ms
117,496 KB
testcase_23 AC 537 ms
117,900 KB
testcase_24 AC 550 ms
118,148 KB
testcase_25 AC 543 ms
117,812 KB
testcase_26 AC 533 ms
117,664 KB
testcase_27 AC 562 ms
116,520 KB
権限があれば一括ダウンロードができます

ソースコード

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