prithivMLmods/Coder-Stat huggingface dataset:
Problem p02274 (C++)

Buggy submission (status: Runtime Error):

#include <cstdio>
#include <iostream>

using namespace std;

typedef long long LL;

const int maxn = 2e4 + 5;
const int sentinel = 0x3f3f3f3f;

int l[maxn / 2 + 2], r[maxn / 2 + 2], a[maxn], n;

LL merge(int left, int mid, int right) {
  int i, j, k;
  LL cnt = 0;
  int n1 = mid - left;
  int n2 = right - mid;
  for (int i = 0; i < n1; i++) {
    l[i] = a[left + i];
  }
  for (int i = 0; i < n2; i++) {
    r[i] = a[mid + i];
  }
  l[n1] = r[n2] = sentinel;
  i = j = 0;
  for (int k = left; k < right; k++) {
    if (l[i] <= r[j]) {
      a[k] = l[i++];
    } else {
      a[k] = r[j++];
      cnt += n1 - i;
    }
  }
  return cnt;
}

LL mergesort(int left, int right) {
  int mid;
  LL v1, v2, v3;
  if (left + 1 < right) {
    mid = (left + right) >> 1;
    v1 = mergesort(left, mid);
    v2 = mergesort(mid, right);
    v3 = merge(left, mid, right);
    return v1 + v2 + v3;
  } else {
    return 0;
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  LL ans = mergesort(0, n);
  cout << ans << endl;
  return 0;
}
/*
5
3 5 2 1 4
*/


Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

typedef long long LL;

const int maxn = 2e5 + 5;
const int sentinel = 0x3f3f3f3f;

int l[maxn / 2 + 2], r[maxn / 2 + 2], a[maxn], n;

LL merge(int left, int mid, int right) {
  int i, j, k;
  LL cnt = 0;
  int n1 = mid - left;
  int n2 = right - mid;
  for (int i = 0; i < n1; i++) {
    l[i] = a[left + i];
  }
  for (int i = 0; i < n2; i++) {
    r[i] = a[mid + i];
  }
  l[n1] = r[n2] = sentinel;
  i = j = 0;
  for (int k = left; k < right; k++) {
    if (l[i] <= r[j]) {
      a[k] = l[i++];
    } else {
      a[k] = r[j++];
      cnt += n1 - i;
    }
  }
  return cnt;
}

LL mergesort(int left, int right) {
  int mid;
  LL v1, v2, v3;
  if (left + 1 < right) {
    mid = (left + right) >> 1;
    v1 = mergesort(left, mid);
    v2 = mergesort(mid, right);
    v3 = merge(left, mid, right);
    return v1 + v2 + v3;
  } else {
    return 0;
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  LL ans = mergesort(0, n);
  cout << ans << endl;
  return 0;
}
/*
5
3 5 2 1 4
*/
Problem p02274 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

template <class V, int ME> class BIT {
public:
  V bit[1 << ME];
  V total(int e) {
    V s = 0;
    e++;
    while (e)
      s += bit[e - 1], e -= e & -e;
    return s;
  }
  void update(int e, V val) {
    e++;
    while (e <= 1 << ME)
      bit[e - 1] += val, e += e & -e;
  }
};

int main() {
  int N, tmp;
  cin >> N;
  vector<int> inputs(N), rank(N);
  vector<pii> x(N);
  REP(i, N) {
    cin >> x[i].first;
    x[i].second = i;
  }
  sort(x.begin(), x.end());
  REP(i, N) { rank[x[i].second] = i + 1; }

  REP(i, N) { inputs[--rank[i]] = i; }

  BIT<int, 17> bit;
  ll res = 0;
  REP(i, N) {
    bit.update(inputs[i], 1);
    // cout << bit.total(N) - bit.total(inputs[i]) << endl;
    res += (bit.total(N) - bit.total(inputs[i]));
  }
  cout << res << endl;
  return 0;
}


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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

template <class V, int ME> class BIT {
public:
  V bit[1 << ME];
  V total(int e) {
    V s = 0;
    e++;
    while (e)
      s += bit[e - 1], e -= e & -e;
    return s;
  }
  void update(int e, V val) {
    e++;
    while (e <= 1 << ME)
      bit[e - 1] += val, e += e & -e;
  }
};

int main() {
  int N, tmp;
  cin >> N;
  vector<int> inputs(N), rank(N);
  vector<pii> x(N);
  REP(i, N) {
    cin >> x[i].first;
    x[i].second = i;
  }
  sort(x.begin(), x.end());
  REP(i, N) { rank[x[i].second] = i + 1; }

  REP(i, N) { inputs[--rank[i]] = i; }

  BIT<int, 19> bit;
  ll res = 0;
  REP(i, N) {
    bit.update(inputs[i], 1);
    // cout << bit.total(N) - bit.total(inputs[i]) << endl;
    res += (bit.total(N) - bit.total(inputs[i]));
  }
  cout << res << endl;
  return 0;
}
Problem p02274 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX_N = 100010;
int bit[MAX_N], n_, n;

int sum(int i) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}

void add(int i, int x) {
  while (i <= n) {
    bit[i] += x;
    i += i & -i;
  }
}

int a[MAX_N], b[MAX_N];
P c[MAX_N];
int main() {
  ll ans = 0;

  cin >> n;
  //    n_=1;
  //    while(n_ < n) n_ *=2;

  for (int j = 0; j < n; j++) {
    cin >> b[j];
    c[j] = P(b[j], j);
  }
  sort(c, c + n);
  for (int j = 0; j < n; j++) {
    c[j].first = j + 1;
    swap(c[j].first, c[j].second);
  }
  sort(c, c + n);
  for (int j = 0; j < n; j++) {
    a[j] = c[j].second;
  }
  for (int j = 0; j < n; j++) {
    ans += j - sum(a[j]);
    add(a[j], 1);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX_N = 1000100;
int bit[MAX_N], n_, n;

int sum(int i) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}

void add(int i, int x) {
  while (i <= n) {
    bit[i] += x;
    i += i & -i;
  }
}

int a[MAX_N], b[MAX_N];
P c[MAX_N];
int main() {
  ll ans = 0;

  cin >> n;
  //    n_=1;
  //    while(n_ < n) n_ *=2;

  for (int j = 0; j < n; j++) {
    cin >> b[j];
    c[j] = P(b[j], j);
  }
  sort(c, c + n);
  for (int j = 0; j < n; j++) {
    c[j].first = j + 1;
    swap(c[j].first, c[j].second);
  }
  sort(c, c + n);
  for (int j = 0; j < n; j++) {
    a[j] = c[j].second;
  }
  for (int j = 0; j < n; j++) {
    ans += j - sum(a[j]);
    add(a[j], 1);
  }
  cout << ans << endl;
}
Problem p02274 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
nd = list(map(int, input().split()))
c = 0
for i in range(n):
    m = min(nd)
    index = nd.index(m)
    nd.remove(m)
    c += index
print(c)


Fixed submission:
n = int(input())
nlist = list(map(int, input().split()))
W = [0 for i in range(n)]


def M(l, r):  ##l~r-1 까지의 리스트.
    if l + 1 >= r:
        return 0
    elif l + 2 == r:
        if nlist[l] <= nlist[l + 1]:
            return 0
        else:
            nlist[l], nlist[l + 1] = nlist[l + 1], nlist[l]
            return 1
    ##위의 if문으로 (r-l)가 2이하일 경우는 해놓음
    m = (l + r) // 2
    i = l
    j = m
    k = l
    cl = M(l, m)
    cr = M(m, r)
    c = 0
    while i < m and j < r:
        if nlist[i] <= nlist[j]:
            W[k] = nlist[i]  ##l부터 r까지 W에 크기순으로 대입해나감
            k += 1
            i += 1
        else:
            W[k] = nlist[j]  ##일단 W[k]에는 더 작은쪽을 저장하는듯
            k += 1
            j += 1
            c += m - i  ##만약 nlist[i] > nlist[j]라면 i~m-1 까지 반전 하고있기 때문
            ##그러고 더 작은 nlist[j]를 안에 넣는것.
    while i < m:  ##i번째쪽에 더 큰것들이 남았을 경우 (j=r 이 되있는 상황이기에 더 반전수를 늘리지는 않음)
        W[k] = nlist[i]
        k += 1
        i += 1
    while j < r:  ##j쪽에 큰게 남아있는경우도 동일
        W[k] = nlist[j]
        k += 1
        j += 1
    for i in range(l, r):
        nlist[i] = W[i]
    return cl + cr + c


print(M(0, n))
Problem p02275 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define N 1000000

using namespace std;

int main() {
  int i, j, n, k = 0;
  int A[N], B[N], C[N];

  cin >> n;
  for (i = 0; i < n; ++i) {
    cin >> A[i];
    if (A[i] > k)
      k = A[i];
  }

  for (i = 0; i <= k; ++i)
    C[i] = 0;
  for (i = 0; i < n; ++i)
    C[A[i]]++;
  for (i = 0; i <= k; ++i)
    C[i] += C[i - 1];
  for (j = n - 1; j >= 0; --j) {
    B[C[A[j]] - 1] = A[j];
    C[A[j]]--;
  }

  for (i = 0; i < n; ++i) {
    cout << B[i];
    if (i != n - 1)
      cout << " ";
  }
  cout << "\n";
  return 0;
}


Fixed submission:
#include <iostream>
#define N 2000000

using namespace std;

int main() {
  int i, j, n, k = 0;
  int A[N], B[N], C[N];

  cin >> n;
  for (i = 0; i < n; ++i) {
    cin >> A[i];
    if (A[i] > k)
      k = A[i];
  }

  for (i = 0; i <= k; ++i)
    C[i] = 0;
  for (i = 0; i < n; ++i)
    C[A[i]]++;
  for (i = 0; i <= k; ++i)
    C[i] += C[i - 1];
  for (j = n - 1; j >= 0; --j) {
    B[C[A[j]] - 1] = A[j];
    C[A[j]]--;
  }

  for (i = 0; i < n; ++i) {
    cout << B[i];
    if (i != n - 1)
      cout << " ";
  }
  cout << "\n";
  return 0;
}
Problem p02275 (C++)

Buggy submission (status: Runtime Error):
#include "iostream"

using namespace std;

int main() {

  int in[2000000];
  int ans[2000000];
  int co[10001];
  int n;

  cin >> n;

  for (int i = 0; i <= 10000; i++) {
    co[i] = 0;
  }

  for (int i = 0; i < n; i++) {
    cin >> in[i];
    co[in[i]]++;
  }

  co[0]--;
  for (int i = 1; i <= 10000; i++) {
    co[i] += co[i - 1];
  }
  for (int i = 0; i < n; i++) {
    ans[co[in[i]]] = in[i];
    co[in[i]]--;
  }

  for (int i = 0; i < n; i++) {
    cout << ans[i];
    if (i < n - 1)
      cout << " ";
  }

  cout << endl;

  return 0;
}

Fixed submission:
#include "iostream"

using namespace std;

int main() {

  short in[2000000];
  short ans[2000000];
  int co[10001];
  int n;

  cin >> n;

  for (int i = 0; i <= 10000; i++) {
    co[i] = 0;
  }

  for (int i = 0; i < n; i++) {
    cin >> in[i];
    co[in[i]]++;
  }

  co[0]--;
  for (int i = 1; i <= 10000; i++) {
    co[i] += co[i - 1];
  }
  for (int i = 0; i < n; i++) {
    ans[co[in[i]]] = in[i];
    co[in[i]]--;
  }

  for (int i = 0; i < n; i++) {
    cout << ans[i];
    if (i < n - 1)
      cout << " ";
  }

  cout << endl;

  return 0;
}
Problem p02275 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

int arr[1000100], c[100001], ans[1000100];

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

  memset(c, 0, sizeof(c));

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

  for (i = 1; i < 100001; ++i)
    c[i] = c[i] + c[i - 1];

  for (i = n; i > 0; --i) {
    ans[c[arr[i]]] = arr[i];
    --c[arr[i]];
  }

  for (i = 1; i < n; ++i)
    printf("%d ", ans[i]);
  printf("%d\n", ans[n]);

  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

int arr[2000100], c[100001], ans[2000100];

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

  memset(c, 0, sizeof(c));

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

  for (i = 1; i < 100001; ++i)
    c[i] = c[i] + c[i - 1];

  for (i = n; i > 0; --i) {
    ans[c[arr[i]]] = arr[i];
    --c[arr[i]];
  }

  for (i = 1; i < n; ++i)
    printf("%d ", ans[i]);
  printf("%d\n", ans[n]);

  return 0;
}
Problem p02275 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define MAX 2000001
#define VMAX 10000

int main() {
  unsigned short *A, *B;

  int C[VMAX + 1];
  int n, i, j;
  scanf("%d", &n);

  A = (unsigned short *)malloc(MAX);
  B = (unsigned short *)malloc(MAX);

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

  for (int i = 0; i < n; i++) {
    scanf("%hu", &A[i + 1]);
    C[A[i + 1]]++;
  }

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

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

  for (i = 1; i <= n; i++) {
    if (i > 1)
      printf(" ");
    printf("%d", B[i]);
  }
  printf("\n");
  return 0;
}

Fixed submission:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define MAX 2000001
#define VMAX 10000

int main() {
  unsigned short *A, *B;

  int C[VMAX + 1];
  int n, i, j;
  scanf("%d", &n);

  A = (unsigned short *)malloc(sizeof(short) * n + 1);
  B = (unsigned short *)malloc(sizeof(short) * n + 1);

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

  for (int i = 0; i < n; i++) {
    scanf("%hu", &A[i + 1]);
    C[A[i + 1]]++;
  }

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

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

  for (i = 1; i <= n; i++) {
    if (i > 1)
      printf(" ");
    printf("%d", B[i]);
  }
  printf("\n");
  return 0;
}
Problem p02275 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdio.h>

#define MAX 2000001
#define vMAX 10000

int main() {
  int n;
  int C[vMAX];
  for (int i = 0; i < vMAX; i++)
    C[i] = 0;

  scanf("%d", &n);
  int *A = new int[n];
  int *B = new int[n];

  for (int i = 1; i <= n; i++) {
    scanf("%d", &A[i]);
    C[A[i]] += 1;
  }

  for (int i = 1; i < vMAX; i++) {
    C[i] = C[i] + C[i - 1];
  }

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

  for (int i = 1; i <= n; i++) {
    if (i > 1)
      printf(" ");
    printf("%d", B[i]);
  }
  printf("\n");
  delete[] A;
  delete[] B;
  return 0;
}

Fixed submission:
#include <iostream>
#include <stdio.h>

#define MAX 2000001
#define vMAX 10000

int main() {
  int n;
  int C[vMAX];
  for (int i = 0; i < vMAX; i++)
    C[i] = 0;

  scanf("%d", &n);
  int *A = new int[n + 1];
  int *B = new int[n + 1];

  for (int i = 1; i <= n; i++) {
    scanf("%d", &A[i]);
    C[A[i]] += 1;
  }

  for (int i = 1; i < vMAX; i++) {
    C[i] = C[i] + C[i - 1];
  }

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

  for (int i = 1; i <= n; i++) {
    if (i > 1)
      printf(" ");
    printf("%d", B[i]);
  }
  printf("\n");
  delete[] A;
  delete[] B;
  return 0;
}
Problem p02275 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

#define MAX_LEN 200001
#define MAX_VAL 10001

void countingSort(int n, int A[], int B[], int C[]) {
  int i;
  for (i = 0; i < MAX_VAL; i++) {
    C[i] = 0;
  }
  for (i = 0; i < n; i++) {
    C[A[i]]++;
  }

  int idx = 0;
  for (i = 0; i < MAX_VAL; i++) {
    if (C[i] == 0)
      continue;
    for (int j = 0; j < C[i]; j++) {
      B[idx++] = i;
    }
  }
}

int main() {

  int n, A[MAX_LEN], B[MAX_LEN], C[MAX_VAL];

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

  countingSort(n, A, B, C);

  cout << B[0];
  for (int i = 1; i < n; i++) {
    cout << " " << B[i];
  }
  cout << endl;

  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;

#define MAX_LEN 2000000
#define MAX_VAL 10000

void countingSort(int n, int A[], int B[], int C[]) {
  int i;
  for (i = 0; i < MAX_VAL; i++) {
    C[i] = 0;
  }
  for (i = 0; i < n; i++) {
    C[A[i]]++;
  }

  int idx = 0;
  for (i = 0; i < MAX_VAL; i++) {
    if (C[i] == 0)
      continue;
    for (int j = 0; j < C[i]; j++) {
      B[idx++] = i;
    }
  }
}

int main() {

  int n, A[MAX_LEN], B[MAX_LEN], C[MAX_VAL];

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

  countingSort(n, A, B, C);

  cout << B[0];
  for (int i = 1; i < n; i++) {
    cout << " " << B[i];
  }
  cout << endl;

  return 0;
}
Problem p02275 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#define N 10001
using namespace std;

void Counting_Sort(int *A, int n, int *B, int k) {
  int *C = new int[N];
  for (int i = 0; i <= k; i++)
    C[i] = 0;
  for (int j = 0; j < n; j++)
    C[A[j]] = C[A[j]] + 1;
  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]]] = A[j];
    C[A[j]] = C[A[j]] - 1;
  }
  delete[] C;
}

