PS/백준
백준 1927번: 최소힙 python 파이썬
지기_
2021. 8. 6. 23:04
반응형
1. 문제
https://www.acmicpc.net/problem/1927
1927번: 최소 힙
첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0
www.acmicpc.net
2. 풀이
우선순위큐
3. 구현
import sys
# sys.stdin = open('input.txt')
import heapq
lis = []
n = int(sys.stdin.readline().strip())
while n:
n-=1
tmp = int(sys.stdin.readline().strip())
if tmp==0:
if len(lis)==0:
sys.stdout.write('0')
else:
sys.stdout.write(str(heapq.heappop(lis)))
sys.stdout.write('\n')
else:
heapq.heappush(lis, tmp)
4. 반례
@leethyun 님
9
1
2
2
3
0
0
0
0
0
답:
1
2
2
3
0
반응형