Skip to content

Commit

Permalink
2024-06-01 중앙값구하기
Browse files Browse the repository at this point in the history
  • Loading branch information
seongwon030 committed Jun 1, 2024
1 parent 0d5e4b2 commit 5e80ca5
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions seongwon030/큐/중앙값구하기.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import heapq

def push_num(num):
# max_heap과 min_heap 크기 같으면 max_heap에 넣음
if len(max_heap) == len(min_heap):
heapq.heappush(max_heap, -num)
else:
# 크기가 다르면 이전에 max_heap에 넣은 것이므로
# min_heap에 넣는다
heapq.heappush(min_heap, num)

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

T = int(input())
for i in range(T):
max_heap = []
min_heap = []
re = []

n = int(input())
if n%2 == 0:
print(n//2)
else:
print(n//2+1)

for j in range(n//10 + 1):
arr = list(map(int,input().split()))
for k in range(len(arr)):
push_num(arr[k])
if k%2 == 0:
re.append(-max_heap[0])
if n%2 == 0:
re.append(-max_heap[0])

cnt = 0
for p in range(len(re)):
cnt += 1
print(re[p],end=' ')
if cnt%10 == 0:
print()
print()

0 comments on commit 5e80ca5

Please sign in to comment.