Problem:
Counting Sort
Counting sort can be used for sorting elements in an array which each of the n input elements is an integer in the range 0 to k. The idea of counting sort is to determine, for each input element x, the number of elements less than x as C[x]. This information can be used to place element x directly into its position in the output array B. This scheme must be modified to handle the situation in which several elements have the same value. Please see the following pseudocode for the detail:
Counting-Sort(A, B, k)
1    for i = 0 to k
2        do C[i] = 0
3    for j = 1 to length[A]
4        do C[A[j]] = C[A[j]]+1
5    /* C[i] now contains the number of elements equal to i */
6    for i = 1 to k
7    do C[i] = C[i] + C[i-1]
8    /* C[i] now contains the number of elements less than or equal to i */
9    for j = length[A] downto 1
10       do B[C[A[j]]] = A[j]
11          C[A[j]] = C[A[j]]-1
Write a program which sorts elements of given array ascending order based on the counting sort.
Input
The first line of the input includes an integer
n
, the number of elements in the sequence.
In the second line,
n
elements of the sequence are given separated by spaces characters.
Output
Print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.
Constraints
1 ≤
n
≤ 2,000,000
0 ≤ A[
i
] ≤ 10,000
Sample Input 1
7
2 5 1 3 2 3 0
Sample Output 1
0 1 2 2 3 3 5

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

vector<int> counting_sort(vector<int>& a, int k)
{
    vector<int> c;
    c.assign(k+1, 0);

    for (int j = 0; j < a.size(); j++)
        c[a[j]]++;

    for (int i = 0; i <= k; i++)
        c[i] = c[i] + c[i-1];

    vector<int> b(k);
    for (int j = a.size()-1; j >= 0; j--)
    {
        b[c[a[j]]-1] = a[j];
        c[a[j]]--;
    }
    return b;
}

int main()
{
    int n;
    cin >> n;
    vector<int> a(n);
    for(int i = 0; i < n; i++)
        cin >> a[i];
    int k = 2000000;
    
    vector<int> v = counting_sort(a, k);
    for(int i = 0; i < n; i++)
    {
        cout << v[i];
        if(i != n-1)
            cout << " ";
    }
    cout << endl;
}
accuracy: 1
time_ms: 360
memory_kb: 26556
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;

vector<int> counting_sort(vector<int>& a, int k)
{
    vector<int> c;
    c.assign(k+1, 0);

    for (int j = 0; j < a.size(); j++)
        c[a[j]]++;

    for (int i = 0; i <= k; i++)
        c[i] = c[i] + c[i-1];

    vector<int> b(k);
    for (int j = 0; j < a.size(); j++)
    {
        b[c[a[j]]-1] = a[j];
        c[a[j]]--;
    }
    return b;
}

int main()
{
    int n;
    cin >> n;
    vector<int> a(n);
    for(int i = 0; i < n; i++)
        cin >> a[i];
    int k = 2000000;
    
    vector<int> v = counting_sort(a, k);
    for(int i = 0; i < n; i++)
    {
        cout << v[i];
        if(i != n-1)
            cout << " ";
    }
    cout << endl;
}
accuracy: 1
time_ms: 360
memory_kb: 26604
final_rank: 17

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define VMAX 10000

void CountingSort(vector<int>& num) {
	int n = num.size();
	vector<int> tmp(n + 1), cnt(VMAX + 1);
	for (int i = 0; i < n; i++) {
		cnt[num[i]]++;
	}
	for (int i = 1; i < VMAX + 1; i++) {
		cnt[i] += cnt[i - 1];
	}
	for (int i = n - 1; i >= 0; i--) {
		tmp[cnt[num[i]]] = num[i];
		cnt[num[i]]--;
	}
	for (int i = 0; i < n; i++) {
		num[i] = tmp[i+1];
	}
}


int main() {
	int n;
	cin >> n;
	vector<int> num(n);
	for (int i = 0; i < n; i++) {
		cin >> num[i];
	}
	CountingSort(num);
	for (int i = 0; i < n; i++) {
		if (i)
			cout << " ";
		cout << num[i];
	}
	cout << endl;
	return 0;
}
accuracy: 1
time_ms: 350
memory_kb: 18644
final_rank: 8

