반응형
#best
import sys
import heapq
input=sys.stdin.readline
heap = []
for _ in range(int(input())):
x = int(input())
if x != 0:
heapq.heappush(heap, [abs(x),x])
else:
print(heapq.heappop(heap)[1] if heap else 0)
- 힙에 배열 넣기
#try
import sys,heapq
heap = []
minus = []
for i in range(int(sys.stdin.readline())):
x = int(sys.stdin.readline())
if x == 0:
if len(heap) == 0 :
print(0)
elif len(minus) != 0:
H = heapq.heappop(heap)
M = heapq.heappop(minus)
if H == M:
print(M*-1)
else :
heapq.heappush(minus,M)
print(H)
else :
print(heapq.heappop(heap))
elif x < 0 :
heapq.heappush(minus,abs(x))
heapq.heappush(heap,abs(x))
else :
heapq.heappush(heap,abs(x))
#파이썬은 최소값이 루트. 최대값 pop하려면 -1
import sys, heapq
heap = []
for i in range(int(sys.stdin.readline())):
N = int(sys.stdin.readline()) * -1
if N == 0:
if len(heap) == 0:
print(0)
else:
print(heapq.heappop(heap) * -1)
else:
heapq.heappush(heap, N)
반응형
'Python' 카테고리의 다른 글
#10988_팰린드롬(거꾸로 읽을 때 같은 것) (0) | 2021.10.04 |
---|---|
#11005_10진법수 B진법 변환 (0) | 2021.10.04 |
#11650_좌표정렬 (0) | 2021.10.04 |
#15649_순열(순서고려), #15650_조합(순서고려x) (0) | 2021.10.04 |
#10825_배열 정렬_감소증가혼합 (0) | 2021.09.20 |