You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.2 KiB

2 years ago
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 10000000;
const int maxs = 10000;
int n;
/*
6
1 2 3 1 2 3
1 2 1 2 1 2
3
4 4 3
3 2 3
*/
unsigned a[N], b[N], res[N], ord[N], cnt[maxs + 1];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d%d", &a[i], &b[i]);
memset(cnt, 0, sizeof(cnt));
for (int i = 0; i < n; i++) ++cnt[b[i]]; // ① 利用cnt数组统计数量个位数桶计数
for (int i = 0; i < maxs; i++) cnt[i + 1] += cnt[i]; //桶计数前缀和
//先--再使用是因为ord的下标是从0开始的
for (int i = 0; i < n; i++) ord[--cnt[b[i]]] = i; //② 记录初步排序结果,个位数升序排序
memset(cnt, 0, sizeof(cnt));
for (int i = 0; i < n; i++)
++cnt[a[i]]; //③ 利用cnt数组统计数量十位数桶计数
for (int i = 0; i < maxs; i++)
cnt[i + 1] += cnt[i]; //十位数桶计数前缀和
for (int i = n - 1; i >= 0; i--)
res[--cnt[a[ord[i]]]] = ord[i]; //④ 记录最终排序结果
for (int i = 0; i < n; i++)
printf("%d %d\n", a[res[i]], b[res[i]]); //⑤
return 0;
}