[백준 11279] 최대 힙_Python

코뉴·2021년 2월 13일
0

백준🍳

목록 보기
35/149

https://www.acmicpc.net/problem/11279

🥚문제


🥚입력/출력


🍳코드

import sys
import heapq

input = sys.stdin.readline

n = int(input())
q = []
for _ in range(n):
    x = int(input())
    if x == 0:
        if len(q) > 0:
            print(heapq.heappop(q))
        else:
            print(0)
    else:
        heapq.heappush(q, x)

🧂아이디어

  • 최대 힙 써보기 문제
  • 최소 힙을 그대로 활용하되, (-n, n)의 형태로 삽입하면 -n의 값이 작은 것부터 pop한다. 즉, n의 값이 큰 것부터 pop되므로 최대 힙의 형태를 가질 수 있다.
profile
코뉴의 도딩기록

0개의 댓글