1번 문제.
https://www.acmicpc.net/problem/11279
-> 최대 힙
import sys
import heapq
# 연산 갯수 입력
n = int(sys.stdin.readline().rstrip())
# 자연수들을 넣은 배열
num_list = []
for _ in range(n):
# x 값을 하나씩 입력 받음
x = int(sys.stdin.readline().rstrip())
# 만약 x 값이 1보다 크거나 같다면(자연수)
if x >= 1:
heapq.heappush(num_list, -(x))
print(num_list)
# 그렇지 않고,
else:
# 자연수 배열이 1개 이상 존재할 때
if len(num_list) >= 1:
print(-1 * heapq.heappop(num_list))
# 비어있을 때 0을 출력
else:
print(0)
=======================================================
2번 문제.
https://www.acmicpc.net/problem/1927
-> 최소 힙
import heapq
import sys
n = int(sys.stdin.readline().rstrip())
num_list = []
for _ in range(n):
x = int(sys.stdin.readline().rstrip())
if x >= 1:
heapq.heappush(num_list, x)
else:
if len(num_list) >= 1:
print(heapq.heappop(num_list))
else:
print(0)
=======================================================
3번 문제.
https://www.acmicpc.net/problem/11286
-> 절댓값 힙
import heapq
import sys
n = int(sys.stdin.readline().rstrip())
num_list = []
for _ in range(n):
x = int(sys.stdin.readline().rstrip())
if x != 0:
heapq.heappush(num_list, (abs(x), x))
else:
if len(num_list) >= 1:
print(heapq.heappop(num_list)[1])
else:
print(0)
=======================================================
오늘은 여기까지