11286번: 절댓값 힙

문제접근🤔


놓쳤던 부분😅


코드😁


2532 KB

16 ms

#include <iostream>
#include <queue>
#include <cmath>
using namespace std;

struct cmp {
	bool operator() (int a, int b){
		if (abs(a) == abs(b))
			return a > b;
		return abs(a) > abs(b);
	}
};

int main(void)
{
	priority_queue<int, vector<int>, cmp> pq;
	int n;
	int input;

	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

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