cmod.ify
[11286] 절댓값 힙 본문
728x90
반응형
음수 양수 체크하려고 튜플 형식으로 처리함
import sys
from heapq import *
input = sys.stdin.readline
heap = []
n = int(input())
answer = []
for i in range(n):
x = int(input())
if x != 0:
if x < 0:
b = 0 # 음수
x = -x
else:
b = 1 # 양수
heappush(heap, (x, b))
elif x == 0:
if heap:
px, pb = heappop(heap)
if pb == 0:
answer.append(-px)
else:
answer.append(px)
else:
answer.append(0)
for i in answer:
print(i)
728x90
반응형
'BASIC > 코딩테스트' 카테고리의 다른 글
| [5430] AC (0) | 2026.01.07 |
|---|---|
| [11403] 경로 찾기 - 플로이드-워셜 (0) | 2026.01.07 |
| [5525] IOIOI (0) | 2026.01.06 |
| [2667] 단지번호붙이기 (1) | 2026.01.06 |
| [2178] 미로 탐색 (0) | 2026.01.05 |