Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 세그먼트 트리
- BFS
- 크루스칼
- DP
- 메모리풀
- SSAFY
- 큐
- 중복 순열
- 조합
- 클래스
- BeautifulSoup
- 분할 정복
- 피보나치 수
- 순열
- 백트래킹
- 문자열
- 재귀
- 스택
- MST
- 링크드리스트
- dfs
- Knapsack
- 시뮬레이션
- 비트마스크
- 그리디
- lis
- 우선순위 큐
- 빠른 입출력
- 이분 탐색
- 완전 탐색
Archives
- Today
- Total
작심 24/7
[백준] 10814번 나이순 정렬 본문
10814번: 나이순 정렬
온라인 저지에 가입한 사람들의 나이와 이름이 가입한 순서대로 주어진다. 이때, 회원들을 나이가 증가하는 순으로, 나이가 같으면 먼저 가입한 사람이 앞에 오는 순서로 정렬하는 프로그램을 �
www.acmicpc.net
vector의 pair기능과 compare함수를 이용하면 쉽게 풀 수 있는 문제이다.
compare 없이 그냥 sort를 해주면 vector의 first를 기준으로 정렬되기 때문에
second.first(나이)가 같으면 second.second(인덱스 값)이 증가하는 순으로 정렬시키도록
compare함수를 작성해주면 끝
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
vector < pair <string, pair <int, int> > > v;
bool compare(pair <string, pair <int, int> > a, pair <string, pair <int, int> > b) {
if (a.second.first == b.second.first) {
return a.second.second < b.second.second;
}
else return a.second.first < b.second.first;
}
int main() {
int N;
cin >> N;
int age;
string name;
for (int n = 0; n < N; n++) {
cin >> age >> name;
v.push_back(pair<string, pair<int, int> >(name, pair<int, int>(age, n)));
}
sort(v.begin(), v.end(), compare);
for (int i = 0; i < N; i++)cout << v[i].second.first << " " << v[i].first << "\n";
return 0;
}
'백준' 카테고리의 다른 글
[백준] 15650번 N과 M - 2 (C++, JAVA) (0) | 2020.05.24 |
---|---|
[백준] 15649번 N과 M - 1 (C++, JAVA) (0) | 2020.05.24 |
[백준] 2108번 통계학 (0) | 2020.05.22 |
[백준] 10989번 수 정렬하기 3 (0) | 2020.05.22 |
[백준] 11729번 하노이 탑 이동 순서 (0) | 2020.05.20 |
Comments