문제
https://www.acmicpc.net/problem/10814
10814번: 나이순 정렬
온라인 저지에 가입한 사람들의 나이와 이름이 가입한 순서대로 주어진다. 이때, 회원들을 나이가 증가하는 순으로, 나이가 같으면 먼저 가입한 사람이 앞에 오는 순서로 정렬하는 프로그램을
www.acmicpc.net
풀이
구조체를 만들어 각 입력에 순서표를 달아주었다.
이를 통하여 Sort() + 우선순위 조건을 추가하여 정렬하는 문제였다.
코드
#include<vector>
#include<iostream>
#include<queue>
#include<algorithm>
#include<string>
using namespace std;
int N;
struct human{
int num;
int age;
string name;
};
bool cmp(human a,human b) {
if (a.age == b.age) {
return a.num < b.num;
}
return a.age < b.age;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> N;
vector<human> arr;
for (int i = 0; i < N; i++) {
int a;
string b;
cin >> a >> b;
arr.push_back({ i,a,b });
}
sort(arr.begin(), arr.end(), cmp);
for (int i = 0; i < N; i++) {
cout << arr[i].age << " " << arr[i].name << '\n';
}
return 0;
}
'알고리즘 공부 > 백준' 카테고리의 다른 글
[백준] 덩치 (7568/C++) (0) | 2022.01.23 |
---|---|
[백준] 수 정렬하기 3 (10989/C++) (0) | 2022.01.23 |
[백준] 균형잡힌 세상 (4949/C++) (0) | 2022.01.22 |
[백준] 설탕 배달 ( 2839 / C++) (0) | 2022.01.20 |
[백준] 나무 자르기 (2805/C++) (0) | 2022.01.16 |