문제 링크
https://www.acmicpc.net/problem/1927
1927번: 최소 힙
첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0
www.acmicpc.net
풀이
- 최소 힙 자료구조 사용
- import heapq
- 원소 추가 : heapq.heappush( 리스트 이름, 원소 )
- 원소 삭제 : heapq.heappop( 리스트 이름 )
import heapq
n = int(input())
h = []
for _ in range(n):
x = int(input())
if x == 0:
if h:
print(heapq.heappop(h))
else:
print(0)
else:
heapq.heappush(h,x)
'개발 > algorithm' 카테고리의 다른 글
[백준 11279번] 최대 힙 - python (0) | 2022.02.15 |
---|---|
[백준 1541번] 잃어버린 괄호 - python (0) | 2022.02.15 |
[백준 11727번] 2 x n 타일링 2 - python (0) | 2022.02.14 |
[백준 11726번] 2 x n 타일링 - python (0) | 2022.02.14 |
[백준 11659번] 구간 합 구하기 4 - python (0) | 2022.02.14 |