728x90
https://www.acmicpc.net/problem/10814
10814번: 나이순 정렬
온라인 저지에 가입한 사람들의 나이와 이름이 가입한 순서대로 주어진다. 이때, 회원들을 나이가 증가하는 순으로, 나이가 같으면 먼저 가입한 사람이 앞에 오는 순서로 정렬하는 프로그램을 �
www.acmicpc.net
pair와 정렬의 연습 문제이다.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool comp(pair<int, string> a, pair<int, string> b){
return a.first < b.first;
}
pair<int, string> arr[100001];
int n, s, old;
string name;
int main() {
cin >> n;
for(int i=0; i<n; i++) {
cin >> old >> name;
arr[i] = make_pair(old, name);
}
stable_sort(arr, arr+n, comp);
for(int i=0; i<n; i++)
cout << arr[i].first << ' ' << arr[i].second << '\n';
}
stable_sort란
- 구조체처럼 여러 데이터타입이 함께 있을 때
하나의 요소로 정렬을 하고, 다른 요소는 순서를 유지하고 싶을 때사용하는 정렬이다. (sort와 매우 비슷)
728x90
'알고리즘 > 문제' 카테고리의 다른 글
[백준] 19939. 박 터뜨리기 (2020 정올 초등부 1번) <C++> (1) | 2020.09.22 |
---|---|
[백준] 1676. 팩토리얼 0의 개수 <C++> (0) | 2020.09.05 |
[백준] 11650. 좌표 정렬하기(1, 2) <C++> (0) | 2020.09.04 |
[NYPC] [2019 예선] 1. 최대 HP (0) | 2020.08.30 |
[codeground] Practice. 11. 개구리 뛰기 (0) | 2020.08.26 |
댓글