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

#include <array>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

static void printArray(int A[], int N) {
  for (int i = 0; i < N; i++) {
    cout << A[i] << endl;
  }
}

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

static int insersionSort(int A[], int N, int g) {
  int v, j, count = 0;
  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;
      count++;
    }
    A[j + g] = v;
  }
  return count;
}

#include <cstdio>

static int shellSort(array<int, 1000000> &A, int N) {
  array<int, 1000000> sortArray(A);
  int g = 1, startedIndex = 0, count = 0;
  vector<int> G = {1};
  for (; g < N;) {
    G.push_back(g);
    g = 3 * g;
  }
  for (g = G.size() - 1; g >= 0; g--) {
    if (G[g] * 2 > N) {
      continue;
    }
    if (startedIndex == 0) {
      startedIndex = g;
    }
    count += insersionSort(sortArray.data(), N, G[g]);
    /*
        if (count > pow(N, 1.5f)) {
          sortArray = A;
          continue;
        }
    */
  }

  cout << startedIndex + 1 << endl;
  for (int i = startedIndex; i >= 0; i--) {
    cout << G[i];
    cout << ' ';
  }
  cout << endl;
  cout << count << endl;
  A = sortArray;
  return count;
}

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

  array<int, 1000000> A;
  inputArray(A.data(), N);

  //  printArray(A.data(), N);

  shellSort(A, N);

  printArray(A.data(), N);

  return 0;
}


Error: WA

Fixed submission:

#include <array>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

static void printArray(int A[], int N) {
  for (int i = 0; i < N; i++) {
    cout << A[i] << endl;
  }
}

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

static int insersionSort(int A[], int N, int g) {
  int v, j, count = 0;
  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;
      count++;
    }
    A[j + g] = v;
  }
  return count;
}

#include <cstdio>

static int shellSort(array<int, 1000000> &A, int N) {
  array<int, 1000000> sortArray(A);
  int g, startedIndex = 0, count = 0;
  vector<int> G;
  for (int g = 1; g < N;) {
    G.push_back(g);
    g = 3 * g;
  }
  for (g = G.size() - 1; g >= 0; g--) {
    if (G[g] * 2 > N) {
      continue;
    }
    if (startedIndex == 0) {
      startedIndex = g;
    }
    count += insersionSort(sortArray.data(), N, G[g]);
    if (count > pow(N, 1.5f)) {
      sortArray = A;
      continue;
    }
  }

  cout << startedIndex + 1 << endl;
  for (int i = startedIndex; i >= 0; i--) {
    cout << G[i];
    cout << ' ';
  }
  cout << endl;
  cout << count << endl;
  A = sortArray;
  return count;
}

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

  array<int, 1000000> A;
  inputArray(A.data(), N);

  //  printArray(A.data(), N);

  shellSort(A, N);

  printArray(A.data(), N);

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

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

template <typename T> void insertion_sort(vector<T> &A, int g);

template <typename T> void shell_sort(vector<T> &A);

int cnt = 0;

int main() {
  int n = 0;
  cin >> n;
  vector<int> A(n);
  for (auto &ai : A)
    cin >> ai;
  shell_sort(A);
  return 0;
}

template <typename T> void insertion_sort(vector<T> &A, int g) {
  for (int i = g; i != A.size(); ++i) {
    auto v = A[i];
    auto j = i - g;
    while (j >= 0 and A[j] > v) {
      A[j + g] = A[j];
      j -= g;
      ++cnt;
    }
    A[j + g] = v;
  }
}

template <typename T> void shell_sort(vector<T> &A) {
  cnt = 0;
  vector<int> G;
  for (int gi = 1; gi <= A.size(); gi = 3 * gi + 1)
    G.push_back(gi);
  reverse(G.begin(), G.end());
  for (auto gi : G)
    insertion_sort(A, gi);

  cout << G.size() << endl;
  cout << G[0];
  for (auto it = G.begin() + 1; it != G.end(); ++it)
    cout << ' ' << *it;
  cout << endl;
  cout << cnt << endl;
  for (auto ai : A)
    cout << ai << endl;
}

Error: 0

Fixed submission:

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

template <typename T> void insertion_sort(vector<T> &A, int g);

template <typename T> void shell_sort(vector<T> &A);

int cnt = 0;

int main() {
  int n = 0;
  cin >> n;
  vector<int> A(n);
  for (auto &ai : A)
    cin >> ai;
  shell_sort(A);
  return 0;
}

