본문 바로가기
백준BOJ/정렬, 투 포인터

백준 10814 나이순 정렬

by 이일아 2022. 6. 4.
반응형

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

 

10814번: 나이순 정렬

온라인 저지에 가입한 사람들의 나이와 이름이 가입한 순서대로 주어진다. 이때, 회원들을 나이가 증가하는 순으로, 나이가 같으면 먼저 가입한 사람이 앞에 오는 순서로 정렬하는 프로그램을

www.acmicpc.net

 

 

 

EXPLANATION

stable_sort를 사용하면 된다. (설명)

 

 

CODE

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

using namespace std;

bool compare(pair<int,string> a, pair<int,string> b){
    return a.first < b.first;
}

int main(){
    int N;
    cin >> N;
    pair<int, string> input;
    vector<pair<int,string>> arr;
    for(int i = 0; i < N; i++){
        cin >> input.first >> input.second;
        arr.push_back(input);
    }

    stable_sort(arr.begin(), arr.end(), compare);
    for(int i = 0; i < N; i++){
        cout << arr[i].first << " " << arr[i].second << "\n";
    }
    return 0;
}

 

반응형

댓글