alexjercan/bugnet huggingface dataset:
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#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;
}

Error: 0

Fixed submission:

#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;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {

  int ns[10001];

  for (int i = 0; i < 10001; i++) {
    ns[i] = 0;
  }

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

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

  int frst = 1;

  for (int i = 0; i < 10001; i++) {
    if (ns[i] != 0) {
      for (int j = 0; j < ns[i]; j++) {
        if (!frst) {
          printf(" ");
        } else {
          frst = 0;
        }
        printf("%d", i);
      }
    }
  }

  printf("\\n");

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
int ara[10001], arra[10001];

int main() {
  int n, k;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &k);
    ara[k]++;
  }
  int j = 0;
  for (int i = 0; i < 10001; i++) {
    if (ara[i] != 0) {
      while (ara[i]--) {
        arra[j] = i;
        j++;
      }
    }
  }
  for (int i = 0; i < j; i++) {
    printf("%d", arra[i]);
    if (i != j - 1)
      printf(" ");
  }
  cout << endl;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#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");
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
ll a[100005];
int main() {
  ll n, i;
  while (scanf("%lld", &n) == 1) {
    for (i = 0; i < n; i++) {
      scanf("%lld", &a[i]);
    }
    sort(a, a + n);
    for (i = 0; i < n - 1; i++) {
      printf("%lld ", a[i]);
    }
    printf("%lld", a[n - 1]);
    printf("\\n");
  }
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

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

int a[2000007];

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

  sort(a, a + n);
  for (int i = 0; i < n; i++)
    (i) ? printf(" %d", a[i]) : printf("%d", a[i]);
  printf("\\n");
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int a[100005];
int main() {
  int n, i;
  while (scanf("%d", &n) == 1) {
    for (i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }
    sort(a, a + n);
    for (i = 0; i < n - 1; i++) {
      printf("%d ", a[i]);
    }
    printf("%d", a[n - 1]);
    printf("\\n");
  }
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

int N;

void countingSort(std::vector<int> &A, std::vector<int> *B, int k) {
  std::vector<int> C(k + 1, 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]]--;
  }
}

int main() {
  std::cin >> N;
  std::vector<int> A;
  int max = 0;
  for (int i = 0; i < N; ++i) {
    int a;
    std::cin >> a;
    max = (a > max) ? a : max;
    A.push_back(a);
  }
  std::vector<int> B(N + 1);
  countingSort(A, &B, max);
  for (int i = 1; i <= N; ++i) {
    std::cout << B[i] << (i == N ? "\\n" : " ");
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

int N;

void countingSort(std::vector<int> &A, std::vector<int> *B, int k) {
  std::vector<int> C(k + 1, 0);

  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]]] = A[j];
    C[A[j]]--;
  }
}

