문제

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;
}

+ Recent posts