Files
python/TangDou/CSP-J/J1_2019/5.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

41 lines
1.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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