code_snippet: #include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using ll = long long;
using ld = long double;
ll INF = 2e18;
#define PI 3.14159265358979323846264338327950l
// namespace multip = boost::multiprecision;
// using lll = multip::int128_t;
using P = pair<ll, ll>;
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
using vl = vc<ll>; using vvl = vv<ll>; using vvvl = vv<vl>; using vvvvl = vv<vvl>;
using vs = vc<string>; using vvs = vv<string>;
#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define drep(i,n) for (ll i = (n)-1; i >= 0; i--)
#define nfor(i, s, n) for (ll i = (s); i < (n); i++)
#define dfor(i, s, n) for (ll i = (s)-1; i>=n; i--)
#define nall(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define em emplace
#define pob pop_back
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define YN { cout << "Yes" << endl; } else { cout << "No" << endl; }
#define dame cout << -1 << endl
#define minval(vec) *min_element(nall(vec))
#define maxval(vec) *max_element(nall(vec))
#define binaryS(vec, x) *lower_bound(nall(vec), x)
#define yu_qurid(x, y) (x) * (x) + (y) * (y)
#define mannhattan(x1, x2, y1, y2) abs(x1-x2)+abs(y1-y2)
#define vc_cout(v){ll n= size(v); rep(i,n)cout<<v[i]<<" ";cout << endl;}
#define vvc_cout(v){ll n= size(v);rep(i,n){rep(j,size(v[i])){cout<<v[i][j]<<" ";}cout<<endl;}}
// 少数の出力
void print(ld x) { printf("%.20Lf\n", x); }

// グリット用
vector<ll> dx = {1, 0, -1, 0};
vector<ll> dy = {0, 1, 0, -1};
bool in_grid(ll i, ll j, ll h, ll w) {
    return 0 <= i and i < h and 0 <= j and j < w;
}

// 計数ソート(要素の最大値がk, 要素数がn)
vector<ll> countingsort(vector<ll> A, vector<ll> &B, ll k) {
    vector<ll> C(k, 0);
    ll n = A.size();

    /* C[i]にiの出現数を記録する */
    nfor(j, 0, n) {
        C[A[j]]++;
    }

    /* C[i]にi以下の数の出現数を記録する(累積和を取る) */
    nfor(i, 1, k) {
        C[i] = C[i] + C[i-1];
    }

    for(int j = n-1; j >= 0; j--) {
        B[C[A[j]]-1] = A[j];
        C[A[j]]--;
    }
    return B;
}


// main
void solve() {
    ll n;
    cin >> n;
    vector<ll> A(n);
    ll maxv;
    nfor(i,0,n) {
        cin >> A[i];
        if (i == 0) maxv = A[i];
        else maxv = max(maxv, A[i]);
    }
    vector<ll> B(n);
    B = countingsort(A, B, maxv+1);
    nfor(i,0,n) {
        cout << B[i];
        if (i + 1 == n) cout << endl;
        else cout << " ";
    }

    return;
}

int main() {
    solve();
    return 0;
}
accuracy: 1
time_ms: 270
memory_kb: 65904
final_rank: 20

code_snippet: #include <iostream>
#include <vector>
using namespace std;

#define MAX 2000001
#define VMAX 10000

int main() {
    int n;
    cin >> n;

    vector<unsigned short> A(n + 1), B(n + 1);
    vector<int> C(VMAX + 1, 0);

    for (int i = 1; i <= n; i++) {
        cin >> A[i];
        C[A[i]]++;
    }

    for (int i = 1; i <= VMAX; i++)
        C[i] += C[i - 1];

    for (int j = n; j >= 1; j--) {
        B[C[A[j]]] = A[j];
        C[A[j]]--;
    }

    for (int i = 1; i <= n; i++) {
        if (i > 1) cout << " ";
        cout << B[i];
    }
    cout << "\n";

    return 0;
}
accuracy: 1
time_ms: 340
memory_kb: 10864
final_rank: 7

code_snippet: // 2025/09/30 Tazoe

#include <iostream>
using namespace std;

