첫 번째 시도

  • 나이가 같으면 입력된 순서가 유지되면서 정렬되어야 하는데, 일반 sort() 함수는 불안정 정렬이므로 순서가 변경될 수 있다.
  • 해당 사실을 몰랐고, 안정 정렬을 해주는 stable_sort() 함수가 있는지 검색해서 알게 되었다.
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
29
30
31
32
33
34
35
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>

#define MAX_N 1000000

using namespace std;

vector<pair<int, string> > input;

bool compare(pair<int, string> a, pair<int, string> b) {
	if (a.first == b.first) {
		return false;
	}
	else
		return a.first < b.first;
}

int main() {
	freopen("in.txt", "r", stdin);

	int N;
	cin >> N;
	for (int i = 0; i < N; i++) {
		int a;
		string b;
		cin >> a >> b;
		input.push_back(make_pair(a, b));
	}
	stable_sort(input.begin(), input.end(), compare);
	for (int i = 0; i < N; i++) {
		cout << input[i].first << " " << input[i].second << "\n";
	}
}