Algorithm/Problem Solve

[백준 11650번] 좌표 정렬하기 (vector sort compare사용)

아네스 2020. 12. 13. 22:21
반응형

#include <bits/stdc++.h>

using namespace std;

int N;
vector<pair<int, int>> v;

bool compare(pair<int,int>& a,pair<int,int>& b)
{
	if (a.first == b.first)
	{
		return a.second < b.second;
	}
	else {
		return a.first < b.first;
	}
}
int main(void)
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
    cin >> N;
	for (int i = 0; i < N; i++)
	{
		int a, b;
		cin >> a >> b;
		v.push_back({ a,b });
	}
	sort(v.begin(), v.end(), compare);
	
	for (int i = 0; i < N; i++)
	{
		cout << v[i].first << ' ' << v[i].second << '\n';
	}
}
반응형