https://www.acmicpc.net/problem/1655
문제를 읽고 heapq를 사용해야겠다는 생각은 들었지만 코드를 어떻게 짜야할지 모르겠어서 검색을 했다...
힙을 두 개 써서 n을 분배한다.
왼쪽 힙은 최대 힙으로 정렬하고, 오른쪽 힙은 최소 힙으로 정렬하면 왼쪽 힙의 첫번째 요소는 항상 중앙값이 된다.
import sys
input = sys.stdin.readline
import heapq
n = int(input())
left_heap, right_heap = [], []
for i in range(n):
x = int(input())
if len(left_heap) == len(right_heap):
heapq.heappush(left_heap, -x)
else:
heapq.heappush(right_heap, x)
if len(left_heap) >= 1 and len(right_heap) >= 1 and left_heap[0] * -1 > right_heap[0]:
max_num = heapq.heappop(left_heap) * -1
min_num = heapq.heappop(right_heap)
heapq.heappush(left_heap, min_num * -1)
heapq.heappush(right_heap, max_num)
print(left_heap[0] * -1)