[BOJ] 11279. 최대 힙

동스토리 ㅣ 2020. 10. 18. 19:01

반응형

문제

www.acmicpc.net/problem/11279

 

11279번: 최대 힙

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

www.acmicpc.net

해설

우선순위 큐를 사용

- priority_queue<int> q -> 내림차순에 따라 정렬하는 우선순위 큐 선언

- priotpriority_queue<int,vector<int>,greater<int>> q -> 오름차순에 따라 정렬하는 우선순위 큐 선언

 

코드

#include<iostream>
#include<queue>
#include<vector>

using namespace std;

priority_queue<int> q;

int main() {

	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	int x;
	int min = 0;

	while (n != 0) {
		cin >> x;
		if (x > 0) {
			q.push(x);
		}
		else if (x == 0) {

			if (!q.empty()) {
				cout << q.top() << '\n';
				q.pop();
			}
			else cout << 0 << '\n';
		}
		n--;
	}
	return 0;
}

 

 

 

반응형

'Development > Algorithm' 카테고리의 다른 글

[BOJ] 2667. 단지번호붙이기  (0) 2020.10.22
[BOJ] 9012. 괄호  (0) 2020.10.18
[BOJ] 1927. 최소 힙  (0) 2020.10.18
[BOJ] 10866. 덱  (0) 2020.10.17
[BOJ] 10845. 큐  (0) 2020.10.17