int main() {
  int n;
  cin >> n;
  int *A = new int[n];
  int *B = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  Counting_Sort(A, n, B, N - 1);
  for (int i = 1; i < n + 1; i++) {
    cout << B[i];
    if (i == n)
      cout << endl;
    else
      cout << " ";
  }
  delete[] A;
  delete[] B;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#define N 10001
using namespace std;

void Counting_Sort(int *A, int n, int *B, int k) {
  int *C = new int[N];
  for (int i = 0; i <= k; i++)
    C[i] = 0;
  for (int j = 0; j < n; j++)
    C[A[j]] = C[A[j]] + 1;
  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]]] = A[j];
    C[A[j]] = C[A[j]] - 1;
  }
  delete[] C;
}

int main() {
  int n;
  cin >> n;
  int *A = new int[n];
  int *B = new int[n + 1];
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  Counting_Sort(A, n, B, N - 1);
  for (int i = 1; i < n + 1; i++) {
    cout << B[i];
    if (i == n)
      cout << endl;
    else
      cout << " ";
  }
  delete[] A;
  delete[] B;
  return 0;
}
Problem p02275 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

#define N 20001

int n;

void Counting_Sort(int a[], int b[], int k) {
  int i, j, c[N];

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

  for (j = 1; j <= n; j++)
    c[a[j]]++;

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

  for (j = n; j > 0; j--) {
    b[c[a[j]]] = a[j];
    c[a[j]]--;
  }
  for (i = 1; i <= n; i++) {
    printf("%d", b[i]);
    if (i != n)
      printf(" ");
  }
  printf("\n");
}

