alexjercan/bugnet huggingface dataset:
Problem p02262 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
long long cnt = 0;
void setsort(vector<int> &, int);
void shell(vector<int> &, int, int);
int main() {
  int n;
  vector<int> t(1000001, 0);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> t[i];
  }
  setsort(t, n);
  for (int i = 0; i < n; ++i) {
    cout << t[i] << endl;
  }
  return 0;
}
void setsort(vector<int> &t, int n) {
  cnt = 0;
  long long m = 0, k = 0, num, h = 0;
  vector<int> g(n + 1, 0);
  num = n - 1;
  for (int i = n / 2; i >= 1; i /= 2) {
    g[k++] = i;
    m++;
  }
  if (m == 0) {
    g[k++] = 1;
    m++;
  }
  for (int i = 0; i < m; ++i) {
    shell(t, n, g[i]);
  }
  cout << m << endl;
  for (int i = 0; i < m; ++i) {
    cout << (i == 0 ? "" : " ") << g[i];
  }

  cout << endl << cnt << endl;
  return;
}
void shell(vector<int> &t, int n, int g) {
  int v, j;
  for (int i = g; i < n; ++i) {
    v = t[i];
    j = i - g;
    while (j >= 0 && t[j] > v) {
      t[j + g] = t[j];
      j = j - g;
      cnt++;
      t[j + g] = v;
    }
  }
  return;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
long long cnt = 0;
void setsort(vector<int> &, int);
void shell(vector<int> &, int, int);
int main() {
  int n;
  vector<int> t(1000001, 0);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> t[i];
  }
  setsort(t, n);
  for (int i = 0; i < n; ++i) {
    cout << t[i] << endl;
  }
  return 0;
}
void setsort(vector<int> &t, int n) {
  cnt = 0;
  long long m = 0, k = 0, num, h = 0;
  vector<int> g(n + 1, 0);
  num = n - 1;
  while (h <= n / 9)
    h = 3 * h + 1;
  for (; h > 0; h /= 3) {
    g[k++] = h;
    m++;
  }
  if (m == 0) {
    m++;
    g[k++] = 1;
  }
  for (int i = 0; i < n; ++i) {
    shell(t, n, g[i]);
  }
  cout << m << endl;
  for (int i = 0; i < m; ++i) {
    cout << (i == 0 ? "" : " ") << g[i];
  }

  cout << endl << cnt << endl;
  return;
}
void shell(vector<int> &t, int n, int g) {
  int v, j;
  for (int i = g; i < n; ++i) {
    v = t[i];
    j = i - g;
    while (j >= 0 && t[j] > v) {
      t[j + g] = t[j];
      j = j - g;
      cnt++;
      t[j + g] = v;
    }
  }
  return;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

#include <cstdlib>
#include <iostream>
#include <vector>
// shellSort(A, n) は、一定の間隔 g
// だけ離れた要素のみを対象とした挿入ソートである insertionSort(A, n, g)
// を、最初は大きい値から g
// を狭めながら繰り返します。これをシェルソートと言います。 上の疑似コードの ?
// を埋めてこのプログラムを完成させてください。n と数列 A
// が与えられるので、疑似コード中の m、m 個の整数 Gi(i=0,1,...,m－1)、入力
// Aを昇順にした列を出力するプログラムを作成してください。ただし、出力は以下の条件を満
// たす必要があります。

using namespace std;
int cnt = 0;
vector<int> G;
int c = 0;
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) {
  int h = 1;
  do {
    G.push_back(h);
    h = 3 * h + 1;
    c++;
  } while (h < n);
  for (int i = c - 1; i >= 0; i--)
    insertionSort(A, n, G[i]);
}
int main() {
  int n;
  cin >> n;
  int A[n];
  for (int i = 0; i < n; i++)
    cin >> A[i];
  shellSort(A, n);
  cout << c << '\\n';

  for (int i = c - 1; i >= 0; i--) {
    cout << G[i];
    if (i != n - 1)
      cout << ' ';
  }
  cout << '\\n';
  cout << cnt << '\\n';
  for (int i = 0; i < n; i++)
    cout << A[i] << endl;
}


Error: WA

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <vector>
// shellSort(A, n) は、一定の間隔 g
// だけ離れた要素のみを対象とした挿入ソートである insertionSort(A, n, g)
// を、最初は大きい値から g
// を狭めながら繰り返します。これをシェルソートと言います。 上の疑似コードの ?
// を埋めてこのプログラムを完成させてください。n と数列 A
// が与えられるので、疑似コード中の m、m 個の整数 Gi(i=0,1,...,m－1)、入力
// Aを昇順にした列を出力するプログラムを作成してください。ただし、出力は以下の条件を満
// たす必要があります。

