結果

問題 No.1438 Broken Drawers
ユーザー aqua_tenhouaqua_tenhou
提出日時 2021-03-26 21:31:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 563 ms / 2,000 ms
コード長 1,079 bytes
コンパイル時間 293 ms
コンパイル使用メモリ 87,228 KB
実行使用メモリ 178,876 KB
最終ジャッジ日時 2023-08-19 07:33:06
合計ジャッジ時間 11,396 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,452 KB
testcase_01 AC 77 ms
71,628 KB
testcase_02 AC 77 ms
71,300 KB
testcase_03 AC 77 ms
71,504 KB
testcase_04 AC 79 ms
71,120 KB
testcase_05 AC 77 ms
71,656 KB
testcase_06 AC 351 ms
178,876 KB
testcase_07 AC 378 ms
134,452 KB
testcase_08 AC 80 ms
71,512 KB
testcase_09 AC 83 ms
75,748 KB
testcase_10 AC 79 ms
71,584 KB
testcase_11 AC 171 ms
79,752 KB
testcase_12 AC 284 ms
96,720 KB
testcase_13 AC 361 ms
106,844 KB
testcase_14 AC 251 ms
95,232 KB
testcase_15 AC 518 ms
122,996 KB
testcase_16 AC 519 ms
122,372 KB
testcase_17 AC 536 ms
130,992 KB
testcase_18 AC 537 ms
131,688 KB
testcase_19 AC 520 ms
131,780 KB
testcase_20 AC 514 ms
131,692 KB
testcase_21 AC 519 ms
130,908 KB
testcase_22 AC 543 ms
131,796 KB
testcase_23 AC 543 ms
132,412 KB
testcase_24 AC 549 ms
131,776 KB
testcase_25 AC 543 ms
132,428 KB
testcase_26 AC 544 ms
132,484 KB
testcase_27 AC 563 ms
132,020 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