본문으로 바로가기

#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<int, string> u, pair<int, string> u1) {
	return u.first < u1.first;
}
int main()
{
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	int n;
	cin >> n;
	vector<pair<int, string>> v1(n);
	for (int i = 0; i < n; i++) {
		cin >> v1[i].first >> v1[i].second;
	}
	stable_sort(v1.begin(), v1.end(), cmp);
	for (int i = 0; i < n; i++) {
		cout << v1[i].first << " " << v1[i].second << "\n";
	}
}
 
Stable Sorting
같은 것이 있는 경우에 정렬하기 전의 순서가 유지되는 정렬 알고리즘을 이용한다.

cmp를 통해 클때만 정렬하도록 하고, 만약 같을 경우는 그대로 반환한다.

기본 sort보다는 연산속도는 느리다.