debug cant solve

whitehousechef·2024년 3월 28일

https://www.acmicpc.net/problem/2696

initial

import heapq

t = int(input())
for _ in range(t):
    max_heap = []
    min_heap = []
    heapq.heapify(max_heap)
    heapq.heapify(min_heap)

    n = int(input())
    lst = list(map(int, input().split()))
    ans = []
    n=len(lst)
    
    for i in range(n):
        if len(max_heap) == len(min_heap):
            heapq.heappush(max_heap, -lst[i])
        else:
            heapq.heappush(min_heap, lst[i])

        if max_heap and min_heap and -max_heap[0] > min_heap[0]:
            big = heapq.heappop(max_heap)
            small = heapq.heappop(min_heap)
            heapq.heappush(max_heap, small)
            heapq.heappush(min_heap, big)

        if i % 2 == 0:
            ans.append(-max_heap[0])

    print(*ans)

0개의 댓글