int main() {
  int i, max = 0;
  static int a[N], b[N];
  scanf("%d", &n);

  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] > max)
      max = a[i];
  }

  Counting_Sort(a, b, max);

  return 0;
}

Fixed submission:
#include <stdio.h>

#define N 2000001

int n;

void Counting_Sort(int a[], int b[], int k) {
  int i, j, c[N];

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

  for (j = 1; j <= n; j++)
    c[a[j]]++;

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

  for (j = n; j > 0; j--) {
    b[c[a[j]]] = a[j];
    c[a[j]]--;
  }
  for (i = 1; i <= n; i++) {
    printf("%d", b[i]);
    if (i != n)
      printf(" ");
  }
  printf("\n");
}

int main() {
  int i, max = 0;
  static int a[N], b[N];
  scanf("%d", &n);

  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] > max)
      max = a[i];
  }

  Counting_Sort(a, b, max);

  return 0;
}
Problem p02275 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

int A[200000];
int B[200000];
int C[10001];

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

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

  for (int i = 1; i <= 10000; i++) {
    C[i] += C[i - 1];
  }

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

  for (int i = 0; i < n; i++) {
    cout << B[i];
    if (i == n - 1) {
      cout << endl;
    } else {
      cout << ' ';
    }
  }
}

Fixed submission:
#include <iostream>

