Notice
Recent Posts
Recent Comments
Link
나의 개발일지
[백준] 1655 가운데를 말해요 [Python, 파이썬] 본문
- 문제 : https://www.acmicpc.net/problem/1655
- 🔑 최대 힙과 최소 힙을 함께 사용
import sys
from heapq import heappush, heappop
input = sys.stdin.readline
n = int(input())
num = int(input())
print(num)
minHeap, maxHeap = [],[-num]
for i in range(n-1):
num = int(input())
if num > -maxHeap[0]:
heappush(minHeap, num)
else:
heappush(maxHeap, -num)
if len(minHeap) > len(maxHeap):
temp = heappop(minHeap)
heappush(maxHeap, -temp)
elif len(minHeap)+1 < len(maxHeap):
temp = heappop(maxHeap)
heappush(minHeap, -temp)
print(-maxHeap[0])
'백준' 카테고리의 다른 글
[백준] 16954 움직이는 미로 탈출 [Python, 파이썬] (0) | 2023.11.04 |
---|---|
[백준] 1654 랜선 자르기 [Python, 파이썬] (1) | 2023.11.03 |
[백준] 1238 파티 [Python, 파이썬] (1) | 2023.11.01 |
[백준] 5557 1학년 [Python, 파이썬] (1) | 2023.10.24 |
[백준] 24479 알고리즘 수업 - 깊이 우선 탐색 1 [Python, 파이썬] (1) | 2023.10.21 |
Comments