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.

33 lines
784 B

2 years ago
#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;
}