using namespace std;

int A[2000000];
int B[2000000];
int C[10001];

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

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

  for (int i = 1; i <= 10000; i++) {
    C[i] += C[i - 1];
  }

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

  for (int i = 0; i < n; i++) {
    cout << B[i];
    if (i == n - 1) {
      cout << endl;
    } else {
      cout << ' ';
    }
  }
}
Problem p02275 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

int b[200005];
signed main() {
  int a;
  scanf("%d", &a);
  for (int c = 0; c < a; c++) {
    scanf("%d", &b[c]);
  }
  sort(b, b + a);
  for (int i = 0; i < a; i++) {
    if (i)
      cout << " ";
    cout << b[i];
  }
  puts("");
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

int b[2000005];
signed main() {
  int a;
  scanf("%d", &a);
  for (int c = 0; c < a; c++) {
    scanf("%d", &b[c]);
  }
  sort(b, b + a);
  for (int i = 0; i < a; i++) {
    if (i)
      cout << " ";
    cout << b[i];
  }
  puts("");
}
Problem p02275 (C++)

Buggy submission (status: Runtime Error):
#include <array>
#include <iostream>
#include <string>

using namespace std;

int n;
array<short, 2000001> A;
array<short, 2000001> B;
array<short, 10001> C;

void CountingSort(int k) {

  for (int j = 0; j < n; j++) {
    C[A[j]]++;
  }
  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]]--;
  }
  for (int i = 0; i < n - 1; i++)
    cout << B.at(i) << " ";
  cout << B.at(n - 1) << endl;
}

int main() {
  int k = -1;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    if (k < A[i])
      k = A[i];
  }
  CountingSort(k);
  return 0;
}

Fixed submission:
#include <array>
#include <iostream>
#include <string>

using namespace std;

int n;
array<short, 2000001> A;
array<short, 2000001> B;
array<int, 10001> C;

void CountingSort(int k) {

  for (int j = 0; j < n; j++) {
    C[A[j]]++;
  }
  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]]--;
  }
  for (int i = 0; i < n - 1; i++)
    cout << B.at(i) << " ";
  cout << B.at(n - 1) << endl;
}

int main() {
  int k = -1;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    if (k < A[i])
      k = A[i];
  }
  CountingSort(k);
  return 0;
}
Problem p02275 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

const int MAX_N = 200000;

int main() {
  int n, A[MAX_N] = {0}, B[MAX_N] = {0}, C[10000] = {0}, max;

  scanf("%d", &n);

  max = 0;

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

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

  for (int i = 1; i <= max; i++) {
    C[i] += C[i - 1];
  }

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

  for (int i = 1; i <= n; i++) {
    if (i == n) {
      printf("%d\n", B[i]);
    } else {
      printf("%d ", B[i]);
    }
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

const int MAX_N = 2000000;

int main() {
  int n, A[MAX_N] = {0}, B[MAX_N] = {0}, C[10000] = {0}, max;

  scanf("%d", &n);

  max = 0;

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

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

  for (int i = 1; i <= max; i++) {
    C[i] += C[i - 1];
  }

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

  for (int i = 1; i <= n; i++) {
    if (i == n) {
      printf("%d\n", B[i]);
    } else {
      printf("%d ", B[i]);
    }
  }

  return 0;
}
Problem p02275 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

void counting_sort(vector<int> &A, int n) {

  int k = *max_element(A.begin(), A.end());
  vector<int> B(n);
  vector<int> C(k + 1);

  for (int i = 0; i < k; i++)
    C[i] = 0;
  for (int j = 0; j < n; j++)
    C[A[j]]++;
  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]]] = A[j];
    C[A[j]]--;
  }

  for (int i = 1; i <= n; i++) {
    if (i != n) {
      cout << B[i] << " ";
    } else {
      cout << B[i] << endl;
    }
  }
}

int main() {

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

  counting_sort(A, n);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

void counting_sort(vector<int> &A, int n) {

  int k = *max_element(A.begin(), A.end());
  vector<int> B(n + 1);
  vector<int> C(k + 1);

  for (int i = 0; i < k; i++)
    C[i] = 0;
  for (int j = 0; j < n; j++)
    C[A[j]]++;
  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]]] = A[j];
    C[A[j]]--;
  }

  for (int i = 1; i <= n; i++) {
    if (i != n) {
      cout << B[i] << " ";
    } else {
      cout << B[i] << endl;
    }
  }
}