using namespace std;
int cnt = 0;
vector<int> 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) {
  int p = (n > 1) ? n / 4 : n;
  while (p > 0) {
    G.push_back(p);
    p /= 4;
  }
  int m = n / 3;
  for (int i = 0; i < m; i++)
    insertionSort(A, n, G[i]);
}
int main() {
  int n;
  cin >> n;
  int A[n];
  for (int i = 0; i < n; i++)
    cin >> A[i];
  shellSort(A, n);
  cout << G.size() << '\\n';
  for (int i = 0; i < G.size(); i++) {
    cout << G[i];
    if (i != n - 1)
      cout << ' ';
  }
  cout << '\\n';
  cout << cnt << '\\n';
  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 <cstdio>
#include <iostream>
#include <vector>
using namespace std;

long long cnt;
int l;
int A[1000000];
int n;
vector<int> 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) {
  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--) {
    insertionSort(A, n, G[i]);
  }
}

int main() {
  cin >> n;
  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 > 0)
      printf(" ");
  }
  printf("\\n");
  printf("%d\\n", cnt);
  for (int i = 0; i < n; i++)
    printf("%d\\n", A[i]);

  return 0;
}

Error: TLE

Fixed submission:

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

#define MAX 10000

int cnt;

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 = j - g;
      cnt++;
    }
    A[j + g] = v;
  }
}