void CountingSort(int A[], int B[], int n, int k)
{
	int C[10001];
	for(int i=0; i<=k; i++){
		C[i] = 0;
	}
	
	// C[i]にiの出現数を記録する
	for(int j=0; j<n; j++){
		C[A[j]]++;
	}
	
	// C[i]にi以下の数の出現数を記録する
	for(int i=1; i<=k; i++){
		C[i] = C[i]+C[i-1];
	}
	
	for(int j=n-1; j>=0; j--){
		B[C[A[j]]-1] = A[j];
		C[A[j]]--;
	}
}


int main()
{
	int n;
	cin >> n;
	
	int A[2000000];
	int B[2000000];
	for(int i=0; i<n; i++){
		cin >> A[i];
	}
	
	CountingSort(A, B, n, 10000);
	
	for(int i=0; i<n; i++){
		cout << B[i];
		if(i==n-1){
			cout << endl;
		}
		else{
			cout << ' ';
		}
	}
	
	return 0;
}
accuracy: 1
time_ms: 360
memory_kb: 19076
final_rank: 11

code_snippet: #include <iostream>
#include <vector>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> A(n + 1), B(n + 1);
    int k = 10000;
    vector<int> C(k + 1, 0);

    for (int i = 1; i <= n; i++) cin >> A[i];

    for (int i = 0; i <= k; i++) C[i] = 0;

    for (int j = 1; j <= n; j++) C[A[j]]++;

    for (int i = 1; i <= k; i++) C[i] += C[i - 1];

    for (int j = n; j >= 1; j--) {
        B[C[A[j]]] = A[j];
        C[A[j]]--;
    }

    for (int i = 1; i <= n; i++) {
        if (i > 1) cout << " ";
        cout << B[i];
    }
    cout << "\n";
}
accuracy: 1
time_ms: 350
memory_kb: 18724
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main(){
    int N;
    cin >> N;
    int A[N];
    for(int i=0; i<N; i++){
        cin >> A[i];
    }
    int K = 10000;
    int C[K+1];
    for(int i=0; i<=K; i++){
        C[i] = 0;
    }
    for(int i=0; i<N; i++){
        C[A[i]]++;
    }
    for(int i=1; i<=K; i++){
        C[i] = C[i] + C[i-1];
    }
    sort(A, A + N, greater<int>());
    for(int i=N-1; i>=0; i--){
        cout << A[i];
        if(i != 0){
            cout << ' ';
        }
        C[A[i]]--;
    }
    cout << endl;
}
accuracy: 1
time_ms: 420
memory_kb: 11176
final_rank: 14

code_snippet: #include <iostream>
#include <vector>
#include <sstream>
using namespace std;

void CountingSort(vector<int>& A) {
    const int max = A.size();
    vector<int> B(max);
    vector<int> C(10001,0);
    
    for(int j=0; j<max; j++) {
        C[A[j]]++;
    }
    for(int i=1; i<=10000; i++) {
        C[i] = C[i] + C[i-1];
    }
    for(int j=max-1; j>=0; j--) {
        B[C[A[j]]-1] = A[j];
        C[A[j]]--;
    }
    for(int i=0; i<max; i++) {
        if(i>0) {
            cout << " ";
        }
        cout << B[i];
    }
    cout << endl;
}

int main() {
    int n;
    cin >> n;
    cin.ignore(); // 改行文字を読み飛ばす
    vector<int> A(n);
    string line;
    getline(cin, line);
    stringstream ss(line);
    for (int i = 0; i < n; i++) {
        ss >> A[i];
        // cout << A[i] << " ";
    }
    // cout << endl;
    CountingSort(A);
    return 0;
}
accuracy: 1
time_ms: 280
memory_kb: 39756
final_rank: 12

code_snippet: #include <bits/stdc++.h>
#include <cerrno>
using namespace std;
#define rep(i,n) for(int i = 0; i < n; ++i)
using ll = long long;


int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i,n) cin >> a[i];

    sort(a.begin(), a.end());
    rep(i,n) {
        if(i == n-1) cout << a[i] << endl;
        else cout << a[i] << " ";
    }
    return 0;
}
accuracy: 1
time_ms: 430
memory_kb: 10676
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < n; ++i)