int main() {

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

  counting_sort(A, n);

  return 0;
}
Problem p02275 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX(x, y) (((x) > (y)) ? (x) : (y))

int main() {

  int n, i, max;
  scanf("%d", &n);

  int A[n + 1];
  int B[n + 1];

  max = (1 << 30) * -1;
  for (i = 0; i < n; i++) {
    scanf("%hu", &A[i + 1]);
    max = MAX(max, A[i + 1]);
  }
  int C[max + 1];
  memset(C, 0, sizeof(C));

  for (i = 0; i < n; i++)
    C[A[i + 1]]++;
  for (i = 1; i <= max; i++)
    C[i] += C[i - 1];

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

  printf("%d", B[1]);
  for (i = 2; i <= n; i++) {
    printf(" %d", B[i]);
  }
  printf("\n");

  return 0;
}


Fixed submission:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX(x, y) (((x) > (y)) ? (x) : (y))

int main() {

  int n, i, max;
  scanf("%d", &n);

  unsigned short A[n + 1];
  unsigned short B[n + 1];

  max = (1 << 30) * -1;
  for (i = 0; i < n; i++) {
    scanf("%hu", &A[i + 1]);
    max = MAX(max, A[i + 1]);
  }
  int C[max + 1];
  memset(C, 0, sizeof(C));

  for (i = 0; i < n; i++)
    C[A[i + 1]]++;
  for (i = 1; i <= max; i++)
    C[i] += C[i - 1];

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

  printf("%d", B[1]);
  for (i = 2; i <= n; i++) {
    printf(" %d", B[i]);
  }
  printf("\n");

  return 0;
}
Problem p02275 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int n;
int *B, *A;
void countingsort(int N[], int max) {
  int S[max + 1];
  for (int i = 0; i < n; i++) {
    S[i] = 0;
  }
  for (int j = 0; j < n; j++) {
    S[N[j]]++;
  }
  int p = 0;
  for (int k = 0; k < max + 1; k++) {
    for (int l = 1; l <= S[k]; l++) {
      B[p] = k;
      p++;
    }
  }
}

