[알고리즘] 백준 11651 - 좌표 정렬하기 2

홍예주·2022년 9월 6일
0

알고리즘

목록 보기
74/92

1. 문제

2차원 평면 위의 점 N개가 주어진다. 좌표를 y좌표가 증가하는 순으로, y좌표가 같으면 x좌표가 증가하는 순서로 정렬한 다음 출력하는 프로그램을 작성하시오.

2. 입력

첫째 줄에 점의 개수 N (1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개의 줄에는 i번점의 위치 xi와 yi가 주어진다. (-100,000 ≤ xi, yi ≤ 100,000) 좌표는 항상 정수이고, 위치가 같은 두 점은 없다.

3. 풀이

11650과 반대로 정렬하면 된다.

4. 코드

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

bool compare(pair<int, int> a, pair<int, int> b) {
	if (a.second==b.second) {
		return a.first < b.first;
	}
	return a.second < b.second;
}

int main()
{
	cin.tie(0);
	cout.tie(0);
	cin.sync_with_stdio(0);

	int n;
	cin >> n;
	pair<int, int> tmp;
	vector<pair<int, int>> arr;

	for (int i = 0; i < n; i++) {
		cin >> tmp.first >> tmp.second;
		arr.push_back(tmp);
	}

	sort(arr.begin(), arr.end(), compare);
	
	for (int i = 0; i < n; i++) {
		cout << arr[i].first << ' ' << arr[i].second << "\n";
	}


	return 0;
}
profile
기록용.

0개의 댓글