int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i,n) cin >> a[i];
    sort(a.begin(), a.end());
    rep(i,n) {
        if(n-1 == i) cout << a[i] << endl;
        else cout << a[i] << " ";
    }
    return 0;
}
accuracy: 1
time_ms: 430
memory_kb: 10828
final_rank: 19

code_snippet: #include <iostream>
#include <vector>
#include <climits> 
using namespace std;
void CountingSort(vector<int>& A, vector<int>& B, int k) {
    vector<int> C(k + 1, 0);
    int n = A.size();
    for(int j = 0 ;j< n; j++){
        C[A[j]]++;
    }
    for(int i = 0 ;i<=k; i++){
        C[i] = C[i] + C[i-1];
    }
    for(int j = n-1;j>=0; j--){
        B[C[A[j]]-1] = A[j];
        C[A[j]]--;
    }   
}
int main() {
    int N;
    cin >> N;
    vector<int> A(N), B(N);
    int max_val = 0;
    for (int i = 0; i < N; i++){
        cin >> A[i];
        if (A[i] > max_val) max_val = A[i];
    } 
    CountingSort(A, B, max_val);
    for (int i = 0; i < N; i++) {
        cout << B[i];
        if (i != N - 1) cout << " ";
    }
    cout << endl;
    return 0;
}
accuracy: 1
time_ms: 360
memory_kb: 18876
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin >> n;
    int ara[n+8];
    for(int i=0;i<n;i++)
        cin >> ara[i];
    sort(ara,ara+n);
    cout << ara[0];
    for(int i=1;i<n;i++)
        cout << " " << ara[i];
    cout << endl;
    return 0;
}
accuracy: 1
time_ms: 420
memory_kb: 11160
final_rank: 13

code_snippet: #include<bits/stdc++.h>

using namespace std;
int arr[2000000];

int main()
{
    int n,i;

    scanf("%d",&n);

    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }

    sort(arr,arr+n);

    for(i=0;i<n-1;i++)
    {
        printf("%d ",arr[i]);
    }
    printf("%d",arr[n-1]);
    printf("\n");
    return 0;
}
accuracy: 1
time_ms: 250
memory_kb: 11376
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
int arr[10001];
int main()
{
    int cs=0;

    {
        int n;
       scanf("%d",&n);


    memset(arr,0,sizeof(arr));

        for(int i=0;i<n;i++)
        {
            int a;
            scanf("%d",&a);
        arr[a]++;

        }
        }

    int i=0;
        for( i=0;i<10001;i++)
        {
            if(arr[i]>0){

           printf("%d",i);
           arr[i]--;

           break;
            }



        }
 for(int j=0;j<10001;j++)
        {
            if(arr[j]>0){
                    for(int k=1;k<=arr[j];k++)
           printf(" %d",j);
            }



        }

        printf("\n");





    }
accuracy: 1
time_ms: 170
memory_kb: 3612
final_rank: 1

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,j;
    cin>>n;
    int a[n+5];
    for(i=0;i<n;i++){
        cin>>a[i];
    }
    sort(a,a+n);
    for(i=0;i<n;i++){
        cout<<a[i];
        if(i<n-1) cout<<" ";
    }
    cout<<"\n";
    return 0;
}
accuracy: 1
time_ms: 420
memory_kb: 11252
final_rank: 15

code_snippet: #include<cstdio>

using namespace std;

int main()
{
    int n,j;
    scanf("%d",&n);
    int arr,i,chk[10007];
    for(i=0;i<10007;i++)
        chk[i]=0;
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr);
        chk[arr]++;
    }
    bool done = 0;
    for(i=0;i<10003;i++)
    {
        for(j=1;j<=chk[i];j++)
        {
            if(done) printf(" ");
            done = 1;
            printf("%d",i);
        }
    }
    printf("\n");
    return 0;

}
accuracy: 1
time_ms: 180
memory_kb: 2996
final_rank: 2

code_snippet: #include<cstdio>

using namespace std;

