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

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

int InsertionSort(vector<int> &A, int g) {
  int N = A.size();
  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 main(void) {
  int cnt = 0;
  int N;
  cin >> N;
  vector<int> A(N, 0);
  for (int i = 0; i < N; ++i)
    cin >> A[i];

  vector<int> G;
  int g = 1;
  while (g <= N) {
    G.push_back(g);
    g = 3 * g + 1;
  }
  int m = G.size();
  cout << m << endl;
  for (int i = m - 1; i >= 0; --i) {
    cout << G[i];
    if (i != 0)
      cout << " ";
  }
  cout << endl;

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

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

  return 0;
}

Error: 0

Fixed submission:

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

int InsertionSort(vector<int> &A, int g) {
  int N = A.size();
  int cnt = 0;
  for (int i = g; i < N; ++i) {
    int idx = i;
    while (idx > 0 && (A[idx - g] > A[idx])) {
      int tmp = A[idx - g];
      A[idx - g] = A[idx];
      A[idx] = tmp;
      idx = idx - g;
      ++cnt;
    }
  }
  return cnt;
}

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

  return cnt;
}

int main(void) {
  int N;
  cin >> N;
  vector<int> A(N, 0);
  for (int i = 0; i < N; ++i)
    cin >> A[i];

  vector<int> G;
  int cnt = ShellSort(A, G);
  int m = G.size();

  cout << m << endl;
  for (int i = m - 1; i >= 0; --i) {
    cout << G[i];
    if (i != 0)
      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 = 0;
void insertionSort(int numbers[], int n, int g) {
  for (int i = g; i < n; i++) {
    int v = numbers[i];
    int j = i - g;
    while (j >= 0 && numbers[j] > v) {
      numbers[j + g] = numbers[j];
      j = j - g;
      cnt++;
    }
    numbers[j + g] = v;
  }
}
void shellSort(int numbers[], int n) {
  // int m=2;
  // 生成数列G int G[2]={4,1}
  vector<int> G;
  for (int i = 1; i <= n;) {
    G.push_back(i);
    i = i * 3 + 1;
  }
  for (int i = G.size() - 1; i >= 0; i--) {

    insertionSort(numbers, n, G[i]);
  }
  cout << G.size() << endl;
  for (int i = G.size() - 1; i > 0; i--) {
    cout << G[i] << " ";
  }
  cout << G[0];
  cout << endl;
}
int main() {
  int n;
  cin >> n;
  int numbers[1000000];

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

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


Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;
long long cnt = 0;
void insertionSort(int numbers[], int n, int g) {
  for (int i = g; i < n; i++) {
    int v = numbers[i];
    int j = i - g;
    while (j >= 0 && numbers[j] > v) {
      numbers[j + g] = numbers[j];
      j = j - g;
      cnt++;
    }
    numbers[j + g] = v;
  }
}
void shellSort(int numbers[], int n) {
  // int m=2;
  // 生成数列G int G[2]={4,1}
  vector<int> G;
  for (int i = 1; i <= n;) {
    G.push_back(i);
    i = i * 3 + 1;
  }
  for (int i = G.size() - 1; i >= 0; i--) {

    insertionSort(numbers, n, G[i]);
  }
  cout << G.size() << endl;
  for (int i = G.size() - 1; i > 0; i--) {
    cout << G[i] << " ";
  }
  cout << G[0];
  cout << endl;
}
int main() {
  int n;
  cin >> n;
  int numbers[100];

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

  shellSort(numbers, n);
  cout << cnt << endl;
  for (int i = 0; i < n; i++) {
    cout << numbers[i] << endl;
  }
}
Problem p02262 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int cnt;

#define MAX 1000000

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

void shellSort(int A[], int n) {
  cnt = 0;
  int G[MAX];
  int m = 0;
  int h;

  for (h = 1; h < n; h = 3 * h + 1)
    ;
  h = (h - 1) / 3;

  while (h > 1) {
    G[m] = h;
    m++;
    h = (h - 1) / 3;
  }
  G[m] = 1;
  m++;

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

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

int main() {

  std::ios_base::sync_with_stdio(false);

  int n;
  int A[MAX];

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

  shellSort(A, n);
  cout << cnt << endl;

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

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int cnt;

#define MAX 1000000

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

void shellSort(int A[], int n) {
  cnt = 0;
  int G[MAX];
  int m = 0;
  int h;

  for (h = 1; h < n; h = h * 4)
    ;
  h = h / 4;

  while (h > 1) {
    G[m] = h;
    m++;
    h = h / 4;
  }
  G[m] = 1;
  m++;

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

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

int main() {

  std::ios_base::sync_with_stdio(false);

  int n;
  int A[MAX];

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

  shellSort(A, n);
  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 <cmath>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;
long long cnt;
int A[1000005];
vector<int> G;
void insertionSort(int A[], int n, int g);
void shellSort(int A[], int n);

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

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

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


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;
long long cnt;
int A[1000005];
vector<int> G;
void insertionSort(int A[], int n, int g);
void shellSort(int A[], int n);

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

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

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

#include <algorithm>
#include <cstdio>
#include <vector>

static const size_t N_MAX = 1000000;
static int A[N_MAX];

int main() {
  size_t N;
  scanf("%zu", &N);
  for (size_t i = 0; i < N; i++)
    scanf("%d", A + i);
  std::vector<size_t> G;
  for (auto i = 1; unsigned((1 << i) - 1) <= N; i++)
    G.push_back((1 << i) - 1);
  std::reverse(G.begin(), G.end());
  int c = 0;
  for (auto &&g : G)
    for (size_t i = g; i < N; i++) {
      auto v = A[i];
      size_t j = i;
      while (j >= g && A[j - g] > v) {
        A[j] = A[j - g];
        j -= g;
        c++;
      }
      A[j] = v;
    }
  printf("%zu\\n", G.size());
  for (size_t i = 0; i < G.size() - 1; i++)
    printf("%zu ", G[i]);
  printf("%zu\\n", G.back());
  printf("%d\\n", c);
  for (size_t i = 0; i < N; i++)
    printf("%d\\n", A[i]);
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

static const size_t N_MAX = 36;

int main() {
  size_t N;
  int A[N_MAX];
  scanf("%zu", &N);
  for (size_t i = 0; i < N; i++)
    scanf("%d", A + i);
  std::vector<size_t> G;
  for (auto i = 1; unsigned((1 << i) - 1) <= N; i++)
    G.push_back((1 << i) - 1);
  std::reverse(G.begin(), G.end());
  int c = 0;
  for (auto &&g : G)
    for (size_t i = g; i < N; i++) {
      auto v = A[i];
      size_t j = i;
      while (j >= g && A[j - g] > v) {
        A[j] = A[j - g];
        j -= g;
        c++;
      }
      A[j] = v;
    }
  printf("%zu\\n", G.size());
  for (size_t i = 0; i < G.size() - 1; i++)
    printf("%zu ", G[i]);
  printf("%zu\\n", G.back());
  printf("%d\\n", c);
  for (size_t i = 0; i < N; i++)
    printf("%d\\n", A[i]);
  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#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 = 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)
      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 <stdio.h>
#include <string.h>
int a[10000];
long long c;
int k = 0;
int g[10000];
int h[10000];
void xier(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;
      c++;
    }
    a[j + g] = v;
  }
}
void cha(int a[], int n) {
  for (int i = 1, j = 0;; i = i * 3 + 1, j++) {
    if (i > n)
      break;
    h[j] = i;
    k = j;
  }
  for (int i = k, j = 0; i >= 0; i--, j++) {
    g[j] = h[i];
  }
  for (int i = 0; i <= k; i++) {
    xier(a, n, g[i]);
  }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  cha(a, n);
  printf("%d\\n", k + 1);
  for (int i = 0; i <= k; i++) {
    if (i > 0)
      printf(" ");
    printf("%d", g[i]);
  }
  printf("\\n%d\\n", c);
  for (int i = 0; i < n; i++) {
    printf("%d\\n", a[i]);
  }
  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
vector<int> ve;
const int maxn = 1000010;
long long cnt = 0;
int num[maxn];

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

void ShellSort(int n) {
  int h = 1;
  while (h <= n) {
    ve.push_back(h);
    h = h * 3 + 1;
  }

  for (int i = ve.size() - 1; i >= 0; i--)
    InsertionSort(n, ve[i]);
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &num[i]);
  ShellSort(n);
  printf("%d\\n", ve.size());
  for (int i = ve.size() - 1; i >= 0; i--) {
    printf("%d", ve[i]);
    if (i != 0)
      printf(" ");
  }
  printf("\\n");
  printf("%lld\\n", cnt);
  for (int i = 0; i < n; i++) {
    printf("%d", num[i]);
    if (i != n - 1)
      printf("\\n");
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000;
vector<int> ve;
int num[maxn];
int cnt = 0;

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

void ShellSort(int n) {
  int h = 1;
  while (h <= n) {
    ve.push_back(h);
    h = h * 3 + 1;
  }

  for (int i = ve.size(); i >= 0; i--)
    InsertionSort(n, ve[i]);
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &num[i]);
  ShellSort(n);
  printf("%d\\n", ve.size());
  for (int i = ve.size() - 1; i >= 0; i--) {
    printf("%d", ve[i]);
    if (i != 0)
      printf(" ");
  }
  printf("\\n");
  printf("%d\\n", cnt);
  for (int i = 0; i < n; i++) {
    printf("%d", num[i]);
    if (i != n - 1)
      printf("\\n");
  }
  return 0;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

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

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

void shellSort(int R[], 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(R, n, G[i]);
  }
}

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

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

long long cnt;
int R[1000000];
vector<int> G;

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

void shellSort(int R[], 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(R, n, G[i]);
  }
}

int main() {
  int R[200], n;
  cin >> n;
  int j;
  for (int i = 0; i < n; i++) {
    scanf("%d", &R[i]);
  }
  int key;
  cnt = 0;
  shellSort(R, 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", R[i]);
  }
}
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: WA

Fixed submission:

#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 % 3 == 1 || i == y) {

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

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

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

int main() {
  int n;
  cin >> n;
  int A[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }
  int cnt = 0;
  vector<int> 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--) {
    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 << G.size() << endl;
  for (int i = G.size() - 1; i >= 0; i--) {
    cout << G[i] << (i == 0 ? "" : " ") << flush;
  }
  cout << endl;
  cout << cnt << endl;
  for (int i = 0; i < n; i++) {
    cout << A[i] << endl;
  }
  return 0;
}


Error: -11

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  int A[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", A[i]);
  }
  int cnt = 0;
  vector<int> 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--) {
    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 << G.size() << endl;
  for (int i = G.size() - 1; i >= 0; i--) {
    cout << G[i] << (i == 0 ? "" : " ") << flush;
  }
  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 <bits/stdc++.h>

using namespace std;

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

long shellSort(vector<long> &A, int n, int m, vector<int> &G) {
  long cnt = 0;
  for (int i = 0; i < m; i++) {
    cnt += insertionSort(A, n, G.at(i));
  }
  return cnt;
}

void print(int m, vector<int> &G) {
  cout << m << endl;
  string sep = "";
  for (int g : G) {
    cout << sep << g;
    sep = " ";
  }
  cout << endl;
}

int main() {
  int N;
  cin >> N;
  vector<long> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }

  int upper = ceil(pow(N, 3.0 / 2.0));

  vector<int> G;
  int m = 0;
  int v = 1;
  for (;;) {
    if (m + 1 > 100)
      break;
    if (v > N)
      break;
    m++;
    G.push_back(v);
    v = 3 * v + 1;
  }
  reverse(G.begin(), G.end());

  long cnt = shellSort(A, N, m, G);

  print(m, G);

  cout << cnt << endl;

  for (int a : A) {
    cout << a << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

long shellSort(vector<long> &A, int n, int m, vector<int> &G) {
  long cnt = 0;
  for (int i = 0; i < m; i++) {
    cnt += insertionSort(A, n, G.at(i));
  }
  return cnt;
}

void print(int m, vector<int> &G) {
  cout << m << endl;
  string sep = "";
  for (int g : G) {
    cout << sep << g;
    sep = " ";
  }
  cout << endl;
}

int main() {
  int N;
  cin >> N;
  vector<long> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }

  int upper = ceil(pow(N, 3.0 / 2.0));

  vector<int> G;
  int m = 1;
  G.push_back(1);
  for (;;) {
    int v = 3 * m + 1;
    if (m + 1 > 100)
      break;
    if (v > N)
      break;
    m++;
    G.push_back(v);
  }
  reverse(G.begin(), G.end());

  long cnt = shellSort(A, N, m, G);

  print(m, G);

  cout << cnt << endl;

  for (int a : A) {
    cout << a << endl;
  }
}