Skip to content

Commit

Permalink
[Silver II] Title: 최대 힙, Time: 104 ms, Memory: 37044 KB -BaekjoonHub
Browse files Browse the repository at this point in the history
  • Loading branch information
alswo1212 committed Aug 16, 2024
1 parent 24be922 commit d8b4009
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
35 changes: 35 additions & 0 deletions 백준/Silver/11279. 최대 힙/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# [Silver II] 최대 힙 - 11279

[문제 링크](https://www.acmicpc.net/problem/11279)

### 성능 요약

메모리: 37044 KB, 시간: 104 ms

### 분류

자료 구조, 우선순위 큐

### 제출 일자

2024년 8월 16일 17:16:42

### 문제 설명

<p>널리 잘 알려진 자료구조 중 최대 힙이 있다. 최대 힙을 이용하여 다음과 같은 연산을 지원하는 프로그램을 작성하시오.</p>

<ol>
<li>배열에 자연수 x를 넣는다.</li>
<li>배열에서 가장 큰 값을 출력하고, <span style="line-height:1.6em">그 값을 배열에서 제거한다. </span></li>
</ol>

<p><span style="line-height:1.6em">프로그램은 처음에 비어있는 배열에서 시작하게 된다.</span></p>

### 입력

<p>첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0이라면 배열에서 가장 큰 값을 출력하고 그 값을 배열에서 제거하는 경우이다. 입력되는 자연수는 2<sup>31</sup>보다 작다.</p>

### 출력

<p>입력에서 0이 주어진 횟수만큼 답을 출력한다. 만약 배열이 비어 있는 경우인데 가장 큰 값을 출력하라고 한 경우에는 0을 출력하면 된다.</p>

13 changes: 13 additions & 0 deletions 백준/Silver/11279. 최대 힙/최대 힙.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from heapq import heappop, heappush
import sys
heap = []
result = []
n = int(sys.stdin.readline())
for _ in range(n):
num = int(sys.stdin.readline())
if num == 0:
result.append(-heappop(heap) if len(heap) else 0)
continue
heappush(heap, -num)

print(*result,sep='\n')

0 comments on commit d8b4009

Please sign in to comment.