백준

11286번 (silver1)

이야기prog 2025. 1. 14. 20:20

 

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


using namespace std;

struct compare {
	bool operator()(long x, long y) {
		long abs_x = abs(x);
		long abs_y = abs(y);

		if (abs_x == abs_y)
			return x > y;
		else
			return abs_x > abs_y;



	}
};
int main() {
	ios::sync_with_stdio(false);
	cout.tie(NULL);
	cin.tie(NULL);
	int N; long x;
	cin >> N;
	
	priority_queue<long, vector<long>, compare> pq;

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

	

	return 0;

}

https://www.acmicpc.net/problem/11286

문제를 보면 N의 크기가 10만으로 넉넉하단 것을 알수있다.

간단하게 우선순위 큐를 사용해서 힙을 구현하면 된다.