본문 바로가기
PS(Problem Solving)/백준_BOJ

[백준] 11279번 - 최대 힙 (C++/파이썬) 문제 및 풀이

by 초코칩프라푸치노 2021. 4. 14.

문제) 백준 - 우선순위 큐 (Priority Queue) - 최대 힙

-> www.acmicpc.net/problem/11279

 

11279번: 최대 힙

첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가

www.acmicpc.net

 

 

 

C++ 소스 코드)

 

#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;
}
view raw 11279.cpp hosted with ❤ by GitHub

파이썬 소스 코드)

 

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)
view raw 11279.py hosted with ❤ by GitHub

 

 

반응형

댓글