목차

가운데를 말해요

ps
링크acmicpc.net/…
출처BOJ
문제 번호1655
문제명가운데를 말해요
레벨골드 2
분류

우선순위큐

시간복잡도O(nlogn)
인풋사이즈n<=100,000
사용한 언어Python
제출기록35056KB / 244ms
최고기록184ms
해결날짜2021/05/07
태그

22단계

풀이

코드

"""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()