# 생각

stable sort를 이용하여 x, y를 비교 정렬할 수 있다

 

또한 pair의 정렬 순서는 first를 먼저 비교하기 때문에 둘의 자리를 바꿔서 대입한다면

간단하게 원하는 값을 출력할 수도 있다

 

 

# stable_sort를 이용한 코드

#include <bits/stdc++.h>
using namespace std;

bool compareX(pair<int, int> a, pair<int, int> b)
{
	return a.first < b.first;
}
bool compareY(pair<int, int> a, pair<int, int> b)
{
	return a.second < b.second;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int n;
	cin >> n;
	vector<pair<int, int>> ans(n);
	for (auto& i : ans)
	{
		cin >> i.first >> i.second;
	}
	stable_sort(ans.begin(), ans.end(), compareX);
	stable_sort(ans.begin(), ans.end(), compareY);
	for (auto i : ans)
	{
		cout << i.first << ' ' << i.second << '\n';
	}
}

 

 

# pair의 성질을 이용한 코드

#include <bits/stdc++.h>
using namespace std;

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

	int n;
	cin >> n;
	pair<int, int> ans[100001];
	for (int i = 0; i < n; i++)
	{
		cin >> ans[i].second >> ans[i].first;
	}
	sort(ans, ans + n);
	for (int i = 0; i < n; i++)
	{
		cout << ans[i].second << ' ' << ans[i].first << '\n';
	}
}

 


 

'Algorithm > BOJ' 카테고리의 다른 글

[BOJ] 2750_수 정렬하기 with cpp  (0) 2022.02.28
[BOJ] 18808_스티커 붙이기 with cpp  (0) 2022.02.21
[BOJ] 11650_좌표 정렬하기 with cpp  (0) 2022.02.19
[BOJ] 10814_나이순 정렬 with cpp  (0) 2022.02.18
[BOJ] 15683_감시 with cpp  (0) 2022.02.17

+ Recent posts