int main() {
  cin >> n;
  int max = -1;
  A = new int[n];
  B = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    if (A[i] >= max) {
      max = A[i];
    }
  }
  countingsort(A, max);
  for (int i = 0; i < n; i++) {
    if (i != n - 1) {
      cout << B[i] << " ";
    } else {
      cout << B[i] << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int n;
int *B, *A;
void countingsort(int N[], int max) {
  int S[max + 1];
  for (int i = 0; i < max + 1; i++) {
    S[i] = 0;
  }
  for (int j = 0; j < n; j++) {
    S[N[j]]++;
  }
  int p = 0;
  for (int k = 0; k < max + 1; k++) {
    for (int l = 1; l <= S[k]; l++) {
      B[p] = k;
      p++;
    }
  }
}

int main() {
  cin >> n;
  int max = -1;
  A = new int[n];
  B = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    if (A[i] >= max) {
      max = A[i];
    }
  }
  countingsort(A, max);
  for (int i = 0; i < n; i++) {
    if (i != n - 1) {
      cout << B[i] << " ";
    } else {
      cout << B[i] << endl;
    }
  }
  return 0;
}
Problem p02275 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#define N 10000
int A[200000], C[N], B[200000];
int main() {
  int i, n;

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

  for (i = 0; i < N; i++) {
    C[i] = 0;
  }
  for (i = 1; i < n + 1; i++) {
    C[A[i]] += 1;
  }
  for (i = 0; i < N; i++) {
    C[i] += C[i - 1];
  }
  for (i = n + 1; i > 0; i--) {
    B[C[A[i]]] = A[i];
    C[A[i]] -= 1;
  }

  for (i = 1; i < n; i++) {
    printf("%d ", B[i]);
  }
  printf("%d", B[i]);
  printf("\n");
  return 0;
}

Fixed submission:
#include <stdio.h>
#define N 10000
int A[2000001], C[N], B[2000001];
int main() {
  int i, n;

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

  for (i = 0; i < N; i++) {
    C[i] = 0;
  }
  for (i = 1; i < n + 1; i++) {
    C[A[i]] += 1;
  }
  for (i = 0; i < N; i++) {
    C[i] += C[i - 1];
  }
  for (i = n + 1; i > 0; i--) {
    B[C[A[i]]] = A[i];
    C[A[i]] -= 1;
  }

  for (i = 1; i < n; i++) {
    printf("%d ", B[i]);
  }
  printf("%d", B[i]);
  printf("\n");
  return 0;
}
Problem p02275 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#define N 10000
int A[10000], C[N], B[2000001];
int main() {
  int i, n;

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

  for (i = 0; i < N; i++) {
    C[i] = 0;
  }
  for (i = 1; i < n + 1; i++) {
    C[A[i]] += 1;
  }
  for (i = 0; i < N; i++) {
    C[i] += C[i - 1];
  }
  for (i = n + 1; i > 0; i--) {
    B[C[A[i]]] = A[i];
    C[A[i]] -= 1;
  }

  for (i = 1; i < n; i++) {
    printf("%d ", B[i]);
  }
  printf("%d", B[i]);
  printf("\n");
  return 0;
}

Fixed submission:
#include <stdio.h>
#define N 10000
int A[2000001], C[N], B[2000001];
int main() {
  int i, n;

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

  for (i = 0; i < N; i++) {
    C[i] = 0;
  }
  for (i = 1; i < n + 1; i++) {
    C[A[i]] += 1;
  }
  for (i = 0; i < N; i++) {
    C[i] += C[i - 1];
  }
  for (i = n + 1; i > 0; i--) {
    B[C[A[i]]] = A[i];
    C[A[i]] -= 1;
  }

  for (i = 1; i < n; i++) {
    printf("%d ", B[i]);
  }
  printf("%d", B[i]);
  printf("\n");
  return 0;
}
Problem p02275 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

int main() {
  int n;
  scanf("%d", &n);
  int A[n + 1], B[n + 1], C[10000 + 1];

  for (int i = 0; i < 10000 + 1; i++) {
    C[i] = 0;
  }

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

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

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

  for (int i = 0; i < n; i++) {
    if (i)
      printf(" ");
    printf("%d", B[i + 1]);
  }
  printf("\n");

  return 0;
}


Fixed submission:
#include <stdio.h>

int main() {
  int n;
  scanf("%d", &n);
  int A[n + 1], B[n + 1], C[10000 + 1];

  for (int i = 0; i < 10000 + 1; i++) {
    C[i] = 0;
  }

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

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

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

  for (int i = 0; i < n; i++) {
    if (i)
      printf(" ");
    printf("%d", B[i + 1]);
  }
  printf("\n");

  return 0;
}
Problem p02275 (C++)

Buggy submission (status: Runtime Error):
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>

const int MAX = 10010;

void countingSort(std::vector<int> &A, std::vector<int> &sorted) {
  int count[MAX];
  int n = A.size();
  memset(count, 0, sizeof(count));
  for (int i = 0; i < n; i++)
    count[A[i]]++;
  for (int i = 0; i < MAX; i++)
    count[i + 1] += count[i];
  for (int i = n - 1; i >= 0; i++)
    sorted[--count[A[i]]] = A[i];
}

int main(void) {
  int n;
  std::cin >> n;
  std::vector<int> A(n);
  for (int i = 0; i < n; i++)
    std::cin >> A[i];
  std::vector<int> sorted(n);
  countingSort(A, sorted);
  for (int i = 0; i < n; i++)
    std::cout << (i ? " " : "") << sorted[i];
  std::cout << std::endl;

  return 0;
}


Fixed submission:
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>

const int MAX = 10010;

void countingSort(std::vector<int> &A, std::vector<int> &sorted) {
  int count[MAX];
  int n = A.size();
  memset(count, 0, sizeof(count));
  for (int i = 0; i < n; i++)
    count[A[i]]++;
  for (int i = 0; i < MAX; i++)
    count[i + 1] += count[i];
  for (int i = n - 1; i >= 0; i--)
    sorted[--count[A[i]]] = A[i];
}

int main(void) {
  int n;
  std::cin >> n;
  std::vector<int> A(n);
  for (int i = 0; i < n; i++)
    std::cin >> A[i];
  std::vector<int> sorted(n);
  countingSort(A, sorted);
  for (int i = 0; i < n; i++)
    std::cout << (i ? " " : "") << sorted[i];
  std::cout << std::endl;

  return 0;
}
Problem p02275 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

static void inputArray(int A[], int num) {
  for (int i = 0; i < num; i++) {
    cin >> A[i];
  }
}

static void printArray(int A[], int num) {
  for (int i = 0; i < num; i++) {
    cout << ((i != 0) ? " " : "") << A[i];
  }
  cout << endl;
}

static vector<int> C(10000 + 1, 0);
static inline void countingSort(int A[], int B[], int num) {
  for (int i = 0; i < num; i++) {
    C[A[i + 1]]++;
  }
  for (int i = 1; i <= 10000; i++) {
    C[i] = C[i] + C[i - 1];
  }
  for (int i = num; i > 0; i--) {
    //  for (int i = 1; i <= num; i++) {
    B[C[A[i]]] = A[i];
    C[A[i]]--;
  }
}

static vector<int> A(2000000 + 1), B(100000 + 1);

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

  inputArray(&A[1], n);

  countingSort(&A[0], &B[0], n);

  printArray(&B[1], n);

  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

static void inputArray(int A[], int num) {
  for (int i = 0; i < num; i++) {
    cin >> A[i];
  }
}

static void printArray(int A[], int num) {
  for (int i = 0; i < num; i++) {
    cout << ((i != 0) ? " " : "") << A[i];
  }
  cout << endl;
}

static vector<int> C(10000 + 1, 0);
static inline void countingSort(int A[], int B[], int num) {
  for (int i = 0; i < num; i++) {
    C[A[i + 1]]++;
  }
  for (int i = 1; i <= 10000; i++) {
    C[i] = C[i] + C[i - 1];
  }
  for (int i = num; i > 0; i--) {
    //  for (int i = 1; i <= num; i++) {
    B[C[A[i]]] = A[i];
    C[A[i]]--;
  }
}

static vector<int> A(2000000 + 1), B(2000000 + 1);

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

  inputArray(&A[1], n);

  countingSort(&A[0], &B[0], n);

  printArray(&B[1], n);

  return 0;
}
Problem p02275 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#define N 10000
int A[200000], C[N], B[2000001];
int main() {
  int i, n;

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

  for (i = 0; i < N; i++) {
    C[i] = 0;
  }
  for (i = 1; i < n + 1; i++) {
    C[A[i]] += 1;
  }
  for (i = 0; i < N; i++) {
    C[i] += C[i - 1];
  }
  for (i = n + 1; i > 0; i--) {
    B[C[A[i]]] = A[i];
    C[A[i]] -= 1;
  }

  for (i = 1; i < n; i++) {
    printf("%d ", B[i]);
  }
  printf("%d", B[i]);
  printf("\n");
  return 0;
}

Fixed submission:
#include <stdio.h>
#define N 10000
int A[2000001], C[N], B[2000001];
int main() {
  int i, n;

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

  for (i = 0; i < N; i++) {
    C[i] = 0;
  }
  for (i = 1; i < n + 1; i++) {
    C[A[i]] += 1;
  }
  for (i = 0; i < N; i++) {
    C[i] += C[i - 1];
  }
  for (i = n + 1; i > 0; i--) {
    B[C[A[i]]] = A[i];
    C[A[i]] -= 1;
  }

  for (i = 1; i < n; i++) {
    printf("%d ", B[i]);
  }
  printf("%d", B[i]);
  printf("\n");
  return 0;
}
Problem p02275 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
const int num = 20001, num1 = 10000;
int main() {
  int n, a[num], c[num1 + 1], b[num];
  cin >> n;
  for (int i = 0; i <= num1; i++)
    c[i] = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    c[a[i]]++;
  for (int i = 1; i <= num1; i++)
    c[i] += c[i - 1];
  for (int i = 0; i < n; i++)
    b[(c[a[i]]--) - 1] = a[i];
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << b[i];
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
const int num = 2000001, num1 = 10000;
int main() {
  int n, a[num], c[num1 + 1], b[num];
  cin >> n;
  for (int i = 0; i <= num1; i++)
    c[i] = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    c[a[i]]++;
  for (int i = 1; i <= num1; i++)
    c[i] += c[i - 1];
  for (int i = 0; i < n; i++)
    b[(c[a[i]]--) - 1] = a[i];
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << b[i];
  }
  cout << endl;
  return 0;
}
Problem p02275 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "string.h"
#include <algorithm>
#include <iostream>
#include <limits>
#include <math.h>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using intll_t = long long;

