ποΈ Algorithm/π© λ°±μ€
π© [λ°±μ€] [Python] [Gold4] 7662λ²_μ΄μ€ μ°μ μμ ν
Dbswnstjd
2023. 4. 20. 10:42
λ¬Έμ
https://www.acmicpc.net/problem/7662
7662λ²: μ΄μ€ μ°μ μμ ν
μ λ ₯ λ°μ΄ν°λ νμ€μ λ ₯μ μ¬μ©νλ€. μ λ ₯μ Tκ°μ ν μ€νΈ λ°μ΄ν°λ‘ ꡬμ±λλ€. μ λ ₯μ 첫 λ²μ§Έ μ€μλ μ λ ₯ λ°μ΄ν°μ μλ₯Ό λνλ΄λ μ μ Tκ° μ£Όμ΄μ§λ€. κ° ν μ€νΈ λ°μ΄ν°μ 첫째 μ€μλ Qμ μ
www.acmicpc.net
νμ΄
# λ°±μ€ 7662λ² λ¬Έμ - μ΄μ€ μ°μ μμ ν
import sys
import heapq
input = sys.stdin.readline
test = int(input())
for _ in range(test):
max_heap, min_heap = [], []
visit = [0] * 1_000_001
n = int(input())
for i in range(n):
cmd = input().split()
if cmd[0] == 'I':
heapq.heappush(min_heap, (int(cmd[1]), i))
heapq.heappush(max_heap, (int(cmd[1]) * -1, i))
visit[i] = 1 #Trueμ΄λ©΄ μ΄λ€ νμμλ μμ§ μμ λμ§ μμ μν
elif cmd[0] == 'D':
if cmd[1] == '-1': #μμ μ°μ°μ, keyκ°μ κΈ°μ€μΌλ‘ ν΄λΉ λ
Έλκ° λ€λ₯Ένμμ μμ λ λ
ΈλμΈκ°λ₯Ό λ¨Όμ νλ¨νλ€.
# μ΄λ―Έ μλνμ μν΄ μμ λ λ
ΈλμΈκ²½μ° μμ λμ§ μμ λ
Έλκ° λμ¬λκΉμ§ κ³μ λ²λ¦¬λ€κ° μ΄ν μμ λμλ
Έλκ° λμ€λ©΄ μμ νλ€.
while min_heap and not visit[min_heap[0][1]]: # visitμ΄ FalseμΌλ -> ν΄λΉλ
Έλκ° μμ λμν
heapq.heappop(min_heap) # λ²λ¦Ό (μλνμμ μ΄λ―Έ μμ λλ
Έλμ΄λ―λ‘)
if min_heap:
visit[min_heap[0][1]] = 0 # visitμ΄ TureμΏμΌλ―λ‘ Falseλ‘ λ°κΎΈκ³ λ΄κ° μμ ν¨
heapq.heappop(min_heap)
elif cmd[1] == '1':
while max_heap and not visit[max_heap[0][1]]: #μ΄λ―Έ μμ λ λ
ΈλμΈκ²½μ° μμ λμ§ μμ λ
Έλκ° λμ¬λκΉμ§ λͺ¨λ λ²λ¦°λ€.
heapq.heappop(max_heap)
if max_heap:
visit[max_heap[0][1]] = 0
heapq.heappop(max_heap)
while min_heap and not visit[min_heap[0][1]]:
heapq.heappop(min_heap)
while max_heap and not visit[max_heap[0][1]]:
heapq.heappop(max_heap)
if min_heap and max_heap:
print(-max_heap[0][0], min_heap[0][0])
else:
print('EMPTY')