• toc {:toc}

문제 ν™•μΈν•˜κΈ°

풀이

sort ν•¨μˆ˜μ˜ compare λ₯Ό μ–΄λ–»κ²Œ μ‘°μ ˆν•˜λŠ”κ°€μ— λŒ€ν•œ λ¬Έμ œμ΄λ‹€. 두 개의 쌍일 경우 pair, 3 개의 쌍일 κ²½μš°μ—λŠ” tuple 을 μ‚¬μš©ν•΄μ„œ ν’€μ΄ν•œλ‹€.

두 λ¬Έμ œμ—μ„œ λ‹€λ₯Έ 쑰건은 첫 번째 숫자λ₯Ό λ¨Όμ € λΉ„κ΅ν•˜λŠ”κ°€, 두 번째 숫자λ₯Ό λ¨Όμ € λΉ„κ΅ν•˜λŠ”κ°€μ— λŒ€ν•œ 것이닀. λ•Œλ¬Έμ— compare ν•¨μˆ˜μ—μ„œ 차이λ₯Ό 두어 ν’€μ΄ν•œλ‹€. < 인 경우 μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ •λ ¬, > 인 경우 λ‚΄λ¦Όμ°¨μˆœμœΌλ‘œ μ •λ ¬ν•œλ‹€.

#include <bits/stdc++.h>
using namespace std;
vector <pair<int, int>> vec;
 
bool compare(const pair<int, int> &a, const pair<int, int> &b){
    if (a.first == b.first){
        return a.second < b.second;
    }
    else{
        return a.first < b.first;
    }
}
 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
 
    int n, x, y;
 
    cin >> n;
 
    for(int i=0; i<n; i++){
        cin >> x >> y;
        vec.push_back({x, y});
    }
    sort(vec.begin(), vec.end(), compare);
 
    for(int i=0; i<n; i++){
        cout << vec[i].first << " " << vec[i].second << '\n';
    }
 
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
vector <pair<int, int>> vec;
 
bool compare(const pair<int, int> &a, const pair<int, int> &b){
    if (a.second == b.second){
        return a.first < b.first;
    }
    else{
        return a.second < b.second;
    }
}
 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
 
    int n, x, y;
 
    cin >> n;
 
    for(int i=0; i<n; i++){
        cin >> x >> y;
        vec.push_back({x, y});
    }
    sort(vec.begin(), vec.end(), compare);
 
    for(int i=0; i<n; i++){
        cout << vec[i].first << " " << vec[i].second << '\n';
    }
 
    return 0;
}