basic-algo-lecture/workbook/0x0E.md at master · encrypted-def/basic-algo-lecture
basic-algo-lecture/workbook/0x0E.md at master · encrypted-def/basic-algo-lecture
바킹독의 실전 알고리즘 강의 자료. Contribute to encrypted-def/basic-algo-lecture development by creating an account on GitHub.
github.com
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
bool compare(pair<int, int> a, pair<int, int> b) {
if (a.second == b.second) {
// 만약 y 값이 같다면
return a.first < b.first; // x 값을 기준으로 오름차순 정렬
}
return a.second < b.second; // y 값을 기준으로 오름차순 정렬
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
pair<int, int>* coors = new pair<int, int>[n];
for (int i = 0; i < n; i++) {
cin >> coors[i].first >> coors[i].second; // x, y
}
sort(coors, coors + n, compare); // 정렬~
for (int i = 0; i < n; i++) {
cout << coors[i].first << " " << coors[i].second << "\n";
}
return 0;
}
sort 메서드에 compare 메서드를 넘겨주면 쉽게 해결할 수 있는 문제이다 ~_~
참고자료:
[바킹독의 실전 알고리즘] 0x0F강 - 정렬 II - YouTube