#include <iostream>
#include <algorithm>
using namespace std;
pair<int, string> member[100001] = {};
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n = 0;
cin >> n;
for (int i = 0; i < n; ++i)
cin >> member[i].first >> member[i].second;
stable_sort(member, member + n,
[](pair<int, string> a, pair<int, string> b){
if (a.first < b.first)
return true;
return false;
});
for (int i = 0; i < n; ++i)
cout << member[i].first << " " << member[i].second << '\n';
return 0;
}
sort() 기존 순서를 보장하지 않고
stable_sort()는 기존 순서를 보장한다.
나이가 같을 경우 입력순으로 정렬하기 때문에
stable_sort()를 사용해야 하는 문제
https://www.acmicpc.net/problem/10814
10814번: 나이순 정렬
온라인 저지에 가입한 사람들의 나이와 이름이 가입한 순서대로 주어진다. 이때, 회원들을 나이가 증가하는 순으로, 나이가 같으면 먼저 가입한 사람이 앞에 오는 순서로 정렬하는 프로그램을
www.acmicpc.net
'알고리즘 공부' 카테고리의 다른 글
백준 11866번 요세푸스 문제 0 C++ (0) | 2023.09.20 |
---|---|
백준 11650번 좌표 정렬하기 C++ (0) | 2023.09.19 |
백준 1181번 단어 정렬 C++ (0) | 2023.09.11 |
백준 11050번 이항 계수 1 C++ (0) | 2023.09.11 |
백준 10989번 수 정렬하기 3 C++ (0) | 2023.09.11 |