백준 algorithm

백준 11651-좌표 정렬하기 2

cosmohoo 2020. 2. 5. 15:13
반응형

문제 설명

 

pair와 vector를 사용하여 풀수 있다.

더불어 sort함수를 사용하면 간단하게 풀 수 있다. 

 

#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    
    int N;
    cin>>N;
    
    pair <int , int > v;
    vector <pair<int , int >> vp;
    
    int x,y;
    for(int i=0; i<N; i++)
    {
        cin>>x;
        cin>>y;
        v=make_pair(y, x);
        vp.push_back(v);
    }

    sort(vp.begin(), vp.end());
    
    for(int i=0; i<N; i++)
    {
        cout<<vp[i].second <<" "<<vp[i].first<<'\n';
    }
}
반응형