[C++] 백준 11650번 좌표 정렬하기

xyzw·2025년 3월 14일
0

algorithm

목록 보기
57/61

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

풀이

두 가지 기준으로 정렬하는 문제이다.
algorithm 라이브러리의 sort 함수의 비교함수를 새로 만들어서 사용하면 된다.

코드

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool comp(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()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    
    int n;
    cin >> n;
    
    vector<pair<int, int>> v;
    while(n--) {
        int x, y;
        cin >> x >> y;
        v.push_back({x, y});
    }
    
    sort(v.begin(), v.end(), comp);
    
    for(auto& e : v) cout << e.first << " " << e.second << "\n";
    
    return 0;
}

0개의 댓글