int main()
{
    int n,j,mx=-1;
    scanf("%d",&n);
    int arr,i,chk[10007];
    for(i=0;i<10007;i++)
        chk[i]=0;
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr);
        if(arr>mx)
            mx=arr;
        chk[arr]++;
    }
    for(i=0;i<10003;i++)
    {
        for(j=1;j<=chk[i];j++)
        {
            if(i==mx && j==chk[i])
                printf("%d",i);
            else
            printf("%d ",i);
        }
    }
    printf("\n");
    return 0;

}
accuracy: 1
time_ms: 180
memory_kb: 3016
final_rank: 3

code_snippet: #include <iostream> // 標準入出力のために必要
#include <vector>   // std::vector を使うために必要（動的配列）

// k の最大値は A[i] の最大値 10,000 です
// ただし、配列のインデックスとして0からkまで使うので、サイズは k+1 必要です
const int K_MAX = 10000;

void CountingSort(std::vector<int>& A, std::vector<int>& B, int k, int n) {
    // カウンタ配列 C を初期化 (k+1 のサイズが必要)
    // 0 から k までの要素の出現数を記録するため
    std::vector<int> C(k + 1, 0);

    // C[i] に i の出現数を記録する
    // 配列 A のインデックスは 0 から n-1 なので、擬似コードの 1 から n を修正
    for (int j = 0; j < n; ++j) {
        C[A[j]]++;
    }

    // C[i] に i 以下の数の出現数を記録する（累積和）
    // これにより、B における各要素の最終位置が決定される
    for (int i = 1; i <= k; ++i) {
        C[i] = C[i] + C[i - 1];
    }

    // A[j] を正しい位置 B[C[A[j]]] に配置し、カウントを減らす
    // 安定性を保つため、A の末尾から処理していく
    // 擬似コードの n downto 1 に対応するため、インデックスは n-1 から 0 へ
    for (int j = n - 1; j >= 0; --j) {
        // C[A[j]] は、A[j] と同じ値を持つ要素が何個あり、その値がBのどこに配置されるかを示す
        // しかし、C[A[j]] は1ベースのインデックスで、vector は0ベースなので -1 する
        B[C[A[j]] - 1] = A[j];
        C[A[j]]--; // 同じ値を持つ次の要素のためにカウントを減らす
    }
}

int main() {
    // 高速な入出力を設定
    // これにより、大量のデータ入力でもタイムリミットに引っかかりにくくなる
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    int n;
    std::cin >> n; // 数列の長さを読み込む

    // 入力数列 A と出力数列 B を動的に確保
    // vector は動的なサイズ変更が可能で、メモリ管理を自動で行ってくれるため便利
    std::vector<int> A(n);
    std::vector<int> B(n);

    // 数列 A の要素を読み込む
    for (int i = 0; i < n; ++i) {
        std::cin >> A[i];
    }

    // CountingSort を呼び出してソートを実行
    // k_max は A[i] の最大値（今回の制約では10000）
    CountingSort(A, B, K_MAX, n);

    // ソートされた数列 B を出力
    for (int i = 0; i < n; ++i) {
        std::cout << B[i] << (i == n - 1 ? "" : " "); // 最後の要素の後にスペースは不要
    }
    std::cout << std::endl; // 改行

    return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 18680
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
typedef pair<int,int> PII;
void CountingSort(int a[],int b[],int n,int k){
    int c[k+1];
    for(int i=0;i<=k;i++) c[i]=0;
    for(int j=1;j<=n;j++) c[a[j]]++;
    for(int i=1;i<=k;i++) c[i]=c[i]+c[i-1];
    for(int j=n;j>=1;j--){
        b[c[a[j]]]=a[j];
        c[a[j]]--;
    }
}
void solve(){
    int n;cin>>n;
    int a[n+1];
    int k=0;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        k=max(k,a[i]);
    }
    int b[n+1];
    CountingSort(a,b,n,k);
    for(int i=1;i<=n;i++) cout<<b[i]<<" \n"[i==n];
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int T=1;
    // cin>>T;
    for(int t=1;t<=T;t++){
        solve();
    }
    return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 34612
final_rank: 6