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

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1000010;

int cnt = 0, G[100];

void Insertion_Sort(int s, int t, int n, int *A) {
  for (int i = s + t; i < n; i += t) {
    int key = A[i], j = i - t;
    while (j >= 0 && A[j] > key) {
      A[j + t] = A[j];
      j -= t;
      cnt++;
    }
    if (A[j + t] != key) {
      A[j + t] = key;
      // cnt++;
    }
  }
}

int main() {
  for (int i = 0; (G[i] = G[i - 1] * 3 + 1) <= MAXN; i++)
    ;
  static int n, A[MAXN];
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);
  int t;
  for (t = 0; G[t] <= n; t++)
    ;
  printf("%d\\n", t);
  for (int i = t - 1; i >= 0; i--)
    printf("%d%c", G[i], i == 0 ? '\\n' : ' ');
  for (int i = t - 1; i >= 0; i--)
    for (int j = 0; j < G[i]; j++)
      Insertion_Sort(j, G[i], n, A);
  printf("%d\\n", cnt);
  for (int i = 0; i < n; i++)
    printf("%d\\n", A[i]);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 200, G[] = {1, 4, 13, 40, 121};

int cnt = 0;

void Insertion_Sort(int s, int t, int n, int *A) {
  for (int i = s + t; i < n; i += t) {
    int key = A[i], j = i - t;
    while (j >= 0 && A[j] > key) {
      A[j + t] = A[j];
      j -= t;
      cnt++;
    }
    if (A[j + t] != key) {
      A[j + t] = key;
      // cnt++;
    }
  }
}

int main() {
  int n, A[MAXN];
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);
  int t;
  for (t = 0; G[t] <= n; t++)
    ;
  printf("%d\\n", t);
  for (int i = t - 1; i >= 0; i--)
    printf("%d%c", G[i], i == 0 ? '\\n' : ' ');
  for (int i = t - 1; i >= 0; i--)
    for (int j = 0; j < G[i]; j++)
      Insertion_Sort(j, G[i], n, A);
  printf("%d\\n", cnt);
  for (int i = 0; i < n; i++)
    printf("%d\\n", A[i]);
  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