int main() {
  std::cin >> N;
  std::vector<int> A;
  int max = 0;
  for (int i = 0; i < N; ++i) {
    int a;
    std::cin >> a;
    max = (a > max) ? a : max;
    A.push_back(a);
  }
  std::vector<int> B(N);
  countingSort(A, &B, max);
  for (int i = 1; i <= N; ++i) {
    std::cout << B[i] << (i == N ? "\\n" : " ");
  }
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdio>
#include <iostream>

#define NMAX 2000000
#define IMAX 10000
using namespace std;

int main() {
  std::ios_base::sync_with_stdio(false);

  int n;
  int cnt = 0;
  int min = IMAX + 1, max = -1;
  int A[NMAX + 1], C[IMAX + 1] = {0};

  cin >> n;

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

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

  for (int i = min; i <= max;) {
    if (C[i] > 0) {
      A[cnt] = i;
      C[i]--;
      cnt++;
    } else
      i++;
  }

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

  return 0;
}

Error: 0

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>

#define NMAX 200000
#define IMAX 10000
using namespace std;

int main() {
  std::ios_base::sync_with_stdio(false);

  int n;
  int cnt = 0;
  int min = IMAX + 1, max = -1;
  int A[NMAX + 1], B[NMAX + 1], C[IMAX + 1] = {0};

  cin >> n;

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

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

  for (int i = min; i <= max;) {
    if (C[i] > 0) {
      B[cnt] = i;
      C[i]--;
      cnt++;
    } else
      i++;
  }

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

  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

#define maxN 2000001
#define maxData 10000
int boxA[maxN], boxB[maxN], boxC[maxData];

int main(void) {
  /* *** initialize *** */
  int dataN = 0;
  boxA[maxN] = {0};
  boxB[maxN] = {0};
  boxC[maxData] = {0};

  /*  *** input Data *** */
  cin >> dataN;
  //	cout << "dataN = " << dataN << endl;		//Debug

  for (int n = 1; n <= dataN; n++) {
    cin >> boxA[n];
    //		cout << "boxA[" << n << "] = " << boxA[n] << endl;
    ////Debug

    boxC[boxA[n]]++;
    //		cout << "boxC[" << boxA[n] << "] = " << boxC[boxA[n]] << endl;
    ////Debug
  }

  /* *** data analysis *** */
  for (int c = 1; c <= maxData; c++) {
    boxC[c] += boxC[c - 1];
    //		cout << "boxC[" << c << "] = " << boxC[c] << endl;
    ////Debug
  }

  /* *** sort *** */
  for (int b = dataN; b >= 1; b--) {
    boxB[boxC[boxA[b]]] = boxA[b];
    boxC[boxA[b]]--;
  }

  /* *** result *** */
  for (int data = 1; data <= dataN; data++) {
    if (data == dataN) {
      cout << boxB[data] << endl;
    } else {
      cout << boxB[data] << " ";
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

#define maxN 2000000
#define maxData 10001

int main(void) {
  /* *** initialize *** */
  int dataN = 0;
  int boxA[maxData] = {};
  int boxB[maxData] = {};
  int boxC[maxData] = {};

  /*  *** input Data *** */
  cin >> dataN;
  if ((dataN > maxN) && (dataN != 0)) {
    cout << "Invalid Data!!" << endl;
    return 0;
  }

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

  /* *** data analysis *** */
  for (int n = 0; n < dataN; n++) {
    int dataA = boxA[n];
    int count = 0;
    for (int c = 0; c < dataN;
         c++) { /* Count the same number of values???as data A */
      if (boxA[c] == dataA) {
        count++;
      }
    }
    boxC[dataA] = count; /* Acquire data from boxC[0] */
  }

  for (int c = 1; c <= maxData; c++) {
    if (c == 0) {
      boxC[c] = boxC[0];
    } else {
      boxC[c] += boxC[c - 1];
    }
  }

  /* *** sort *** */
  for (int b = 0; b <= dataN; b++) {
    boxB[boxC[boxA[b]]] = boxA[b];
    boxC[boxA[b]]--;
  }

  /* *** result *** */
  for (int data = 1; data <= dataN; data++) {
    if (data == dataN) {
      cout << boxB[data] << endl;
    } else {
      cout << boxB[data] << " ";
    }
  }
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int c[20000 + 200];
int a[3000000 + 200];
int n;

void countSort() {
  memset(c, 0, sizeof(c));
  for (int i = 0; i < n; i++) {
    c[a[i]]++;
  }
  int index = 0;
  for (int i = 0; i < 10000 + 10; i++) {
    while (c[i]) {
      a[index++] = i;
      c[i]--;
    }
    if (index == n)
      break;
  }
  printf("%d", a[0]);
  for (int i = 1; i < n; i++) {
    printf(" %d", a[i]);
  }
  cout << endl;
  return;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  countSort();
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int c[1000 + 20];
int a[2000000 + 200];
int n;

void countSort() {
  memset(c, 0, sizeof(c));
  for (int i = 0; i < n; i++) {
    c[a[i]]++;
  }
  int index = 0;
  for (int i = 0; i < 1000 + 10; i++) {
    while (c[i]) {
      a[index++] = i;
      c[i]--;
    }
  }
  printf("%d", a[0]);
  for (int i = 1; i < n; i++) {
    printf(" %d", a[i]);
  }
  cout << endl;
  return;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  countSort();
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

//
//  main.cpp
//  ALDS1_6
//
//  Created by ptyuan on 2018/10/31.
//  Copyright © 2018 YY. All rights reserved.
//

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int n = 0;

void CountingSort(int *A, int *B, int k) {
  int *C = (int *)malloc(sizeof(int) * k + 1);
  for (int i = 0; i <= k; i++) {
    C[i] = 0;
  }

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

  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]]--;
  }
  free(C);
}

int main(int argc, const char *argv[]) {
  scanf("%d", &n);
  int *A = (int *)malloc(sizeof(int) * n + 1);
  int *B = (int *)malloc(sizeof(int) * n + 1);
  int _max = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", A + i);
    _max = max(_max, A[i]);
  }
  CountingSort(A, B, _max);
  for (int i = 1; i <= n; ++i) {
    if (i > 1) {
      printf(" ");
    }
    printf("%d", *(B + i));
  }
  printf("\\n");
  free(A);
  free(B);
  return 0;
}


Error: 0

Fixed submission:

//
//  main.cpp
//  ALDS1_6
//
//  Created by ptyuan on 2018/10/31.
//  Copyright © 2018 YY. All rights reserved.
//

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int A[1000000];
int B[1000000];
int n = 0;

void CountingSort(int *A, int *B, int k) {
  int *C = (int *)malloc(sizeof(int) * k + 1);
  for (int i = 0; i <= k; i++) {
    C[i] = 0;
  }

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

  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]]--;
  }
  free(C);
}

int main(int argc, const char *argv[]) {
  scanf("%d", &n);
  int _max = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", A + i);
    _max = max(_max, A[i]);
  }
  CountingSort(A, B, _max);
  for (int i = 1; i <= n; ++i) {
    if (i > 1) {
      printf(" ");
    }
    printf("%d", *(B + i));
  }
  printf("\\n");
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
using namespace std;
const int MAX = 10005;
int c[MAX];
int main(void) {
  int n;
  int *a, *b;
  scanf("%d", &n);
  a = new int[n + 1];
  b = new int[n + 1];
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    ++c[a[i]];
  }

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

  for (int i = 0; i < n; ++i) {
    b[c[a[i]]--] = a[i];
  }
  printf("%d", b[1]);
  for (int i = 2; i <= n; ++i) {
    printf(" %d", b[i]);
  }
  putchar('\\n');
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;
const int N = 10005;
int a[N];
int b[N];
int c[N];
int main(void) {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    c[a[i]]++;
  }
  for (int i = 1; i < N; ++i)
    c[i] += c[i - 1];
  for (int i = 0; i < n; ++i)
    b[c[a[i]]--] = a[i];
  for (int i = 1; i <= n; ++i) {
    if (i > 1)
      printf(" ");
    printf("%d", b[i]);
  }
  printf("\\n");
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

/* ALDS1_6_A: Counting Sort */

#include <cstdio>
#include <iostream>

const int MAX_VALUE = 10000;

void counting_sort(int n, int in[], int out[], int max) {
  int counts[max + 1];
  std::fill_n(counts, max + 1, 0);

  for (int i = 0; i < n; i++)
    counts[in[i]]++;

  for (int i_c = 1; i_c <= max; i_c++)
    counts[i_c] += counts[i_c - 1];

  for (int i = n - 1; 0 <= i; i--) {
    out[counts[in[i]] - 1] = in[i];
    counts[in[i]]--;
  }
}

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

  int *in = new int[n];
  for (int i = 0; i < n; i++)
    scanf("%d", &in[i]);

  int *out = new int[n];
  counting_sort(n, in, out, MAX_VALUE);

  printf("%d", out[0]);
  for (int i = 1; i < n; i++)
    printf(" %d", out[i]);
  puts("");

  return 0;
}

