정렬(Sort)

백준 10814: 나이순 정렬

cepiloth 2020. 11. 28. 18:26
반응형

www.acmicpc.net/problem/10814

 

10814번: 나이순 정렬

온라인 저지에 가입한 사람들의 나이와 이름이 가입한 순서대로 주어진다. 이때, 회원들을 나이가 증가하는 순으로, 나이가 같으면 먼저 가입한 사람이 앞에 오는 순서로 정렬하는 프로그램을

www.acmicpc.net

 

멀티맵(multimap)의 특성인 중복을 허용하면서 정렬 되는 점을 이용하여서 풀이

#include <iostream>
#include <algorithm> // min
#include <functional>
#include <math.h>
#include <string>
#include <string.h>
#include <vector>
#include <map>
#include <cmath>
#include <sstream>

#define M_PI 3.14159265358979323846

using namespace std;

int main()
{
	std::ios::sync_with_stdio(false);

	// 중복을 허용하면서 정렬이 되니 좋구먼
    // 이문제가 맞은 이유는 입력은 가입한 순서로 주어지고 출력도 가입한 순서로 나오면 되기 때문에
	multimap<int, string> m;

	int n; cin >> n;

	while (n--) {
		int d; cin >> d;
		string s; cin >> s;
		m.insert(pair<int, string>(d, s));
	}

	multimap<int, string>::iterator iter;
	for (iter = m.begin(); iter != m.end(); iter++) {
		cout << iter->first << " " << iter->second << "\n";
	}
	return 0;
}
반응형