알고리즘 풀이

좌표 정렬하기

남생이야 2024. 8. 10. 03:07

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

 

  기본으로 주어진 sort 함수에서 compare 함수를 따로 정의하여 풀었다. 처음엔 문제를 좀 대충 읽어서 이론상은 맞았는데 왜 안되지라는 막막함에 다시 읽어보니 x==y라는 전제가 있었다.. 

 

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
	int n; 
	cin >> n;

	vector<pair<int, int>> arr;

	for (int i = 0; i < n; i++)
	{
		int x, y; 
		cin >> x >> y; 
		arr.push_back({ x, y });
	}

	sort(arr.begin(), arr.end(), [](pair<int, int> a, pair<int, int> b) {
		
		if (a.first == b.first)
			return a.second < b.second;
		
		return a.first < b.first; 

		});

	for (long i = 0; i < arr.size(); i++)
	{
		printf("%d %d\n", arr[i].first, arr[i].second);
	}


	return 0;
}