Error: 0

Fixed submission:

/* ALDS1_6_A: Counting Sort */

#include <iostream>

const int MAX_VALUE = 10000;

void counting_sort(int n, int in[], int out[], int max) {
  int counts[max + 1];
  std::fill_n(counts, max + 1, 0);

  for (int i = 0; i < n; i++)
    counts[in[i]]++;

  for (int i_c = 1; i_c <= max; i_c++)
    counts[i_c] += counts[i_c - 1];

  for (int i = n - 1; 0 <= i; i--) {
    out[counts[in[i]] - 1] = in[i];
    counts[in[i]]--;
  }
}

int main() {
  std::ios::sync_with_stdio(false);

  int n;
  std::cin >> n;

  int in[n], out[n];
  for (int i = 0; i < n; i++)
    std::cin >> in[i];

  counting_sort(n, in, out, MAX_VALUE);

  std::cout << out[0];
  for (int i = 1; i < n; i++)
    std::cout << " " << out[i];
  std::cout << std::endl;

  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <climits>
#include <cstdio>
#include <iostream>
using namespace std;

void show(int arr[], size_t size) {
  cout << arr[1];
  for (size_t i = 2; i < size; i++) {
    cout << ' ' << arr[i];
  }
  cout << endl;
}

void bucketSort(int arr[], const size_t arr_s, const int min, const int max) {
  //	size_t cnt_s=max - min +1;
  size_t cnt_s = max + 2;
  int *cnt = new int[cnt_s];

  for (size_t i = 0; i < cnt_s; i++) { //
    cnt[i] = 0;
  }

  for (size_t i = 1; i < arr_s; i++) { //
    cnt[arr[i]]++;
  }

  for (size_t i = 1; i < cnt_s; i++) {
    cnt[i] = cnt[i] + cnt[i - 1];
  }

  int *sorted = new int[arr_s];
  for (size_t i = arr_s - 1; i > 0; i--) { //
    sorted[cnt[arr[i]]] = arr[i];
    cnt[arr[i]]--;
  }

  show(sorted, arr_s);

  /*
  for( size_t i=1; i<arr_s; i++ ){
          arr[i]=sorted[i];
  }
  */

  delete[] cnt;
  delete[] sorted;
}

int main() {
  // arr : ???????????????
  size_t arr_s;
  cin >> arr_s;
  arr_s++;
  int *arr = new int[arr_s];

  int max = INT_MIN;
  //	int min=INT_MAX;

  for (size_t i = 1; i < arr_s; i++) {
    scanf("%d", &arr[i]);

    //		if( arr[i] < min ) min=arr[i];
    if (arr[i] > max)
      max = arr[i];
  }

  bucketSort(arr, arr_s, 0, max);
  //	show(arr, arr_s);

  delete[] arr;
}

Error: TLE

Fixed submission:

#include <climits>
#include <cstdio>
#include <iostream>
using namespace std;

void show(int arr[], size_t size) {
  cout << arr[1] << flush;
  for (size_t i = 2; i < size; i++) {
    cout << ' ' << arr[i] << flush;
  }
  cout << endl;
}

void bucketSort(int arr[], const size_t arr_s, const int min, const int max) {
  //	size_t cnt_s=max - min +1;
  size_t cnt_s = max + 2;
  int *cnt = new int[cnt_s];

  for (size_t i = 0; i < cnt_s; i++) { //
    cnt[i] = 0;
  }

  for (size_t i = 1; i < arr_s; i++) { //
    cnt[arr[i]]++;
  }

  for (size_t i = 1; i < cnt_s; i++) {
    cnt[i] = cnt[i] + cnt[i - 1];
  }

  int *sorted = new int[arr_s];
  for (size_t i = arr_s - 1; i > 0; i--) { //
    sorted[cnt[arr[i]]] = arr[i];
    cnt[arr[i]]--;
  }

  show(sorted, arr_s);

  /*
  for( size_t i=1; i<arr_s; i++ ){
          arr[i]=sorted[i];
  }
  */

  delete[] cnt;
  delete[] sorted;
}

int main() {
  // arr : ???????????????
  size_t arr_s;
  cin >> arr_s;
  arr_s++;
  int *arr = new int[arr_s];

  int max = INT_MIN;
  //	int min=INT_MAX;

  for (size_t i = 1; i < arr_s; i++) {
    scanf("%d", &arr[i]);

    //		if( arr[i] < min ) min=arr[i];
    if (arr[i] > max)
      max = arr[i];
  }

  bucketSort(arr, arr_s, 0, max);
  //	show(arr, arr_s);

  delete[] arr;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <tuple>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define square(x) (x) * (x)
#define cube(x) (x) * (x) * (x)

const int INF = 1e9;

using namespace std;
int a[2000000];
int b[2000000];
int c[2000001];

int main() {
  int n;
  int maxim = 0;
  cin >> n;
  REP(i, n) {
    cin >> a[i];
    maxim = max(maxim, a[i]);
  }
  REP(i, maxim + 1) c[i] = 0;
  REP(i, n) c[a[i]]++;
  REP(i, maxim) c[i + 1] += c[i];
  for (int i = n - 1; i > -1; i--) {
    b[c[a[i]] - 1] = a[i];
    c[a[i]]--;
  }

  REP(i, n - 1) cout << b[i] << " ";
  cout << b[n - 1] << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <tuple>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define square(x) (x) * (x)
#define cube(x) (x) * (x) * (x)

const int INF = 1e9;

using namespace std;

int main() {
  int n;
  int maxim = 0;
  cin >> n;
  int a[n], b[n];
  REP(i, n) {
    cin >> a[i];
    maxim = max(maxim, a[i]);
  }
  int c[maxim + 1];
  REP(i, maxim + 1) c[i] = 0;
  REP(i, n) c[a[i]]++;
  REP(i, maxim) c[i + 1] += c[i];
  for (int i = n - 1; i > -1; i--) {
    b[c[a[i]] - 1] = a[i];
    c[a[i]]--;
  }

  REP(i, n - 1) cout << b[i] << " ";
  cout << b[n - 1] << endl;

  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#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;
}


Error: -11

Fixed submission:

#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;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
#define MAXVALUE 10000
#define rep(i, a) for (i = 0; i < a; i++)
using namespace std;

void CountingSort(vector<int> &A, vector<int> &B, int k) {
  int i, j;
  vector<int> C(k + 1, 0);

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

int main(void) {
  vector<int> A;
  vector<int> B;
  int i, n, num;

  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> num;
    A.push_back(num);
  }
  B.resize(n);
  CountingSort(A, B, *max_element(A.begin(), A.end()));

  rep(i, n) {
    cout << B[i];
    if (i != n - 1) {
      cout << " ";
    } else {
      continue;
    }
  }
  cout << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define MAXVALUE 10000
#define rep(i, a) for (i = 0; i < a; i++)
using namespace std;

void CountingSort(vector<int> &A, vector<int> &B, int k) {
  int i, j;
  vector<int> C(k, 0);

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

int main(void) {
  vector<int> A;
  vector<int> B;
  int i, n, num;

  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> num;
    A.push_back(num);
  }
  B.resize(n);
  CountingSort(A, B, *max_element(A.begin(), A.end()));

  rep(i, n) {
    cout << B[i + 1];
    if (i != n - 1) {
      cout << " ";
    } else {
      continue;
    }
  }
  cout << endl;
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
#define max 2000001
#define vmax 10000

int main() {

  int c[vmax + 1];
  int n, i, j;
  cin >> n;
  unsigned short *A, *B;
  A = new unsigned short[n + 1];
  B = new unsigned short[n + 1];
  for (i = 0; i < vmax; i++) {
    c[i] = 0;
  }
  for (i = 1; i <= n; i++) {
    cin >> A[i];
    c[A[i]]++;
  }
  for (i = 1; i <= vmax; i++) {
    c[i] = c[i - 1] + c[i];
  }
  for (j = 0; j <= n; j++) {
    B[c[A[j]]] = A[j];
    c[A[j]]--;
  }
  for (i = 1; i <= n; i++) {
    if (i > 1)
      cout << " ";
    cout << B[i];
  }
  cout << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
#define max 2000001
#define vmax 10000

int main() {

  int c[vmax + 1];
  int n, i, j;
  cin >> n;
  vector<int> A;
  vector<int> B;
  for (i = 0; i < vmax; i++) {
    c[i] = 0;
  }
  for (i = 0; i < n; i++) {
    cin >> A[i];
    c[A[i]]++;
  }
  for (i = 1; i <= vmax; i++) {
    c[i] = c[i - 1] + c[i];
  }
  for (j = 0; j <= n; j++) {
    B[c[A[j]]] = A[j];
    c[A[j]]--;
  }
  for (i = 1; i <= n; i++) {
    if (i > 1)
      cout << " ";
    cout << B[i];
  }
  return 0;
}