int main() {
  int N;
  scanf("%d", &N);
  vector<int> AIS;
  AIS.reserve(N);
  for (int i = 0; i < N; i++) {
    int AI;
    scanf("%d", &AI);
    AIS.push_back(AI);
  }
  vector<int> c(10001, 0);
  for (int number : AIS) {
    c[number]++;
  }
  for (int i = 1; i < c.size(); i++) {
    c[i] += c[i - 1];
  }
  vector<int> out(N, 0);
  for (int j = N - 1; j >= 0; j--) {
    int number = AIS[j];
    int &cntAtMostNumber = c[number];
    out[cntAtMostNumber - 1] = number;
    cntAtMostNumber--;
  }
  auto outIt = out.begin();
  while (true) {
    printf("%d", *outIt);
    if (outIt == out.end()) {
      break;
    } else {
      printf(" ");
    }
  }
  printf("\n");
  return 0;
}


Fixed submission:
#include "string.h"
#include <algorithm>
#include <iostream>
#include <limits>
#include <math.h>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using intll_t = long long;

int main() {
  int N;
  scanf("%d", &N);
  vector<int> AIS;
  AIS.reserve(N);
  for (int i = 0; i < N; i++) {
    int AI;
    scanf("%d", &AI);
    AIS.push_back(AI);
  }
  vector<int> c(10001, 0);
  for (int number : AIS) {
    c[number]++;
  }
  for (int i = 1; i < c.size(); i++) {
    c[i] += c[i - 1];
  }
  vector<int> out(N, 0);
  for (int j = N - 1; j >= 0; j--) {
    int number = AIS[j];
    int &cntAtMostNumber = c[number];
    out[cntAtMostNumber - 1] = number;
    cntAtMostNumber--;
  }
  auto outIt = out.begin();
  while (true) {
    printf("%d", *outIt);
    ++outIt;
    if (outIt == out.end()) {
      break;
    } else {
      printf(" ");
    }
  }
  printf("\n");
  return 0;
}
Problem p02275 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

static void inputArray(int A[], int num) {
  for (int i = 0; i < num; i++) {
    cin >> A[i];
  }
}

static void printArray(int A[], int num) {
  for (int i = 0; i < num; i++) {
    cout << ((i != 0) ? " " : "") << A[i];
  }
  cout << endl;
}

static vector<int> C(10000 + 1, 0);
static inline void countingSort(int A[], int B[], int num) {
  for (int i = 0; i < num; i++) {
    C[A[i + 1]]++;
  }
  for (int i = 1; i <= 10000; i++) {
    C[i] = C[i] + C[i - 1];
  }
  for (int i = num; i > 0; i--) {
    //  for (int i = 1; i <= num; i++) {
    B[C[A[i]]] = A[i];
    C[A[i]]--;
  }
}

static vector<int> A(10000 + 1), B(10000 + 1);

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

  inputArray(&A[1], n);

  countingSort(&A[0], &B[0], n);

  printArray(&B[1], n);

  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

static void inputArray(int A[], int num) {
  for (int i = 0; i < num; i++) {
    cin >> A[i];
  }
}

static void printArray(int A[], int num) {
  for (int i = 0; i < num; i++) {
    cout << ((i != 0) ? " " : "") << A[i];
  }
  cout << endl;
}

static vector<int> C(10000 + 1, 0);
static inline void countingSort(int A[], int B[], int num) {
  for (int i = 0; i < num; i++) {
    C[A[i + 1]]++;
  }
  for (int i = 1; i <= 10000; i++) {
    C[i] = C[i] + C[i - 1];
  }
  for (int i = num; i > 0; i--) {
    //  for (int i = 1; i <= num; i++) {
    B[C[A[i]]] = A[i];
    C[A[i]]--;
  }
}

static vector<int> A(2000000 + 1), B(2000000 + 1);

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

  inputArray(&A[1], n);

  countingSort(&A[0], &B[0], n);

  printArray(&B[1], n);

  return 0;
}
Problem p02276 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#define N 10000
int partition(int, int);

int A[N];
int main() {
  int i, n, q;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }
  q = partition(0, n - 1);

  for (i = 0; i < n; i++) {
    if (i != 0)
      printf(" ");
    if (i == q)
      printf("[");
    printf("%d", A[i]);
    if (i == q)
      printf("]");
  }
  printf("\n");

  return 0;
}

int partition(int y, int x) {
  int i, j, n, m;
  n = A[x];
  j = y - 1;
  for (i = 0; i < x; i++) {
    if (A[i] <= n) {
      j++;
      m = A[j];
      A[j] = A[i];
      A[i] = m;
    }
  }
  m = A[j + 1];
  A[j + 1] = A[x];
  A[x] = m;
  return j + 1;
}

Fixed submission:
#include <stdio.h>
#define N 100000
int partition(int, int);

int A[N];
int main() {
  int i, n, q;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }
  q = partition(0, n - 1);

  for (i = 0; i < n; i++) {
    if (i != 0)
      printf(" ");
    if (i == q)
      printf("[");
    printf("%d", A[i]);
    if (i == q)
      printf("]");
  }
  printf("\n");

  return 0;
}

