cgy12306

[백준 BoJ] 11286 - 절댓값 힙 본문

Algorithm/C++

[백준 BoJ] 11286 - 절댓값 힙

cgy12306 2021. 7. 5. 17:26
// https://www.acmicpc.net/problem/11286
// 절댓값 힙

#include<iostream>
#include<queue>

using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	int N;

	priority_queue<pair<int, int>, vector<pair<int,int>>, greater<pair<int, int>>> pq;
	cin >> N;

	for (int i = 0; i < N; i++) {
		int num;
		cin >> num;

		if (num == 0) {
			if (pq.empty()) {
				cout << "0" << "\n";
			}
			else {
				cout << pq.top().first * pq.top().second << "\n";
				pq.pop();
			}
		}
		else {
			if (num < 0) {
				num *= -1;
				pq.push(make_pair(num, -1));
			}
			else {
				pq.push(make_pair(num, 1));
			}
		}
		
	}
}

'Algorithm > C++' 카테고리의 다른 글

[백준 BoJ] 5639 - 이진 검색 트리  (0) 2021.08.06
[백준 BoJ] 1991 - 트리 순회  (0) 2021.08.04
[백준 BoJ] 11729 - 최대 힙  (0) 2021.07.05
[백준 BoJ] 1927 - 최소 힙  (0) 2021.07.05
[백준 BoJ] 5430 - AC  (0) 2021.07.02
Comments