ALDS1_6_A

2024-04-03 14:58
文章标签 alds1

本文主要是介绍ALDS1_6_A,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

计数排序

  • 执行步骤 
    • 首先将C数组下的每个元素A[i]的值累加起来,C[A[i]]++
    • 然后从前往后累加C数组,C[i] = C[i] + C[i-1],这样的累加过程实际上将数值为i的最大位置保存了,之后从最大位置往前填充数据。
    • 由于存在多个相同元素的情况,所以在输出A元素Ai前,先修正计数用的C[Ai]C[Ai]=C[Ai]1。修正过程实际上是往前移动,因为C数组累加过程已经保证存储数字i的位置不多也不少。
  • 性质 
    • 时间复杂度是O(n+k) ,n是元素的个数,k是全部元素的最大值
    • 稳定排序

using namespace std;
const int maxx = 2000001;
const int maxnum = 10010;
int A[maxx], B[maxx];
int C[maxnum];int main() {int n;scanf("%d", &n);for (int i = 0; i < maxnum; i++)C[i] = 0;for (int i = 0; i < n; i++) {scanf("%d", &A[i + 1]);C[A[i + 1]]++;}for (int i = 1; i < maxnum; i++)C[i] += C[i - 1];for (int j = 1; j <= n; j++) {B[C[A[j]]] = A[j];C[A[j]] --;}for (int i = 1; i <= n; i++)printf("%d%c", B[i], i == n ? '\n' : ' ');return 0;
}

这篇关于ALDS1_6_A的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/873223

相关文章

ALDS1_5_D 2018-2-24

#include<iostream>#include<cstdio>using namespace std;const int MAX = 500000;const int maxnum = 1000000010;// 两个局部数组int L[MAX / 2 + 2], R[MAX / 2 + 2];int A[MAX], n;long long cnt ;// 排序和合并void

ALDS1_6_C 2018-2-24

#include<iostream>#include<cstdio>using namespace std;const int MAX = 100000;const int maxnum = 1000000010;struct Card {char suit;int value;};Card L[MAX / 2 + 2], R[MAX / 2 + 2];// 排序和合并void mer

ALDS1_5_C 2018-2-23

#include<iostream>#include<cstdio>#include<cmath>using namespace std;const double PI = acos(-1.0);struct Point{double x,y;};void koch(int n, Point a, Point b) {// 递归结束if (n == 0)return;// 三等分点s,t

ALDS1_4_D 2018-2-22

#include<iostream>#include<cstdio>#include<cstring>using namespace std;typedef long long LL;LL A[100000 + 10];int n, k;//模拟装货过程bool check(LL P) {int cnt = k;for (int i = 0; i < n; ) {LL t = P;

ALDS1_5_D 逆序数 2018-2-11

using namespace std;const int MAX = 500000;const int maxnum = 1000000010;// 两个局部数组int L[MAX / 2 + 2], R[MAX / 2 + 2];int A[MAX], n;long long cnt ;// 排序和合并void merge(int left, int mid, int right)

Aizu - ALDS1_7_A

Rooted Trees 问题 A graph G = (V, E) is a data structure where V is a finite set of vertices and E is a binary relation on V represented by a set of edges. Fig. 1 illustrates an example of a graph (or

Aizu - ALDS1_7_B

Binary Tree 题目 A binary tree is a tree with a root node in which every node has at most two children. Your task is to write a program which reads a rooted binary tree T and prints the following inf