알고리즘/BOJ
[BOJ] 11650. 좌표 정렬하기
hyerann
2020. 3. 25. 18:27
좌표를 저장할 클래스를 만들고 vector로 좌표들을 관리하였습니다.
그리고 이를 요구사항에 맞는 비교 함수를 정의하여 정렬하였습니다.
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Location {
public:
int x;
int y;
Location(int _x, int _y) {
x = _x;
y = _y;
}
};
vector<Location> locations;
bool cmp(Location l1, Location l2) {
if(l1.x == l2.x) {
return l1.y < l2.y;
}
return l1.x < l2.x;
}
int main() {
cin.tie(0); cout.tie(0);
ios::sync_with_stdio(0);
int N;
cin >> N;
for(int i=0; i<N; i++) {
int x, y;
cin >> x >> y;
locations.push_back(Location(x, y));
}
sort(locations.begin(), locations.end(), cmp);
for(int i=0; i<N; i++) {
cout << locations[i].x << ' ' << locations[i].y << '\n';
}
return 0;
}