ps | |
---|---|
링크 | acmicpc.net/… |
출처 | BOJ |
문제 번호 | 1655 |
문제명 | 가운데를 말해요 |
레벨 | 골드 2 |
분류 |
우선순위큐 |
시간복잡도 | O(nlogn) |
인풋사이즈 | n<=100,000 |
사용한 언어 | Python |
제출기록 | 35056KB / 244ms |
최고기록 | 184ms |
해결날짜 | 2021/05/07 |
태그 |
"""Solution code for "BOJ 1655. 가운데를 말해요".
- Problem link: https://www.acmicpc.net/problem/1655
- Solution link: http://www.teferi.net/ps/problems/boj/1655
"""
import heapq
import sys
INF = float('inf')
def main():
N = int(sys.stdin.readline())
max_heap = []
min_heap = []
median = INF
for _ in range(N):
num = int(sys.stdin.readline())
if num < median:
heapq.heappush(max_heap, -num)
if len(max_heap) > len(min_heap) + 1:
heapq.heappush(min_heap, -heapq.heappop(max_heap))
else:
heapq.heappush(min_heap, num)
if len(max_heap) < len(min_heap):
heapq.heappush(max_heap, -heapq.heappop(min_heap))
median = -max_heap[0]
print(median)
if __name__ == '__main__':
main()