문제) 백준 - 우선순위 큐 (Priority Queue) - 최대 힙
-> www.acmicpc.net/problem/11279
11279번: 최대 힙
첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가
www.acmicpc.net
C++ 소스 코드)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
#define endl "\n" | |
using namespace std; | |
int main() { | |
ios::sync_with_stdio(false); | |
cout.tie(0); cin.tie(0); | |
priority_queue<int> heap; | |
int n; cin >> n; | |
for (int i = 0; i < n; ++i) { | |
int temp; cin >> temp; | |
if (temp != 0) heap.push(temp); | |
else { | |
if (heap.empty()) cout << 0 << endl; | |
else { | |
cout << heap.top() << endl; | |
heap.pop(); | |
} | |
} | |
} | |
return 0; | |
} |
파이썬 소스 코드)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import heapq | |
import sys | |
input = sys.stdin.readline | |
q = [] | |
n = int(input()) | |
for _ in range(n): | |
temp = int(input()) | |
temp = -temp | |
if temp == 0: | |
if q: | |
print(-heapq.heappop(q)) | |
else: | |
print(0) | |
else: | |
heapq.heappush(q, temp) |
반응형
'PS(Problem Solving) > 백준_BOJ' 카테고리의 다른 글
[백준] 14499번 - 주사위 굴리기 (C++) 문제 및 풀이 (0) | 2021.06.29 |
---|---|
[백준] 1715번 - 카드 정렬하기 (C++/파이썬) 문제 및 풀이 (0) | 2021.04.14 |
[백준] 1927번 - 최소 힙 (C++/파이썬) 문제 및 풀이 (0) | 2021.04.14 |
[백준] 1181번 - 단어 정렬 (C++/파이썬) 문제 및 풀이 (0) | 2021.04.14 |
[백준] 1991번 - 트리 순회 (C++/파이썬) (0) | 2021.04.14 |
댓글