int n, a[1111111], c, g[20];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  g[0] = 1;
  c = 0;
  while (true) {
    g[c + 1] = g[c] * 3 + 1;
    if (g[++c] >= n)
      break;
  }
  int ret = 0;
  for (int i = c - 1; i >= 0; i--) {
    for (int j = g[i]; j < n; j++) {
      int v = a[j], w = j - g[i];
      while (w >= 0 && a[w] > v) {
        a[w + g[i]] = a[w];
        w -= g[i];
        ret++;
      }
      a[w + g[i]] = v;
    }
  }
  cout << c << endl;
  for (int i = c - 1; i >= 0; i--) {
    if (i)
      cout << g[i] << ' ';
    else
      cout << g[i] << endl;
  }
  cout << ret << endl;
  for (int i = 0; i < n; i++) {
    cout << a[i] << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int n, a[1111111], c, g[20];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  g[0] = 1;
  c = 0;
  while (true) {
    g[c + 1] = g[c] * 3 + 1;
    if (g[++c] >= n)
      break;
  }
  int ret = 0;
  for (int i = c - 1; i >= 0; i--) {
    for (int j = g[i]; j < n; j--) {
      int v = a[j], w = j - g[i];
      while (w >= 0 && a[w] > v) {
        a[w + g[i]] = a[w];
        w -= g[i];
        ret++;
      }
      a[w + g[i]] = v;
    }
  }
  cout << c << endl;
  for (int i = c - 1; i >= 0; i--) {
    if (i)
      cout << g[i] << ' ';
    else
      cout << g[i] << endl;
  }
  cout << ret << endl;
  for (int i = 0; i < n; i++) {
    cout << a[i] << endl;
  }
  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void insertionSort(vector<int> &a, int n, int g, int &cnt) {
  int v, j;
  for (int i = g; i < n; i++) {
    v = a[i];
    j = i - g;
    while (j >= 0 && a[j] > v) {
      a[j + g] = a[j];
      j -= g;
      cnt++;
    }
    a[j + g] = v;
  }
}

int shellSort(vector<int> &a, vector<int> G, int m, int n) {
  int cnt = 0;
  for (int i = 0; i < m; i++) {
    insertionSort(a, n, G[i], cnt);
  }
  return cnt;
}

int main() {
  int n, m = 0, cnt;
  cin >> n;
  vector<int> a(n), G(100);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  G[0] = 1;
  while (n >= G[m++])
    G[m] = 3 * G[m - 1] + 1;
  m--;
  reverse(G.begin(), G.begin() + m);
  cout << m << endl;
  for (int i = 0; i < m; i++) {
    cout << G[i];
    if (i != m - 1)
      cout << " ";
  }
  cnt = shellSort(a, G, m, n);
  cout << endl << cnt << endl;
  for (int i = 0; i < n; i++)
    cout << a[i] << endl;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void insertionSort(vector<int> &a, int n, int g, int &cnt) {
  int v, j;
  for (int i = g; i < n; i++) {
    v = a[i];
    j = i - g;
    while (j >= 0 && a[j] > v) {
      a[j + g] = a[j];
      j -= g;
      cnt++;
    }
    a[j + g] = v;
  }
}

int shellSort(vector<int> &a, vector<int> G, int m, int n) {
  int cnt = 0;
  for (int i = 0; i < m; i++) {
    insertionSort(a, n, G[i], cnt);
  }
  return cnt;
}

int main() {
  int n, m, cnt;
  cin >> n;
  vector<int> a(n), G(100);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  G[0] = 1;
  // for (int i = 1; i < 100; i++) ;
  for (m = 0; n >= G[m]; m++)
    G[m] = 3 * G[m - 1] + 1;
  reverse(G.begin(), G.begin() + m);
  cout << m << endl;
  for (int i = 0; i < m; i++) {
    cout << G[i];
    if (i != m - 1)
      cout << " ";
  }
  cnt = shellSort(a, G, m, n);
  cout << endl << cnt << endl;
  for (int i = 0; i < n; i++)
    cout << a[i] << endl;
}
Problem p02262 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

void i_sort(int *a, int n, int g, int *count) {
  int j, v;
  for (int i = g; i < n; ++i) {
    v = a[i];
    j = i - g;
    while (j >= 0 && a[j] > v) {
      a[j + g] = a[j];
      j = j - g;
      a[j + g] = v;
      (*count)++;
    }
  }
}

void s_sort(int *a, int n, int *cnt) {
  *cnt = 0;
  vector<int> g;
  int m = 0, tmp = 1;
  for (;;) {
    g.push_back(tmp);
    tmp = tmp * 4 + 1;
    m++;
    if (tmp >= n)
      break;
  }
  cout << m << endl;
  reverse(g.begin(), g.end());
  for (int i = 0; i < m; ++i) {
    i_sort(a, n, g[i], cnt);
    cout << g[i];
    if (i != m)
      cout << " ";
  }
  cout << endl;
}

int main() {
  int n, count;
  cin >> n;
  int *a = new int[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  s_sort(a, n, &count);
  cout << count << endl;
  for (int i = 0; i < n; ++i) {
    cout << a[i] << endl;
  }
  delete[] a;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

void i_sort(int *a, int n, int g, int *count) {
  int j, v;
  for (int i = g; i < n; ++i) {
    v = a[i];
    j = i - g;
    while (j >= 0 && a[j] > v) {
      a[j + g] = a[j];
      j = j - g;
      a[j + g] = v;
      (*count)++;
    }
  }
}

void s_sort(int *a, int n, int m, int *g, int *cnt) {
  *cnt = 0;
  for (int i = 0; i < m; ++i) {
    i_sort(a, n, g[i], cnt);
  }
}

int main() {
  random_device rnd;
  mt19937 mt(rnd());
  int n, m, count;
  cin >> n;
  int *a = new int[n];
  int *g = new int[n / 8 + 1];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  auto make_rand = bind(uniform_int_distribution<int>(1, n), mt);
  g[0] = n - make_rand();
  for (int i = 1; i < n / 8; ++i) {
    int tmp = make_rand();
    if (g[i - 1] < tmp) {
      i--;
      continue;
    }
    g[i] = tmp;
  }
  g[n / 8] = 1;
  if (n / 8 > 100) {
    m = 100;
    g[99] = 1;
  } else
    m = n / 8 + 1;
  s_sort(a, n, m, g, &count);
  cout << m << endl;
  for (int i = 0; i < m - 1; ++i) {
    cout << g[i] << " ";
  }
  cout << g[m - 1] << endl << count << endl;
  for (int i = 0; i < n; ++i) {
    cout << a[i] << endl;
  }
  delete[] a;
  delete[] g;
  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

int insertion_sort(int *seq, int length, int g) {
  int temp, count = 0;
  int v, j;
  for (int i = g; i < length; i++) {
    v = seq[i];
    j = i - g;
    while (j >= 0 && seq[j] > v) {
      seq[j + g] = seq[j];
      j = j - g;
      count++;
    }
    seq[j + g] = v;
  }
  return count;
}

int shellSort(int *A, int n) {
  int cnt = 0, m;
  int i = 1;
  int G[1000000];

  for (int i = 1, j = 0; i < n; j++, i *= 2) {
    G[j] = i;
    m = j;
  }

  cout << m + 1 << endl;
  for (int i = m; i >= 0; i--) {
    cnt += insertion_sort(A, n, G[i]);
    cout << G[i] << " ";
  }
  cout << endl;
  return cnt;
}

int main() {
  int N, count;
  int seq[1000000];
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> seq[i];

  count = shellSort(seq, N);
  cout << count << endl;
  for (int k = 0; k < N; k++)
    cout << seq[k] << endl;
}


Error: WA

Fixed submission:

#include <iostream>

using namespace std;

int insertion_sort(int *seq, int length, int g) {
  int temp, count = 0;
  int v, j;
  for (int i = g; i < length; i++) {
    v = seq[i];
    j = i - g;
    while (j >= 0 && seq[j] > v) {
      seq[j + g] = seq[j];
      j = j - g;
      count++;
    }
    seq[j + g] = v;
  }
  return count;
}

int shellSort(int *A, int n) {
  int cnt = 0, m;
  int i = 1;
  int G[1000000];

  for (int i = 1, j = 0; i < n; j++, i *= 2) {
    G[j] = i;
    m = j;
  }

  cout << m + 1 << endl;
  for (int i = m; i >= 0; i--) {
    cnt += insertion_sort(A, n, G[i]);
    cout << G[i] << " ";
  }
  cout << endl;
  return cnt;
}

int main() {
  int N, count;
  int seq[100];
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> seq[i];

  count = shellSort(seq, N);
  cout << count << endl;
  for (int k = 0; k < N; k++)
    cout << seq[k] << endl;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

// ConsoleApplication1.cpp : Defines the entry point for the console
// application.
//

// #include "stdafx.h"
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

long long cnt = 0;
vector<int> g;

int shell(int n) {
  int m = 0;
  for (int i = 1;; i = i * 3 + 1, m++) {
    if (i > n)
      break;
    g.push_back(i);
  }
  return m;
}

void insertionSort(int a[], int n, int g) {
  int i, j;
  for (i = g; i < n; i++) {
    j = i;
    int v = a[i];
    while (j - g >= 0 && a[j - g] > v) {
      a[j] = a[j - g];
      j -= g;
      cnt++;
    }
    a[j] = v;
  }
}

int main() {
  int a[1000001];
  int m;
  int n, i, j;
  cin >> n;
  m = shell(n);
  for (i = 0; i < n; i++)
    cin >> a[i];

  cout << m << endl;
  for (i = m - 1; i >= 0; i--) {
    insertionSort(a, n, g[i]);
    cout << g[i];
    if (i)
      cout << ' ';
  }
  cout << "\\n" << cnt << endl;
  for (i = 0; i < n; i++)
    cout << a[i] << endl;
  return 0;
}


Error: 0

Fixed submission:

// ConsoleApplication1.cpp : Defines the entry point for the console
// application.
//

// #include "stdafx.h"
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

long long cnt = 0;
vector<int> g;

int shell(int n) {
  int m = 0;
  for (int i = 1;; i = i * 3 + 1, m++) {
    if (i > n)
      break;
    g.push_back(i);
  }
  return m;
}

void insertionSort(int a[], int n, int g) {
  int i, j;
  for (i = g; i < n; i++) {
    j = i;
    int v = a[i];
    while (j - g >= 0 && a[j - g] > v) {
      a[j] = a[j - g];
      j -= g;
      cnt++;
    }
    a[j] = v;
  }
}

int main() {
  int a[101];
  int m;
  int n, i, j;
  cin >> n;
  m = shell(n);
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);

  cout << m << endl;
  for (i = m - 1; i >= 0; i--) {
    insertionSort(a, n, g[i]);
    cout << g[i];
    if (i)
      cout << ' ';
  }
  cout << "\\n" << cnt << endl;
  for (i = 0; i < n; i++)
    cout << a[i] << endl;
  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <utility>
#include <vector>
using namespace std;

static inline void pn(int n) {
  if (n == 0)
    putchar('0');
  else {

    int i;
    static char buf[20];
    for (i = 0; n; n /= 10, i++)
      buf[i] = n % 10 + '0';
    while (i--)
      putchar(buf[i]);
  }
  putchar('\\n');
}

int main() {
  int n, cnt = 0;

  scanf("%d", &n);

  vector<int> A(n), G;
  for (int &x : A)
    scanf("%d", &x);

  for (int i = 1, x = 1; x <= n; x = (pow(3, ++i) - 1) / 2)
    G.push_back(x);
  reverse(G.begin(), G.end());

  for (int g : G)
    for (auto b = A.begin(), i = b + g, e = A.end(); i != e; i++) {
      int v = *i;
      auto j = i - g;
      for (; j >= b && *j > v; j -= g)
        *(j + g) = *j, cnt++;
      *(j + g) = v;
    }

  pn(G.size());
  for (int &x : G)
    printf("%s%d", (&x == &G[0] ? "" : " "), x);
  putchar('\\n');
  pn(cnt);
  for (int x : A) {
    pn(x);
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <utility>
#include <vector>
using namespace std;

static inline void p(int x) { printf("%d\\n", x); }

int main() {
  int n, cnt = 0;

  scanf("%d", &n);

  vector<int> A(n), G;
  for (int &x : A)
    scanf("%d", &x);

  for (int i = 1, x = 1; x <= n; x = (pow(3, ++i) - 1) / 2)
    G.push_back(x);
  reverse(G.begin(), G.end());

  for (int g : G)
    for (auto b = A.begin(), i = b + g, e = A.end(), j = i; i != e; j = ++i) {
      int v = *i;
      for (; j > b && *(j - g) > v; j -= g)
        *j = *(j - g), cnt++;
      *j = v;
    }

  p(G.size());
  for (int &x : G)
    printf("%s%d", (&x == &G[0] ? "" : " "), x);
  puts("");
  p(cnt);
  for (int x : A)
    p(x);
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

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

void insertionSort(int A[], const int n, const int g, int *cnt) {
  for (int i = g; i < n; ++i) {
    int tmp = A[i];
    int j = i - g;
    while (-1 < j && tmp < A[j]) {
      // cout << A[j+g] << "<-->" << A[j] << endl;
      A[j + g] = A[j];
      j = j - g;
      ++*cnt;
    }
    A[j + g] = tmp;
  }
}

void shellSort(int A[], const int n) {
  int cnt = 0;
  int m;
  int G[100];
  for (int i = 0; i < 100; ++i) {
    if (i == 0)
      G[i] = n / 2 + 1;
    else
      G[i] = G[i - 1] / 2 - 1;
    if (G[i] < 0) {
      G[i - 1] = 1;
      G[i] = 0;
      m = i;
      break;
    }
  }

  for (int i = 0; i < m; ++i)
    insertionSort(A, n, G[i], &cnt);

  // 出力
  printf("%d\\n", m);
  for (int i = 0; i < m; ++i) {
    if (i)
      printf(" ");
    printf("%d", G[i]);
  }
  printf("\\n%d\\n", cnt);
  for (int i = 0; i < n; ++i)
    printf("%d\\n", A[i]);
}

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

  // シェルソート
  shellSort(A, n);

  return 0;
}

Error: 0

Fixed submission:

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

void insertionSort(int A[], const int n, const int g, int *cnt) {
  for (int i = g; i < n; ++i) {
    int tmp = A[i];
    int j = i - g;
    while (-1 < j && tmp < A[j]) {
      A[j + g] = A[j];
      j = j - g;
      ++*cnt;
    }
    A[j + g] = tmp;
  }
}

void shellSort(int A[], const int n) {
  int cnt = 0;
  int m = (int)(n / 2);
  if (100 < m)
    m = 100;
  int G[m];
  for (int i = 0; i < m; ++i) {
    if (i < 1)
      G[i] = n - 1;
    else if (i == m - 1)
      G[i] = 1;
    else
      G[i] = G[i - 1] / 2 - 1;
  }

  for (int i = 0; i < m; ++i)
    insertionSort(A, n, G[i], &cnt);

  // 出力
  printf("%d\\n", m);
  for (int i = 0; i < m; ++i) {
    if (i)
      printf(" ");
    printf("%d", G[i]);
  }
  printf("\\n%d\\n", cnt);
  for (int i = 0; i < n; ++i)
    printf("%d\\n", A[i]);
}

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

  // シェルソート
  shellSort(A, n);

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

long long cnt;
int l;
int A[1000000];
int n;
vector<int> G;

// 間隔gを指定した挿入ソート
void insertionSort(int A[], int n, int g) {
  for (int i = g; i < n; i++) {
    int v = A[i];
    int j = i - g;
    while (j >= 0 && A[j] > v) {
      A[j + g] = A[j];
      j -= g;
      cnt++;
    }
    A[j + g] = v;
  }
}

void shellSort(int A[], int n) {
  // 数列 G = { 1, 4, 13, 40, 121, 364, 1093, ... } を生成
  for (int h = 1;;) {
    if (h > n)
      break;
    G.push_back(h);
    h = 3 * h + 1;
  }

  for (int i = G.size() - 1; i >= 0; i--) { // 逆順に G[i] = g を指定
    insertionSort(A, n, G[i]);
  }
}

int main() {
  cin >> n;
  // より速い入力scanf関数を使用
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);
  cnt = 0;

  shellSort(A, n);

  cout << G.size() << endl;
  for (int i = G.size() - 1; i >= 0; i--) {
    printf("%d", G[i]);
    if (i)
      printf(" ");
  }
  printf("\\n");
  printf("%d\\n", cnt);
  for (int i = 0; i < n; i++)
    printf("%d\\n", A[i]);

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

long long cnt;
int l;
int A[1000000];
int n;
vector<int> G;

// 間隔gを指定した挿入ソート
void insertionSort(int A[], int n, int g) {
  for (int i = g; i < n; i++) {
    int v = A[i];
    int j = i - g;
    while (j >= 0 && A[j] > v) {
      A[j + g] = A[j];
      j -= g;
      cnt++;
    }
    A[j + g] = v;
  }
}

void shellSort(int A[], int n) {
  // 数列 G = { 1, 4, 13, 40, 121, 364, 1093, ... } を生成
  for (int h = 1;;) {
    G.push_back(h);
    h = 3 * h + 1;
  }

  for (int i = G.size() - 1; i >= 0; i--) { // 逆順に G[i] = g を指定
    insertionSort(A, n, G[i]);
  }
}

int main() {
  cin >> n;
  // より速い入力scanf関数を使用
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);
  cnt = 0;

  shellSort(A, n);

  cout << G.size() << endl;
  for (int i = G.size() - 1; i >= 0; i--) {
    printf("%d", G[i]);
    if (i)
      printf(" ");
  }
  printf("\\n");
  printf("%d\\n", cnt);
  for (int i = 0; i < n; i++)
    printf("%d\\n", A[i]);

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

#include <iostream>
#include <string>
using namespace std;
long d = 0, z = 0, b[1000000];
void ChentrucTiep(long long a[], long n, long h) {
  int x, k;
  for (int i = h; i < n; i++) {
    x = a[i];
    k = i - h;
    while (k >= 0 && a[k] > x) {
      a[k + h] = a[k];
      k -= h;
      d++;
    }
    a[k + h] = x;
  }
}
void shellSort(long long a[], long n) {

  for (int i = 1; i <= n;) {
    b[z] = i;
    z += 1;
    i = i * 3 + 1;
  }
  for (int x = z - 1; x >= 0; x--)
    ChentrucTiep(a, n, b[x]);
}

int main() {
  long n;
  long long a[1000000];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  shellSort(a, n);
  cout << z << endl;
  for (int i = z - 1; i >= 0; i--) {
    cout << b[i];
    if (i)
      cout << " ";
  }
  cout << endl << d << endl;
  for (int i = 0; i < n; i++) {
    cout << a[i] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
long d = 0, z = 0, b[1000000];
void ChentrucTiep(long long a[], long n, long h) {
  int x, k;
  for (int i = h; i < n; i++) {
    x = a[i];
    k = i - h;
    while (k >= 0 && a[k] > x) {
      a[k + h] = a[k];
      k -= h;
      d++;
    }
    a[k + h] = x;
  }
}
void shellSort(long long a[], long n) {

  for (int i = 1; i <= n;) {
    b[z] = i;
    z += 1;
    i = i * 3 + 1;
  }
  for (int x = z - 1; x >= 0; x--)
    ChentrucTiep(a, n, b[x]);
}

int main() {
  long n;
  long long a[100000];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  shellSort(a, n);
  cout << z << endl;
  for (int i = z - 1; i >= 0; i--) {
    cout << b[i];
    if (i)
      cout << " ";
  }
  cout << endl << d << endl;
  for (int i = 0; i < n; i++) {
    cout << a[i] << endl;
  }

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

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

int cnt = 0;
void insertionSort(vector<long> &A, int n, int g) {
  long i, j, v;
  for (i = g; i < n; ++i) {
    v = A[i];
    j = i - g;
    while (j >= 0 && A[j] > v) {
      A[j + g] = A[j];
      j = j - g;
      ++cnt;
    }
    A[j + g] = v;
  }
}

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

  long n, i, j = 13;
  vector<long> A;

  cin >> n;
  A.resize(n, 0);
  for (i = 0; i < n; ++i)
    cin >> A[i];

  long G[13] = {1,    4,    13,    40,    121,    364,   1093,
                3280, 9841, 29524, 88573, 265720, 797161};
  for (i = 12; i >= 0; --i) {
    if (G[i] >= n) {
      j = i;
      continue;
    }

    insertionSort(A, n, G[i]);
  }
  if (0 == j)
    j = 1;

  cout << j << "\\n";
  for (i = j - 1; i >= 0; --i) {
    cout << G[i];
    if (i != 0)
      cout << " ";
  }
  cout << "\\n" << cnt << "\\n";

  for (i = 0; i < n; ++i)
    cout << A[i] << "\\n";

  return 0;
}

Error: 0

Fixed submission:

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

int cnt = 0;
void insertionSort(vector<long> &A, int n, int g) {
  int i, j, v;
  for (i = g; i < n; ++i) {
    v = A[i];
    j = i - g;
    while (j >= 0 && A[j] > v) {
      A[j + g] = A[j];
      j = j - g;
      ++cnt;
    }
    A[j + g] = v;
  }
}

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

  int n, i, j;
  vector<long> A;

  cin >> n;
  A.resize(n, 0);
  for (i = 0; i < n; ++i)
    cin >> A[i];

  int G[13] = {1,    4,    13,    40,    121,    364,   1093,
               3280, 9841, 29524, 88573, 265720, 797161};
  for (i = 12; i >= 0; --i) {
    if (G[i] >= n) {
      j = i;
      continue;
    }

    insertionSort(A, n, G[i]);
  }
  if (0 == j)
    j = 1;

  cout << j << "\\n";
  for (i = j - 1; i >= 0; --i) {
    cout << G[i];
    if (i != 0)
      cout << " ";
  }
  cout << "\\n" << cnt << "\\n";

  for (i = 0; i < n; ++i)
    cout << A[i] << "\\n";

  return 0;
}