문제 접근
vector<pair<int,string>> 으로 접근했다가 우선 순위가 있다는 걸 알고 tuple 로 접근했다. tuple 은 여러개의 타입을 하나로 접근할 수 있도록 한 것이다.
문제를 풀고 나서 다른 풀이를 보니 algorithm 헤더에 stable_sort 라는 함수가 있었다. 찾아보니 하나의 요소로 정렬을 한 뒤 다른 요소들의 정렬 순서가 정렬 전과 같이 유지되도록 하는 함수였다. 한마디로 나이에 대해 정렬한 뒤 먼저 들어온 순서대로 정렬된다는 것이다.
오늘도 지식이 늘었다.
문제 풀이
#include <iostream>
#include <string>
#include <vector>
#include <tuple>
#include <algorithm>
using namespace std;
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N,cnt,age,priority=1;
string name;
vector<tuple<int,int,string>> list;
cin>>N;
for(int i=0; i<N; ++i)
{
cin>>age>>name;
list.push_back(make_tuple(age,priority++,name));
}
sort(list.begin(),list.end());
for(int i=0; i<N; ++i)
cout<<get<0>(list[i])<<" "<<get<2>(list[i])<<'\n';
return 0;
}
실행 결과
결과
'개발 > 알고리즘' 카테고리의 다른 글
백준 9095번 : 1,2,3 더하기 (0) | 2020.08.14 |
---|---|
백준 1012번 : 유기농 배추 (0) | 2020.07.20 |
자료구조 : Dequeue (Feat.백준 10866 : 덱) (0) | 2020.07.12 |
자료구조 : Queue (Feat.백준 10845번 : 큐) (0) | 2020.07.12 |
자료구조 : Stack (Feat. 백준 10828 : 스택) (0) | 2020.07.12 |