Files
python/TangDou/CSP-J/J1_2019/5_Prepare.cpp

33 lines
784 B
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 10;
int b[N] = {5, 3, 5, 1}, cnt[N], ord[N];
int main() {
int n = 4;
int M = -1;
for (int i = 0; i < n; i++) M = max(M, b[i]);
//原始数组
for (int i = 0; i < n; i++) printf("%d ", b[i]);
puts("");
//桶数组
for (int i = 0; i < n; i++) cnt[b[i]]++;
for (int i = 0; i <= M; i++) printf("%d ", cnt[i]);
puts("");
//桶数组前缀和:cnt[]:
for (int i = 0; i < M; i++) cnt[i + 1] += cnt[i]; //前缀和
for (int i = 0; i <= M; i++) printf("%d ", cnt[i]);
puts("");
for (int i = 0; i < n; i++) ord[--cnt[b[i]]] = i;
for (int i = 0; ord[i]; i++) printf("%d ", ord[i]);
return 0;
}