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

#include <iostream>
using namespace std;

int main() {
  const int MAX_VALUE = 10000;

  int n;
  cin >> n;

  int A[n + 1], B[n + 1], C[MAX_VALUE + 1];
  for (int i = 0; i <= MAX_VALUE; i++) {
    C[i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
    C[A[i]]++;
  }
  for (int i = 1; i <= MAX_VALUE; i++) {
    C[i] += C[i - 1];
  }
  for (int i = 1; i <= n; i++) {
    B[C[A[i]]--] = A[i];
  }
  for (int i = 1; i <= n; i++) {
    cout << B[i] << (i < n ? " " : "\\n");
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int n;

void CountingSort(int A[], int B[], int k) {
  int C[k + 1];
  /* Initialize */
  for (int i = 0; i <= k; i++) {
    C[k] = 0;
  }
  /* Count the appearance of each number */
  for (int j = 1; j <= n; j++) {
    C[A[j]]++;
  }
  /* Count the appearance of numbers under i*/
  for (int i = 1; i <= k; i++) {
    C[i] = C[i] + C[i - 1];
  }
  for (int j = 1; j <= n; j++) {
    B[C[A[j]]] = A[j];
    C[A[j]]--;
  }
}

int main() {
  cin >> n;
  int A[n + 1], B[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> A[i];
  CountingSort(A, B, 10000);
  for (int i = 1; i <= n; i++) {
    cout << B[i] << (i < n ? " " : "\\n");
  }
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define pi 3.141592653589793

void CountingSort(int a[], int b[], int k) {
  int *c = new int[2000000];
  for (int i = 0; i < 2000000; i++) {
    c[i] = 0;
  }
  for (int i = 0; i < k; i++) {
    c[a[i]]++;
  }
  for (int i = 1; i < 2000000; i++) {
    c[i] = c[i] + c[i - 1];
  }
  for (int i = k - 1; i >= 0; i--) {
    b[c[a[i]] - 1] = a[i];
    c[a[i]]--;
  }
  delete[] c;
  return;
}

int main() {
  int n;
  int *a = new int[2000000];
  int *b = new int[2000000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  CountingSort(a, b, n);
  for (int i = 0; i < n; i++) {
    cout << b[i];
    if (i == n - 1)
      cout << endl;
    else
      cout << " ";
  }
  delete[] a;
  delete[] b;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define pi 3.141592653589793

void CountingSort(int a[], int b[], int k) {
  int c[2000000];
  for (int i = 0; i < k; i++) {
    c[i] = 0;
  }
  for (int i = 0; i < k; i++) {
    c[a[i]]++;
  }
  for (int i = 1; i < k; i++) {
    c[i] = c[i] + c[i - 1];
  }
  for (int i = k - 1; i >= 0; i--) {
    b[c[a[i]] - 1] = a[i];
    c[a[i]]--;
  }
  return;
}

int main() {
  int n;
  int a[2000000], b[2000000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  CountingSort(a, b, n);
  for (int i = 0; i < n; i++) {
    cout << b[i];
    if (i == n - 1)
      cout << endl;
    else
      cout << " ";
  }
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

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

Error: 0

Fixed submission:

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

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

long n;

void CountingSort(long A[], long B[], long k) {
  vector<long> C(k + 1); // count 0 to k
  for (long i = 0; i <= k; i++)
    C[i] = 0; // initialize
  for (long i = 0; i < n; i++)
    C[A[i]]++; // counting
  for (long i = 1; i <= k; i++)
    C[i] += C[i - 1];
  for (long i = 0; i < n; i++) {
    B[C[A[i]] - 1] = A[i];
    C[A[i]]--;
  }
}

int main() {
  cin >> n;
  long *A, *B;
  A = new long[2000000];
  B = new long[2000000];
  for (long i = 0; i < n; i++)
    cin >> A[i];
  CountingSort(A, B, 10000);
  delete[] A;
  for (long i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << B[i];
  }
  cout << endl;
  delete[] B;
  return 0;
}

Error: -11

Fixed submission:

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

long n;

void CountingSort(long A[], long B[], long k) {
  vector<long> C(k + 1, 0);
  for (long i = 0; i <= k; i++)
    C[i] = 0;
  for (long i = 1; i <= n; i++)
    C[A[i]]++;
  for (long i = 1; i <= k; i++)
    C[i] += C[i - 1];
  for (long i = n; i > 0; i--) {
    B[C[A[i]]] = A[i];
    C[A[i]]--;
  }
}

int main() {
  cin >> n;
  long A[2000001], B[2000001] = {};
  for (long i = 0; i < n; i++)
    cin >> A[i];
  CountingSort(A, B, 10000);
  for (long i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << B[i] << endl;
  }
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  while (scanf("%d", &n) != EOF) {
    int k = 0;
    int *A = new int[n + 2];
    int *B = new int[n + 2];
    for (int i = 1; i <= n; i++) {
      scanf("%d", &A[i]);
      if (k < A[i])
        k = A[i];
    }
    Counting_Sort(A, B, k);

    for (int i = 1; i <= n; i++) {

      cout << B[i];

      if (i != n)
        cout << " ";
    }
    cout << endl;
    delete[] A;
    delete[] B;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  while (cin >> n) {
    int k = 0;
    int *A = new int[n + 2];
    int *B = new int[n + 2];
    for (int i = 1; i <= n; i++) {
      cin >> A[i];
      if (k < A[i])
        k = A[i];
    }
    Counting_Sort(A, B, k);

    for (int i = 1; i <= n; i++) {

      cout << B[i];

      if (i != n)
        cout << " ";
    }
    cout << endl;
    delete[] A;
    delete[] B;
  }

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

#include <iostream>
using namespace std;
static const int MAX = 10000;
int Count[MAX + 1];

int main() {
  int n, m;
  cin >> n;
  for (int i = 0; i <= MAX; i++)
    Count[i] = 0;
  for (int i = 0; i < n; i++) {
    cin >> m;
    Count[m]++;
  }

  for (int i = 0, c = 0; c < n; i++) {
    if (Count[i] > 0) {
      while (Count[i]--) {
        if (c)
          cout << " ";
        cout << i;
        c++;
      }
    }
  }
  cout << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;
static const int MAX = 10000;
int Count[MAX + 1];

int main() {
  int n, m;
  cin >> n;
  for (int i = 0; i <= MAX; i++)
    Count[i] = 0;
  for (int i = 0; i < n; i++) {
    cin >> m;
    Count[m]++;
  }
  int c = 0;
  for (int i = 0; c <= n; i++) {
    if (Count[i] > 0) {
      while (Count[i]--) {
        if (c)
          cout << " ";
        cout << i;
        c++;
      }
    }
  }
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
using namespace std;

int n;

int main() {
  scanf("%d", &n);
  int a[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  sort(a, a + n);
  printf("%d", a[0]);
  for (int i = 1; i < n; i++)
    printf(" %d", a[i]);
  printf("\\n");
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
using namespace std;

int n;
void CountingSort(int a[], int b[], int k) {
  int c[k + 1];
  memset(c, 0, sizeof(c));
  for (int i = 0; i < n; i++)
    c[a[i]]++;
  for (int i = 1; i <= k; i++)
    c[i] += c[i - 1];
  for (int i = n - 1; i >= 0; i--)
    b[--c[a[i]]] = a[i];
}

int main() {
  scanf("%d", &n);
  int a[n];
  int max = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    max = max > a[i] ? max : a[i];
  }
  int b[n];
  CountingSort(a, b, max);
  printf("%d", b[0]);
  for (int i = 1; i < n; i++)
    printf(" %d", b[i]);
  printf("\\n");
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

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

#define A_MAX 2000001
#define C_MAX 10001

int A[A_MAX], B[A_MAX], C[C_MAX];

int main(void) {
  int i;
  int n;

  scanf("%d", &n);

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

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

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

  printf("\\n");

  return 0;
}


Error: 0

Fixed submission:

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

#define MAX 10005

int A[MAX], B[MAX], C[MAX];

int main(void) {
  int i;
  int n;

  scanf("%d", &n);

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

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

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

  printf("\\n");

  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: 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);
    ++outIt;
    if (outIt == out.end()) {
      break;
    } else {
      printf(" ");
    }
  }
  printf("\\n");
  return 0;
}


Error: TLE

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);
    if (outIt == out.end()) {
      break;
    } else {
      printf(" ");
    }
  }
  printf("\\n");
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define k 10000
int n, C[k];

int main() {
  scanf("%d", &n);
  int *A = (int *)malloc(sizeof(int) * n);
  int *B = (int *)malloc(sizeof(int) * n);

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

  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 - 1];
  for (int i = n - 1; i >= 0; i--) {
    B[C[A[i]] - 1] = A[i];
    C[A[i]]--;
  }

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

Error: 0

Fixed submission:

#include <stdio.h>
using namespace std;
#define k 10000
int n;
int B[k];

int CountingSort(int A[k]) {
  int C[k];
  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 - 1];
  for (int i = n - 1; i >= 0; i--) {
    B[C[A[i]] - 1] = A[i];
    C[A[i]]--;
  }
  return 0;
}

int main() {
  int A[k];
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }
  CountingSort(A);
  for (int i = 0; i < n - 1; i++) {
    printf("%d ", B[i]);
  }
  printf("%d\\n", B[n - 1]);

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

#include <stdio.h>
int main() {
  int n, a[10001], temp, low = 10001;
  for (int i = 0; i < 10001; i++) {
    a[i] = 0;
  }
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &temp);
    if (temp < low)
      low = temp;
    a[temp]++;
  }
  printf("%d", low);
  a[low]--;
  for (int i = 0; i < 10001; i++) {
    while (a[i] > 0) {
      a[i]--;
      printf(" %d", i);
    }
  }
  printf("\\n");
}

Error: 0

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int main() {
  int n, a[1000000];
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  sort(a, a + n);
  printf("%d", a[0]);
  for (int i = 1; i < n; i++)
    printf(" %d", a[i]);
  printf("\\n");
}
Problem p02275 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#define MAX 2000001
#define VMAX 10000

int A[MAX], B[MAX];
int C[VMAX + 1];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i + 1]);
    C[A[i + 1]]++;
  }
  for (int i = 1; i <= VMAX; 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;
}


Error: TLE

Fixed submission:

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

int main() {
  int A[2000001], B[2000001];
  int C[10001], n;
  cin >> n;
  for (int i = 0; i <= 10000; 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 <= 10000; i++) {
    C[i] += C[i - 1];
  }
  for (int i = 1; i <= n; i++) {
    B[C[A[i]]] = A[i];
    C[A[i]]--;
  }
  for (int i = 1; i <= n; i++) {
    if (i > 1) {
      cout << " " << flush;
    }
    cout << B[i] << flush;
  }
  cout << endl;
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, b, n) for (int i = b; i < n; i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int k = 10000;
int n;

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

int main() {
  cin >> n;
  int *A;
  int *B;
  A = new int[n];
  B = new int[n];
  memset(A, 0, sizeof(A));
  memset(B, 0, sizeof(B));
  rep(i, n) cin >> A[i];
  countingsort(A, B);
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << B[i];
  }
  cout << endl;
  delete[] A;
  delete[] B;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, b, n) for (int i = b; i < n; i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int k = 10000;
int n;

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

int main() {
  cin >> n;
  int A[n + 1];
  int B[n + 1];
  memset(A, 0, sizeof(A));
  memset(B, 0, sizeof(B));
  rep(i, n) cin >> A[i];
  countingsort(A, B);
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << B[i];
  }
  cout << endl;
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: 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 < 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;
}

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

#include <stdio.h>
#include <stdlib.h>
#define MAX 2000000
#define VMAX 10000

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

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

  for (i = n; i > 0; 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;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#define MAX 2000000
#define VMAX 10000
int A[MAX], B[MAX];
int C[VMAX + 1];
int n;

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

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }
  CountingSort(A, B, VMAX + 1);
  for (int i = 0; i < n; i++) {
    if (i)
      printf(" ");
    printf("%d", &B[i]);
  }
  printf("\\n");
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>
using namespace std;
int a[10000001], b[10000000], c[10000000];
int main() {
  int n, i, j;
  scanf("%d", &n);
  for (i = 0; i < 10000; i++) {
    c[i] = 0;
  }
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i + 1]);
    c[a[i + 1]]++;
  }
  for (i = 1; i <= 10000; i++) {
    c[i] = c[i] + c[i - 1];
  }
  for (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;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAX 100000000
int A[MAX], B[MAX], C[MAX] = {0};
int main() {
  int k, n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
  }
  for (int i = 1; i <= MAX; i++) {
    C[i] = 0;
  }
  for (int j = 1; j <= n; j++) {
    C[A[j]]++;
  }
  for (int i = 1; i <= MAX; 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 (int i = 1; i <= n; i++) {
    if (i > 1)
      printf(" ");
    printf("%d", B[i]);
  }
  printf("\\n");
}