본문 바로가기
Algorithm/Baekjoon

11279_최대 힙

by 모너아링 2023. 2. 3.

▶실버 2

풀이

결론적으로 최댓값을 구하는 것이기 때문에 우선순위 큐를 이용한다.

연산 값이 0이며 우선순위 큐가 empty가 아닐 경우 pop,

연산 값이 양수일 경우 push

코드

#include <iostream>
#include <cmath>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
using namespace std;

priority_queue <int> pq;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int n;
	cin >> n;

	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		if (x == 0) {
			if (pq.empty()) {
				cout << 0 << "\n";
			}
			else {
				cout << pq.top() << "\n";
				pq.pop();
			}
		}
		else {
			pq.push(x);
		}
	}
}

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

1874_스택 수열  (0) 2023.02.08
1620_나는야 포켓몬 마스터 이다솜  (2) 2023.02.03
10799_쇠막대기  (0) 2023.02.02
17478_재귀함수가 뭔가요?  (0) 2023.01.22
2156_포도주 시식  (0) 2023.01.16