int partition(int y, int x) {
  int i, j, n, m;
  n = A[x];
  j = y - 1;
  for (i = 0; i < x; i++) {
    if (A[i] <= n) {
      j++;
      m = A[j];
      A[j] = A[i];
      A[i] = m;
    }
  }
  m = A[j + 1];
  A[j + 1] = A[x];
  A[x] = m;
  return j + 1;
}
Problem p02276 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;
int n;
int partition(int A[], int p, int r) {
  int x = A[r];
  int i = p - 1;
  for (int j = p; j < r; j++) {
    cerr << "j:" << j << endl;
    if (A[j] <= x) {
      i++;
      swap(A[i], A[j]);
    }
  }
  swap(A[i + 1], A[r]);
  return i + 1;
}
int main() {
  for (; cin >> n;) {
    int data[n];
    for (int i = 0; i < n; i++)
      cin >> data[i];

    int q = partition(data, 0, n - 1);
    for (int i = 0; i < n; i++) {
      if (i == q)
        cout << "[";
      cout << data[i];
      if (i == q)
        cout << "]";

      if (i != n - 1)
        cout << " ";
      else
        cout << endl;
    }
  }
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;
int n;
int partition(int A[], int p, int r) {
  int x = A[r];
  int i = p - 1;
  for (int j = p; j < r; j++) {
    if (A[j] <= x) {
      i++;
      swap(A[i], A[j]);
    }
  }
  swap(A[i + 1], A[r]);
  return i + 1;
}
int main() {
  for (; cin >> n;) {
    int data[n];
    for (int i = 0; i < n; i++)
      cin >> data[i];

    int q = partition(data, 0, n - 1);
    for (int i = 0; i < n; i++) {
      if (i == q)
        cout << "[";
      cout << data[i];
      if (i == q)
        cout << "]";

      if (i != n - 1)
        cout << " ";
      else
        cout << endl;
    }
  }
}
Problem p02276 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
using namespace std;

int n[100003] = {};
int main() {

  int N;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> n[i];

  int x = 0, y = 0;
  while (x < N && y < N) {
    if (n[x] < n[N - 1]) {
      x++;
      y = x + 1;
    } else if (n[x] > n[N - 1] && n[y] > n[N - 1])
      y++;
    else if (n[x] > n[N - 1] && n[y] <= n[N - 1]) {
      int t = n[y];
      n[y] = n[x];
      n[x] = t;
      x++;
      y++;
    }
  }

  //	cout<<n[N-1];
  //	int t=n[N-1];n[N-1]=n[x-1];n[x-1]=t;

  for (int i = 0; i < N; i++) {
    if (i > 0)
      cout << " ";
    if (i != x - 1)
      cout << n[i];
    if (i == x - 1)
      cout << "[" << n[i] << "]";
  }
  cout << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
using namespace std;

int n[100003] = {};
int main() {

  int N;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> n[i];

  int x = 0, y = 0;
  while (x < N && y < N) {

    if (n[x] <= n[N - 1]) {
      x++;
      y = x + 1;
    } else if (n[x] > n[N - 1] && n[y] > n[N - 1])
      y++;
    else if (n[x] > n[N - 1] && n[y] <= n[N - 1]) {
      int t = n[y];
      n[y] = n[x];
      n[x] = t;
      x++;
      y++;
    }
  }

  //	cout<<n[N-1];
  //	int t=n[N-1];n[N-1]=n[x-1];n[x-1]=t;

  for (int i = 0; i < N; i++) {
    if (i > 0)
      cout << " ";
    if (i != x - 1)
      cout << n[i];
    if (i == x - 1)
      cout << "[" << n[i] << "]";
  }
  cout << endl;

  return 0;
}
Problem p02276 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int main() {
  int i, a, A[50000], z, jsta = 0, jend = 0, B[50000], j;
  scanf("%d", &a);
  for (i = 0; i < a; i++) {
    scanf("%d", &A[i]);
  }
  for (i = 0; i < a; i++) {
    if (A[i] <= A[a - 1]) {
      z = A[jsta];
      A[jsta] = A[i];
      A[i] = z;
      jsta++;
      jend++;
    } else
      jend++;
  }

  for (i = 0; i < a; i++) {
    if (i == jsta - 1)
      printf("[%d] ", A[i]);
    else if (i != a - 1)
      printf("%d ", A[i]);
    else
      printf("%d", A[i]);
  }
  printf("\n");
}


Fixed submission:
#include <stdio.h>
int main() {
  int i, a, A[100000], z, jsta = 0, jend = 0, B[100000], j;
  scanf("%d", &a);
  for (i = 0; i < a; i++) {
    scanf("%d", &A[i]);
  }
  for (i = 0; i < a; i++) {
    if (A[i] <= A[a - 1]) {
      z = A[jsta];
      A[jsta] = A[i];
      A[i] = z;
      jsta++;
      jend++;
    } else
      jend++;
  }

  for (i = 0; i < a; i++) {
    if (i == jsta - 1)
      printf("[%d] ", A[i]);
    else if (i != a - 1)
      printf("%d ", A[i]);
    else
      printf("%d", A[i]);
  }
  printf("\n");
}
Problem p02276 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

// Exchange
void exchange(int A[], int i, int j) {
  int tmp = A[i];
  A[i] = A[j];
  A[j] = tmp;
}

// Partition
int partition(int A[], int l, int r) {
  int threshold = A[r - 1];
  int i = l - 1;
  for (int j = l; j < r - 1; j++) {
    if (A[j] <= threshold) {
      i++;
      // Exchange
      exchange(A, i, j);
    }
  }
  i++;
  // Insert threshold
  exchange(A, i, r - 1);
  return i;
}

// MAIN FUNCTION
int main(int argc, char **argv) {
  // Input
  int A[50000];
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];

  // Partition
  int pivot = partition(A, 0, n);

  // Print all elements
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    if (i == pivot) {
      cout << "[" << A[i] << "]";
    } else {
      cout << A[i];
    }
  }
  cout << endl;

  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

// Exchange
void exchange(int A[], int i, int j) {
  int tmp = A[i];
  A[i] = A[j];
  A[j] = tmp;
}

// Partition
int partition(int A[], int l, int r) {
  int threshold = A[r - 1];
  int i = l - 1;
  for (int j = l; j < r - 1; j++) {
    if (A[j] <= threshold) {
      i++;
      // Exchange
      exchange(A, i, j);
    }
  }
  i++;
  // Insert threshold
  exchange(A, i, r - 1);
  return i;
}

// MAIN FUNCTION
int main(int argc, char **argv) {
  // Input
  int A[1000000];
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];

  // Partition
  int pivot = partition(A, 0, n);

  // Print all elements
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    if (i == pivot) {
      cout << "[" << A[i] << "]";
    } else {
      cout << A[i];
    }
  }
  cout << endl;

  return 0;
}