void shellSort(int A[], int n) {
  int m = 0;
  for (int i = n; i > 2; m++) {
    i = n / 3 + 1;
  }
  m++;

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

  cout << m << endl;
  for (int i = 0; i < m - 1; i++) {
    cout << G[i] << " " << flush;
  }
  cout << G[m - 1] << endl;

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

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

int main() {
  int n;
  int A[MAX];

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

  shellSort(A, n);

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

#include <cstdio>
#include <iostream>

using namespace std;

void insertionSort(int *, int, int);
void shellSort(int *, int);

int cnt = 0;
int m = 13;

void print_array(int *x, int n, bool f) {
  if (f) {
    for (int k = n - 1; k >= 0; k--) {
      if (k == 0) {
        printf("%d\\n", x[k]);
      } else {
        printf("%d ", x[k]);
      }
    }
  } else {
    for (int i = 0; i < n; ++i) {
      printf("%d\\n", x[i]);
    }
  }
}

int main(int argc, char const *argv[]) {
  int inputSize = 0;
  cin >> inputSize;
  int input[inputSize];

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

  shellSort(input, inputSize);
  printf("%d\\n", cnt);
  print_array(input, inputSize, false);

  return 0;
}

void insertionSort(int *array, int arraySize, int g) {
  for (int i = g; i < arraySize; ++i) {
    int key = array[i];
    int j = i - g;
    while (j >= 0 && array[j] > key) {
      array[j + g] = array[j];
      j -= g;
      cnt++;
    }
    array[j + g] = key;
  }
}

/* g 離れた数に対して挿入ソートを行う */
void shellSort(int *array, int arraySize) {
  int G[13] = {1,    4,    13,    40,    121,    364,   1093,
               3280, 9841, 29524, 88573, 265720, 797161};

  while (G[m - 1] > arraySize)
    m--;

  printf("%d\\n", m);
  print_array(G, m, true);

  for (int i = m - 1; i >= 0; i--) {
    insertionSort(array, arraySize, G[i]);
  }
}

Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

void insertionSort(int *, int, int);
void shellSort(int *, int);

int cnt = 0;
int m = 13;

void print_array(int *x, int n, bool f) {
  if (f) {
    for (int k = n - 1; k >= 0; ++k) {
      if (k == 0) {
        printf("%d\\n", x[k]);
      } else {
        printf("%d ", x[k]);
      }
    }
  } else {
    for (int i = 0; i < n; ++i) {
      printf("%d\\n", x[i]);
    }
  }
}

int main(int argc, char const *argv[]) {
  int inputSize = 0;
  cin >> inputSize;
  int input[inputSize];

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

  shellSort(input, inputSize);
  printf("%d\\n", cnt);
  print_array(input, inputSize, false);

  return 0;
}

void insertionSort(int *array, int arraySize, int g) {
  for (int i = g; i < arraySize; ++i) {
    int key = array[i];
    int j = i - g;
    while (j >= 0 && array[j] > key) {
      array[j + g] = array[j];
      j -= g;
      cnt++;
    }
    array[j + g] = key;
  }
}

/* g 離れた数に対して挿入ソートを行う */
void shellSort(int *array, int arraySize) {
  int G[13] = {1,    4,    13,    40,    121,    364,   1093,
               3280, 9841, 29524, 88573, 265720, 797161};

  while (G[m - 1] > arraySize)
    m--;

  printf("%d\\n", m);
  print_array(G, m, true);

  for (int i = 0; i < m; ++i) {
    insertionSort(array, arraySize, G[i]);
  }
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

/**
 * shell sort
 */

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

using namespace std;

void insertionSort(int a[], int n, int g, int &cnt) {
  for (int i = g; i < n; ++i) {
    // show
    // for(int i = 0; i<n ; ++i){ cout << a[i] <<" "; }
    //  cout << endl;
    //  cout << "cnt: " << cnt << endl;
    //  cout << "g:   " << g << endl;
    //  cout << "---" <<endl;

    int v = a[i];
    int j = i - g;

    while (j >= 0 && a[j] > v) {
      a[j + g] = a[j];
      j = j - g;
      ++cnt;
    }
    a[j + g] = v;
  }
}

int main() {
  const int N_max = 1000000;
  int N;
  int cnt = 0;
  int a[N_max];
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
  }

  int k = 1;
  int pow_2_k = 2;
  vector<int> G;
  while (pow_2_k - 1 <= N) {
    G.push_back(pow_2_k - 1);
    ++k;
    pow_2_k *= 2;
  }

  reverse(G.begin(), G.end());
  int m = G.size();

  for (int i = 0; i < m; ++i) {
    insertionSort(a, N, G[i], cnt);
  }

  // output
  // L1
  cout << m << endl;
  // L2
  for (int i = 0; i < m; ++i) {
    cout << (i == 0 ? "" : " ") << G[i];
  }
  cout << endl;
  // L3
  cout << cnt << endl;
  // L4 -
  for (int i = 0; i < N; ++i) {
    cout << a[i] << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

/**
 * shell sort
 */

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

using namespace std;

void insertionSort(int a[], int n, int g, int &cnt) {
  for (int i = g; i < n; ++i) {
    // show
    //  for(int i = 0; i<n ; ++i){ cout << a[i] <<" "; }
    //  cout << endl;
    //  cout << "cnt: " << cnt << endl;
    //  cout << "g:   " << g << endl;
    //  cout << "---" <<endl;

    int v = a[i];
    int j = i - g;

    while (j >= 0 && a[j] > v) {
      a[j + g] = a[j];
      j = j - g;
      ++cnt;
    }
    a[j + g] = v;
  }
}

int main() {
  const int N_max = 100;
  int N;
  int cnt = 0;
  int a[N_max];
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
  }

  int k = 1;
  int pow_2_k = 2;
  vector<int> G;
  while (pow_2_k - 1 <= N) {
    G.push_back(pow_2_k - 1);
    ++k;
    pow_2_k *= 2;
  }

  reverse(G.begin(), G.end());
  int m = G.size();

  for (int i = 0; i < m; ++i) {
    insertionSort(a, N, G[i], cnt);
  }

  // output
  // L1
  cout << m << endl;
  // L2
  for (int i = 0; i < m; ++i) {
    cout << (i == 0 ? "" : " ") << G[i];
  }
  cout << endl;
  // L3
  cout << cnt << endl;
  // L4 -
  for (int i = 0; i < N; ++i) {
    cout << a[i] << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;

int n, A[1000010], cnt = 0;

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

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  int m, G[1000000], i;
  G[0] = 1;
  i = 0;
  if (n == 1)
    m = 1;
  else {
    while (G[i] < n) {
      G[i + 1] = 3 * G[i] + 1;
      i++;
    }
    m = i;
  }
  cout << m << endl;
  for (int i = m - 1; i >= 0; i--) {
    insertionSort(A, n, G[i]);
    cout << G[i];
    if (i)
      cout << " ";
  }
  cout << endl;
  cout << cnt << endl;
  for (int i = 0; i < n; i++)
    cout << A[i] << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;

int n, A[1000010], cnt = 0;

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

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  int m, G[1000000], i;
  G[0] = 1;
  i = 1;
  while (G[i] < n) {
    G[i] = 3 * G[i - 1] + 1;
    i++;
  }
  m = i;
  cout << m << endl;
  for (int i = m - 1; i >= 0; i--) {
    insertionSort(A, n, G[i]);
    cout << G[i];
    if (i)
      cout << " ";
  }
  cout << cnt << endl;
  for (int i = 0; i < n; i++)
    cout << A[i] << endl;
  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <vector>
using namespace std;
#define NMAX 1000000

void printArr(int A[], int N) {
  for (int i = 0; i < N; ++i) {
    printf("%d\\n", A[i]);
  }
}

void printG(vector<int> v) {
  int N = v.size();
  for (int i = N - 1; i >= 1; --i) {
    printf("%d ", v[i]);
  }
  printf("%d\\n", v[0]);
}

long long insertionSort(int A[], int g, int N) {
  long long cnt = 0;

  for (int i = g; i < N; ++i) {
    int tmp = A[i];
    int j = i - g;
    while (j >= 0 && A[j] > tmp) {
      A[j + g] = A[j];
      j -= g;
      ++cnt;
    }
    A[j + g] = tmp;
  }

  return cnt;
}

void shellSort(int A[], int N) {
  int m;
  long long cnt = 0;
  vector<int> G;

  for (int g = 1; g <= N; g = 3 * g + 1) {
    G.push_back(g);
  }
  m = G.size();

  printf("%d\\n", m);
  printG(G);

  for (int i = m - 1; i >= 0; --i) {
    cnt += insertionSort(A, G[i], N);
  }

  printf("%lld\\n", cnt);
  printArr(A, N);
}

int main() {
  int N;
  static int A[NMAX];

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

  shellSort(A, N);

  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
#include <vector>
using namespace std;
#define NMAX 1000000

void printArr(int A[], int N) {
  for (int i = 0; i < N; ++i) {
    printf("%d\\n", A[i]);
  }
}

void printG(vector<int> v) {
  int N = v.size();
  for (int i = N - 1; i >= 1; --i) {
    printf("%d ", v[i]);
  }
  printf("%d\\n", v[0]);
}

long long insertionSort(int A[], int g, int N) {
  long long cnt = 0;

  for (int i = g; i < N; ++i) {
    int tmp = A[i];
    int j = i - g;
    while (j >= 0 && A[j] > tmp) {
      A[j + g] = A[j];
      j -= g;
      ++cnt;
    }
    A[j + g] = tmp;
  }

  return cnt;
}

void shellSort(int A[], int N) {
  int m;
  long long cnt = 0;
  vector<int> G;

  for (int g = 1; g < N; g = 3 * g + 1) {
    G.push_back(g);
  }
  m = G.size();

  printf("%d\\n", m);
  printG(G);

  for (int i = m - 1; i >= 0; --i) {
    cnt += insertionSort(A, G[i], N);
  }

  printf("%lld\\n", cnt);
  printArr(A, N);
}

int main() {
  int N;
  static int A[NMAX];

  scanf("%d", &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 <iostream>
#include <vector>

using namespace std;

long long g_count = 0;
const int MAX_N = 1000000;
vector<int> 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 = j - g;
      g_count++;
    }
    a[j + g] = v;
  }
}

int shellSort(int *a, int n) {

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

  for (int i = g.size() - 1; i >= 0; i--) {
    insertionSort(a, n, g[i]);
  }
  return g_count;
}

int main() {
  int n;
  int a[MAX_N];
  cin >> n;

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

  int count = shellSort(a, n);

  cout << g.size() << endl;
  for (int i = g.size() - 1; i >= 0; i--) {
    cout << g[i] << " ";
  }
  cout << endl;
  cout << count << endl;
  for (int i = 0; i < n; i++) {
    cout << a[i] << endl;
  }
}

Error: WA

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int g_count = 0;
const int MAX_N = 10000;
vector<int> 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 = j - g;
      g_count++;
    }
    a[j + g] = v;
  }
}

int shellSort(int *a, int n) {

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

  for (int i = g.size() - 1; i >= 0; i--) {
    insertionSort(a, n, g[i]);
  }
  return g_count;
}

int main() {
  int n;
  int a[MAX_N];
  cin >> n;

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

  int count = shellSort(a, n);

  cout << g.size() << endl;
  for (int i = g.size() - 1; i >= 0; i--) {
    cout << g[i] << " ";
  }
  cout << endl;
  cout << count << endl;
  for (int i = 0; i < n; i++) {
    cout << a[i] << endl;
  }
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int N, A[1000000], cnt;

void Out(const vector<int> G) {
  cout << G.size() << endl;
  cout << G.back();
  for (int i = G.size() - 2; i >= 0; i--) {
    cout << ' ' << G[i];
  }
  cout << endl;
  cout << cnt << endl;
  for (int i = 0; i < N; i++)
    cout << A[i] << endl;
}

void InsertionSort(int g) {
  for (int i = g; i < N; i++) {
    int v = A[i], j = i - g;
    while (j >= 0 && A[j] > v) {
      A[j + g] = A[j];
      j -= g;
      cnt++;
    }
    A[j + g] = v;
  }
}
void ShellSort(const vector<int> G) {
  cnt = 0;
  for (int i = G.size() - 1; i >= 0; i--) {
    InsertionSort(G[i]);
  }
}

int main() {
  vector<int> G;
  cin >> N;
  int calc = 1;
  while (calc <= N) {
    G.push_back(calc);
    calc = calc * 3 + 1;
  }
  for (int i = 0; i < N; i++)
    cin >> A[i];
  ShellSort(G);
  Out(G);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int N, A[1000000], cnt;

void Out(const vector<int> G) {
  cout << G.size() << endl;
  cout << G.back();
  for (int i = G.size() - 2; i >= 0; i--) {
    cout << ' ' << G[i];
  }
  cout << endl;
  cout << cnt << endl;
  for (int i = 0; i < N; i++)
    cout << A[i] << endl;
}

void InsertionSort(int g) {
  for (int i = g; i < N; i++) {
    int v = A[i], j = i - g;
    while (j >= 0 && A[j] > v) {
      A[j + g] = A[j];
      j -= g;
      cnt++;
    }
    A[j + g] = v;
  }
}
void ShellSort(const vector<int> G) {
  cnt = 0;
  for (int i = G.size() - 1; i >= 0; i--) {
    InsertionSort(G[i]);
  }
}

int main() {
  cin >> N;
  int calc = 1;
  vector<int> G;
  while (calc < N) {
    G.push_back(calc);
    calc = calc * 3 + 1;
  }
  for (int i = 0; i < N; i++)
    cin >> A[i];
  ShellSort(G);
  Out(G);
  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli>> mat;
typedef vector<vector<bool>> matb;
typedef vector<string> vst;
typedef pair<lli, lli> pll;
typedef pair<double, double> pdd;

lli n;
vll a;
vll g;
lli ans = 0;

int main() {
  cin >> n;
  a = vll(n);
  for (lli i = 0; i < n; i++)
    cin >> a[i];
  for (lli i = 1; i <= n; i = 3 * i + 1) {
    g.push_back(i);
  }
  for (lli k = g.size() - 1; k >= 0; k--) {
    for (lli i = 0; i < n; i++) {
      for (lli j = i - g[k]; j >= 0 && a[j] > a[j + g[k]]; j -= g[k]) {
        swap(a[j], a[j + g[k]]);
        ans++;
      }
    }
  }
  cout << g.size() << endl;
  cout << g[g.size() - 1];
  for (lli i = g.size() - 2; i >= 0; i--) {
    cout << " " << g[i];
  }
  cout << endl;
  cout << ans << endl;
  for (lli i = 0; i < n; i++)
    cout << a[i] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli>> mat;
typedef vector<vector<bool>> matb;
typedef vector<string> vst;
typedef pair<lli, lli> pll;
typedef pair<double, double> pdd;

lli n;
vll a;
vll g;
lli ans = 0;

int main() {
  cin >> n;
  a = vll(n);
  for (lli i = 0; i < n; i++)
    cin >> a[i];
  for (lli i = 1; i < n; i = 3 * i + 1) {
    g.push_back(i);
  }
  for (lli k = g.size() - 1; k >= 0; k--) {
    for (lli i = 0; i < n; i++) {
      for (lli j = i - g[k]; j >= 0 && a[j] > a[j + g[k]]; j -= g[k]) {
        swap(a[j], a[j + g[k]]);
        ans++;
      }
    }
  }
  cout << g.size() << endl;
  cout << g[g.size() - 1];
  for (lli i = g.size() - 2; i >= 0; i--) {
    cout << " " << g[i];
  }
  cout << endl;
  cout << ans << endl;
  for (lli 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 <iostream>
#include <vector>
using namespace std;

long long cnt;
int l;
int A[100000000];
int n;

vector<int> G;

void insertion(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 shell(int A[], int n) {
  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--) {
    insertion(A, n, G[i]);
  }
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);
  cnt = 0;
  shell(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: 0

Fixed submission:

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

static long long cnt;
static int l;
static int A[100000];
static int n;

vector<int> G;

void insertion(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 shell(int A[], int n) {
  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--) {
    insertion(A, n, G[i]);
  }
}

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