알고리즘/BOJ
[C++] 백준 11651번 좌표 정렬하기2
Jay, Lee
2021. 4. 2. 14:32
sort를 적절히 활용하는 문제
sort에서 compare함수를 조금 잘 정의해야할 것 같다
여기 부분때문에 3번정도 틀린 것 같다.
문제가 y 좌표를 기준으로 비교하고,
y좌표가 같을때는 다시 x좌표를 기준으로 정렬해야되니까
이 부분 신경써서 코드를 작성해야한다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool compare(const pair<int, int> &a, const pair<int, int> &b)
{
if (a.second < b.second) {
return true;
}
else if (a.second == b.second) {
if (a.first < b.first)
return true;
}
return false;
}
int main() {
int testCase;
cin >> testCase;
vector<pair<int, int>> vec(testCase);
for (int i = 0; i < testCase; i++) {
cin >> vec[i].first >> vec[i].second;
}
sort(vec.begin(), vec.end(), compare);
for (int i = 0; i < testCase; i++) {
cout << vec[i].first << " " << vec[i].second << "\n";
}
return 0;
}