template <typename T> void insertion_sort(vector<T> &A, int g) {
  for (int i = g; i != A.size(); ++i) {
    auto v = A[i];
    auto j = i - g;
    while (j >= 0 and A[j] > v) {
      A[j + g] = A[j];
      j -= g;
      ++cnt;
    }
    A[j + g] = v;
  }
}

template <typename T> void shell_sort(vector<T> &A) {
  cnt = 0;
  vector<int> G;
  for (int gi = 1; gi < A.size(); gi = 3 * gi + 1)
    G.push_back(gi);
  reverse(G.begin(), G.end());
  for (auto gi : G)
    insertion_sort(A, gi);

  cout << G.size() << endl;
  cout << G[0];
  for (auto it = G.begin() + 1; it != G.end(); ++it)
    cout << ' ' << *it;
  cout << endl;
  cout << cnt << endl;
  for (auto ai : A)
    cout << ai << endl;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
long long cnt;
int G[100];
int m;

void insertionSort(int A[], int n, int g) {
  int v, i, j;

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

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

void shellSort(int A[], int n) {
  int i;
  int g = 0;
  m = -1;

  while (m < 100 && g <= n) {
    g = 3 * g + 1;
    m++;
  }
  g = 0;
  for (i = m - 1; i >= 0; i--) {
    g = 3 * g + 1;
    G[i] = g;
  }

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

int main() {
  int A[1000000], n;
  cnt = 0;

  scanf("%d", &n);

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

  shellSort(A, n);

  print(m, G, A, n);

  return 0;
}


Error: 0

Fixed submission:

#include <stdio.h>
long long cnt;
int G[100];
int m;

void insertionSort(int A[], int n, int g) {
  int v, i, j;

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

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

void shellSort(int A[], int n) {
  int i;
  int g = 0;
  m = -1;

  while (m < 100 && g <= n) {
    g = 3 * g + 1;
    m++;
  }
  g = 0;
  for (i = m - 1; i >= 0; i--) {
    g = 3 * g + 1;
    G[i] = g;
  }

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

int main() {
  int A[100000], n;
  cnt = 0;

  scanf("%d", &n);

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

  shellSort(A, n);

  print(m, G, A, n);

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

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

typedef long long ll;
typedef unsigned long long ull;

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

  int m = 0;
  int g = 0;
  int cnt = 0;
  vector<int> G;
  while (3 * g + 1 <= n)
    g = 3 * g + 1;
  for (; g > 0; g /= 3) {
    G.push_back(g);
    m++;
  }
  for (int i = 0; i < m; i++) {
    for (int j = G[i]; j < n; j++) {
      int v = A[j];
      int k = j - G[i];
      while (k >= 0 && A[k] > v) {
        A[k + G[i]] = A[k];
        k -= G[i];
        cnt++;
      }
      A[k + G[i]] = v;
    }
  }

  cout << m << endl;
  cout << G[0];
  for (int i = 1; i < m; i++)
    cout << ' ' << G[i];
  cout << endl;
  cout << cnt << endl;
  for (int i = 0; i < n; i++)
    cout << A[i] << endl;

  return 0;
}


Error: 0

Fixed submission:

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

typedef long long ll;
typedef unsigned long long ull;

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

  int m = 0;
  int g = 0;
  int cnt = 0;
  vector<int> G;
  while (3 * g + 1 < n)
    g = 3 * g + 1;
  for (; g > 0; g /= 3) {
    G.push_back(g);
    m++;
  }
  for (int i = 0; i < m; i++) {
    for (int j = G[i]; j < n; j++) {
      int v = A[j];
      int k = j - G[i];
      while (k >= 0 && A[k] > v) {
        A[k + G[i]] = A[k];
        k -= G[i];
        cnt++;
      }
      A[k + G[i]] = v;
    }
  }

  cout << m << endl;
  cout << G[0];
  for (int i = 1; i < m; i++)
    cout << ' ' << G[i];
  cout << endl;
  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 <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
#define rep2(x, from, to) for (int x = (from); (x) < (to); (x)++)
#define rep(x, to) rep2(x, 0, to)
int n, a[1000010], cnt, m[100];
vector<int> v;
void insertionSort(int *a, int n, int g) {
  rep2(i, g, n) {
    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) {
  cnt = 0;
  int h = 1;
  while (h <= n) {
    h = 3 * h + 1;
  }
  while (h) {
    h = (h - 1) / 3;
    if (h)
      v.push_back(h);
  }
  rep(i, v.size()) { insertionSort(a, n, v[i]); }
}
int main() {
  cin >> n;
  rep(i, n) { cin >> a[i]; }
  shellSort(a, n);
  cout << v.size() << endl;
  rep(i, v.size() - 1) { cout << v[i] << " "; }
  cout << v[v.size() - 1] << endl;
  cout << cnt << endl;
  rep(i, n) { cout << a[i] << endl; }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
#define rep2(x, from, to) for (int x = (from); (x) < (to); (x)++)
#define rep(x, to) rep2(x, 0, to)
int n, a[1000010], cnt;
vector<int> v;
void insertionSort(int *a, int n, int g) {
  rep2(i, g, n) {
    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) {
  cnt = 0;
  int x = n / 2;
  while (x) {
    v.push_back(x);
    x /= 2;
  }
  rep(i, v.size()) { insertionSort(a, n, v[i]); }
}
int main() {
  cin >> n;
  rep(i, n) { cin >> a[i]; }
  shellSort(a, n);
  cout << v.size() << endl;
  rep(i, v.size() - 1) { cout << v[i] << " "; }
  cout << v[v.size() - 1] << endl;
  cout << cnt << endl;
  rep(i, n) { cout << a[i] << endl; }
  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

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

int N;
vector<int> A;
vector<int> G;

// 昇順ソート
void insertionSort(vector<int> &A, int n, int g, int &cnt) {
  for (int i = g; i < n; i++) {
    int v = A[i];  // iの示す値を取り出す
    int j = i - g; // とっかえ対象はgだけ離れる

    while (j >= 0 && A[j] > v) { // jの示す値A[j]がiの示す値よりも大きい
      A[j + g] = A[j];           // 押出し
      j = j - g;                 // またg間隔移動する
      cnt++;
    }

    A[j + g] = v; // 取り出していた値を現在のi位置に戻す
  }
}

int shellSort(vector<int> &A, int n) {
  int cnt = 0;

  // シェルソート間隔数列Gの生成
  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], cnt);
  }

  return cnt;
}

int main() {
  int cnt;
  cin >> N;
  A.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  cnt = shellSort(A, N);

  cout << G.size() << endl;
  for (int i = G.size() - 1; i >= 0; i--) {
    cout << G[i];
    if (i != 0)
      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 <vector>
using namespace std;

int N;
vector<int> A;
vector<int> G;

// 昇順ソート
void insertionSort(vector<int> &A, int n, int g, int &cnt) {
  for (int i = g; i < n; i++) {
    int v = A[i];  // iの示す値を取り出す
    int j = i - g; // とっかえ対象はgだけ離れる

    while (j >= 0 && A[j] > v) { // jの示す値A[j]がiの示す値よりも大きい
      A[j + g] = A[j];           // 押出し
      j = j - g;                 // またg間隔移動する
      cnt++;
    }

    A[j + g] = v; // 取り出していた値を現在のi位置に戻す
  }
}

int shellSort(vector<int> &A, int n) {
  int cnt = 0;

  // シェルソート間隔数列Gの生成
  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], cnt);
  }

  return cnt;
}

int main() {
  int cnt;
  cin >> N;
  A.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  cnt = shellSort(A, N);

  cout << G.size() << endl;
  for (size_t i = G.size() - 1; i >= 0; i--) {
    if (i != 0)
      cout << " ";
    cout << G[i];
  }
  cout << endl;
  cout << cnt << endl;

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

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

#include <iostream>
using namespace std;

int InsertionSort(int *a, int n, int g) {
  int tmp, j, count = 0;
  for (int i = g; i < n; i++) {
    tmp = a[i];
    for (j = i - g; j >= 0 && a[j] > tmp; j -= g) {
      a[j + g] = a[j];
      count++;
    }
    a[j + g] = tmp;
  }
  return count;
}

int ShellSort(int *a, int n) {
  int m, g[100], count = 0;
  for (m = 1; m * m * m <= n; m++)
    continue;
  m--;
  cout << m << endl;
  for (int i = 0; i < m; i++) {
    g[i] = (m - i) * (m - i);
    if (i)
      cout << " ";
    cout << g[i];
  }
  for (int i = 0; i < m; i++)
    count += InsertionSort(a, n, g[i]);

  return count;
}

int main() {
  int n, a[1000000], count;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  count = ShellSort(a, n);
  cout << endl << count << endl;
  for (int i = 0; i < n; i++)
    cout << a[i] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int InsertionSort(int *a, int n, int g) {
  int tmp, j, count = 0;
  for (int i = g; i < n; i++) {
    tmp = a[i];
    for (j = i - g; j >= 0 && a[j] > tmp; j -= g) {
      a[j + g] = a[j];
      count++;
    }
    a[j + g] = tmp;
  }
  return count;
}

int ShellSort(int *a, int n) {
  int m, g[1000], count = 0, tmp;
  tmp = n;
  for (m = 0; tmp > 0; m++)
    tmp /= 10;
  cout << m << endl;
  g[m - 1] = 1;
  for (int i = m - 1; i > 0; i++)
    g[i - 1] = g[i] * 2;
  for (int i = 0; i < m; i++) {
    if (i)
      cout << " ";
    cout << g[i];
  }
  for (int i = 0; i < m; i++)
    count += InsertionSort(a, n, g[i]);

  return count;
}

int main() {
  int n, a[1000000], count;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  count = ShellSort(a, n);
  cout << endl << count << endl;
  for (int i = 0; i < n; i++)
    cout << a[i] << endl;

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

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

int calc(int);
long long cnt;
int X;
void insertionSort(int A[], int n, int g) {
  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 = j - g;
      cnt++;
    }
    A[j + g] = v;
  }
}

int Gt[1000000], gt = 0;
int G[1000000], g = 0;

void shellSort(int A[], int n) {

  int Z = pow(n, 1.5), flg, y;
  X = n;
  cnt = 0;

  for (int i = n; i >= 1; i--) {
    if (n % i == 0) {
      Gt[gt++] = i;
      // cout<<i<<" - "<<endl;
    } else {
      if (i * i <= n && n <= (i + 1) * (i + 1))
        Gt[gt++] = y = i;
    }
  }

  for (int i = 0; i < gt; i++) {
    if (i % 2 == 1 || i == y) {

      G[g++] = Gt[i];
    }
  }
  G[g++] = 1;

  int ng;
  while (1) {
    if (g < 100)
      break;
    ng = 0;
    for (int i = 0; i < g; i++) {
      if (i == y || i % 2 == 1) {
        G[ng++] = G[i];
      }
    }
    g = ng;
  }
  for (int i = 0; i < g; i++) {
    insertionSort(A, n, G[i]);
  }

  cout << g << endl;
  for (int i = 0; i < g; i++) {
    if (i)
      cout << ' ';
    cout << G[i];
  }
  cout << endl;
  cout << cnt << endl;

  assert(g < 100);
  assert(cnt < Z);
  // cout<<cnt<<" cnt"<<endl;
  // cout<<Z<<" Z"<<endl;
  // cout<<(int)pow(n,1.5)<<endl;
}

int main() {

  int n;
  int A[1000000];

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

  shellSort(A, n);

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

  return 0;
}

int calc(int n) {
  for (int i = sqrt(n); i >= 0; i--) {
    if (n % i == 0)
      return n / i;
  }
}


Error: 0

Fixed submission:

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

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

void insertionSort(int A[], int n, 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(int A[], int n) {
  for (int h = 1; h <= n; h = 3 * h + 1) {
    G.push_back(h);
  }

  for (int i = G.size() - 1; i >= 0; i--) {
    insertionSort(A, n, G[i]);
  }
}

int main() {
  int A[1000000], n;

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

  shellSort(A, n);

  cout << G.size() << endl;
  for (int i = G.size() - 1; i >= 0; i--) {
    cout << G[i];
    if (i)
      cout << ' ';
  }

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

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

#include <iostream>
#include <vector>

using namespace std;

long long cnt;
int i, A[1000000], 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; h <= n; h = 3 * h + 1)
    G.push_back(h);
  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++)
    cin >> A[i];
  cnt = 0;

  shellSort(A, n);

  cout << G.size() << endl;
  for (int i = G.size() - 1; i > 0; i--)
    cout << G[i] << ' ';
  cout << G[0] << endl;
  cout << cnt << endl;
  for (int i = 0; i < n; i++)
    cout << A[i] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

long long cnt;
int i, A[10000], 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; h <= n; h = 3 * h + 1)
    G.push_back(h);
  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++)
    cin >> A[i];
  cnt = 0;

  shellSort(A, n);

  cout << G.size() << endl;
  for (int i = G.size() - 1; i > 0; i--)
    cout << G[i] << ' ';
  cout << G[0] << endl;
  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 <iostream>

using namespace std;

template <typename T> void cout_seq(T seq[], int n) {
  for (int i = 0; i < n; i++) {
    if (i) {
      cout << " ";
    }
    cout << seq[i];
  }
  cout << endl;
}

int insertion_sort(int A[], int n, int g) {
  int cnt = 0;
  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;
  }
  return cnt;
}

int shell_sort(int A[], int n) {
  int cnt = 0;
  int G[n];
  int v = (n > 1) ? n - 1 : 1;
  int m = 0;
  for (int i = 0;; i++) {
    G[i] = v;
    if (v == 1) {
      m = i + 1;
      break;
    }
    v = v / 2;
  }
  cout << m << endl;
  cout_seq(G, m);
  for (int i = 0; i < m; i++) {
    cnt += insertion_sort(A, n, G[i]);
  }
  return cnt;
}

int main() {
  int n;
  cin >> n;
  int seq[n];
  for (int i = 0; i < n; i++)
    cin >> seq[i];
  int count = 0;
  count = shell_sort(seq, n);
  cout << count << endl;
  for (auto a : seq) {
    cout << a << endl;
  }
}

Error: WA

Fixed submission:

#include <iostream>

using namespace std;

template <typename T> void cout_seq(T seq[], int n) {
  for (int i = 0; i < n; i++) {
    if (i) {
      cout << " ";
    }
    cout << seq[i];
  }
  cout << endl;
}

int insertion_sort(int A[], int n, int g) {
  int cnt = 0;
  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;
  }
  return cnt;
}

int shell_sort(int A[], int n) {
  int cnt = 0;
  int G[n];
  int v = n - 1;
  int m = 0;
  for (int i = 0;; i++) {
    G[i] = v;
    if (v == 1) {
      m = i + 1;
      break;
    }
    v = v / 2;
  }
  cout << m << endl;
  cout_seq(G, m);
  for (int i = 0; i < m; i++) {
    cnt += insertion_sort(A, n, G[i]);
  }
  return cnt;
}

int main() {
  int n;
  cin >> n;
  int seq[n];
  for (int i = 0; i < n; i++)
    cin >> seq[i];
  int count = 0;
  count = shell_sort(seq, n);
  cout << count << endl;
  for (auto a : seq) {
    cout << a << endl;
  }
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

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

long InsertionSort(vector<int> &A, int n, int g) {
  long cnt = 0;
  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;
  }
  return cnt;
}

void ShellSort(vector<int> &A, int n) {
  vector<int> G; // = {1, 4, 13, 40, 121...}
  // g_n+1 = 3g_n +
  // 1?????°????????¨????????¨O(N^1.25)??¨???????????¨????????¶????????£????????¬???????????????
  for (int i = 1; i <= n; i = 3 * i + 1) {
    G.push_back(i);
  }
  cout << G.size() << endl; // output m
  //?????????????????????????????\\?????????
  long cnt = 0;
  for (auto itr = G.crbegin(); itr != G.crend(); ++itr) {
    if (itr != G.crbegin())
      cout << " "; // output[13 4 1]
    cout << *itr;
    cnt += InsertionSort(A, n, *itr);
  }
  cout << endl;
  cout << cnt << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n; // input
  vector<int> A(n);
  for (auto &a : A)
    cin >> a; // input
  ShellSort(A, n);
  for (auto &a : A)
    cout << a << endl; // output
  // system("pause");
}

Error: 0
sh: 1: pause: not found


Fixed submission:

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

long InsertionSort(vector<int> &A, int n, int g) {
  long cnt = 0;
  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;
  }
  return cnt;
}

void ShellSort(vector<int> &A, int n) {
  vector<int> G; // = {1, 4, 13, 40, 121...}
  // g_n+1 = 3g_n +
  // 1?????°????????¨????????¨O(N^1.25)??¨???????????¨????????¶????????£????????¬???????????????
  for (int i = 1; i <= n; i = 3 * i + 1) {
    G.push_back(i);
  }
  cout << G.size() << endl; // output m
  //?????????????????????????????\\?????????
  long cnt = 0;
  for (auto itr = G.crbegin(); itr != G.crend(); ++itr) {
    if (itr != G.crbegin())
      cout << " "; // output[13 4 1]
    cout << *itr;
    cnt += InsertionSort(A, n, *itr);
  }
  cout << endl;
  cout << cnt << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n; // input
  vector<int> A(n);
  for (auto &a : A)
    cin >> a; // input
  ShellSort(A, n);
  for (auto &a : A)
    cout << a << endl; // output
  system("pause");
}