공부/C++

[우선순위 큐] priority_queue 사용자 정의 함수

래울 2023. 12. 27. 19:47

- 우선순위 큐에서 cmp 구조체에서 operator() 연산자 오버로딩을 통해 사용가능하다.

 * 반환 값이 true 이면, swap 한다.

#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include <list>
#include <cmath>
using namespace std;

int N;
typedef struct yx
{
	int y;
	int x;
} yx;

struct cmp {
	bool operator()(yx a, yx b)
	{
		if (a.y == b.y)
		{
			return a.x > b.x;
		}
		else
		{
			return a.y > b.y;
		}
	}
};

priority_queue<yx, vector<yx>, cmp> q;
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> N;
	
	int x, y;
	for (int i = 0; i < N; ++i)
	{
		cin >> x >> y;
		q.push({ y, x });
	}

	while (!q.empty())
	{
		cout << q.top().x << " " << q.top().y << "\n";
		q.pop();
	}
	return 0;
}