문제

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

 

11650번: 좌표 정렬하기

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

www.acmicpc.net

 

풀이

기본적인 sort 정렬(2개짜리) 문제였다. 

이런 정렬 같은 경우 sort()를 활용하는 것과, priority_queue + bool operator() 활용하는 두 가지 모두 알아두자.

 

코드

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int N;
vector<pair<int, int>> arr;
bool cmp(pair<int,int> a,pair<int,int> b) {
	if (a.first == b.first) {
		return a.second < b.second;
	}
	return a.first < b.first;
}

int main() {
	cin >> N;

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



	}

	sort(arr.begin(), arr.end(), cmp);

	for (int i = 0; i < N; i++) {
		cout << arr[i].first << " " << arr[i].second << '\n';

	}


	return 0;
}

'알고리즘 공부 > 백준' 카테고리의 다른 글

[백준] 최대 힙 (11279/C++)  (0) 2022.01.26
[백준] 최소 힙 (1927/C++)  (0) 2022.01.26
[백준] 덩치 (7568/C++)  (0) 2022.01.23
[백준] 수 정렬하기 3 (10989/C++)  (0) 2022.01.23
[백준] 나이순 정렬 (10814/C++)  (0) 2022.01.22

+ Recent posts