λ¬Έμ
https://www.acmicpc.net/problem/7662
νμ΄
# λ°±μ€ 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')
'ποΈ Algorithm > π© λ°±μ€' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
π© [λ°±μ€] [Python] [Gold5] 1011λ²_Fly me to the Alpha Centauri (0) | 2023.04.21 |
---|---|
π© [λ°±μ€] [Python] [Gold5] 2447λ²_λ³ μ°κΈ° - 10 (0) | 2023.04.21 |
π© [λ°±μ€] [Python] [Gold3] 1520λ²_λ΄λ¦¬λ§ κΈΈ (0) | 2023.04.19 |
π© [λ°±μ€] [Python] [Silver1] 1991λ²_νΈλ¦¬ μν (0) | 2023.04.19 |
π© [λ°±μ€] [Python] [Silver3] 15654λ²_Nκ³ΌM(5) (0) | 2023.04.18 |