import sys
import heapq
input=sys.stdin.readline
N=int(input())
front,back=[],[]
for i in range(1,N+1):
one = int(input())
if i==1:
heapq.heappush(front,-one)
print(-front[0])
elif i%2!=0:
# 홀수: 앞 배열 맨 뒤
heapq.heappush(front,-one)
if -front[0]>back[0]: # 양 힙의 루트 정렬해주기
f=-heapq.heappop(front)
b=heapq.heappop(back)
heapq.heappush(front,-b)
heapq.heappush(back,f)
print(-front[0])
else:
# 짝수: 양 배열 개수 동일하게 맞추기, 앞 배열 맨 뒤와 뒷 배열 맨 앞 비교
if -front[0]>=one: # front의 루트가 새로 들어오는 것보다 크거나 같다면 (새로 들어온 것이 작은 것)
heapq.heappush(front,-one)
else:
heapq.heappush(back,one)
if len(front)>len(back): # 개수 맞추기
move=-heapq.heappop(front)
heapq.heappush(back,move)
elif len(front)<len(back):
move=heapq.heappop(back)
heapq.heappush(front,move)
print(min(-front[0],back[0]))
뭔가 한번 꼬아서 생각했던 것 같다.
어쨌든 통과하긴 했지만, 그냥 양 힙의 개수를 맞춰주기만 했으면 쉬웠을텐데 ㅋ
import sys
import heapq
if __name__ == "__main__":
n = int(sys.stdin.readline())
max_h, min_h = [], []
# max_h[0][1]값을 기준으로 큰 값은 min_h, 같거나 작은 값은 max_h에 삽입
for _ in range(n):
num = int(sys.stdin.readline())
if len(max_h) == len(min_h):
heapq.heappush(max_h, (-num, num))
else:
heapq.heappush(min_h, (num, num))
if len(max_h) >= 1 and len(min_h) >= 1 and max_h[0][1] > min_h[0][1]:
max_value = heapq.heappop(max_h)[1]
min_value = heapq.heappop(min_h)[1]
heapq.heappush(max_h, (-min_value, min_value))
heapq.heappush(min_h, (max_value, max_value))
print(max_h[0][1])