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

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

int cnt = 0;
int m = 20;
int G[20] = {};
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 i = m - 1; i >= 0; i--) {
    if (i == m - 1)
      G[i] = 1;
    else
      G[i] = G[i + 1] * 3 + 1;
  }
  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);
  int mtrue = 0;
  for (int i = 0; i < m; i++) {
    if (G[i] <= n)
      mtrue++;
  }
  cout << mtrue << endl;
  for (int i = 0; i < m; i++) {
    if (G[i] <= n)
      cout << G[i] << " ";
  }
  cout << endl << cnt << endl;
  for (int i = 0; i < n; i++) {
    cout << A[i] << endl;
  }

  return 0;
}


Error: -11

Fixed submission:

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

int cnt = 0;
int m = 100;
int G[100] = {};
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 i = m - 1; i >= 0; i--) {
    if (i == m - 1)
      G[i] = 1;
    else
      G[i] = G[i + 1] * 3 + 1;
  }
  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);
  int mtrue = 0;
  for (int i = 0; i < m; i++) {
    if (G[i] <= n)
      mtrue++;
  }
  cout << mtrue << endl;
  for (int i = 0; i < m; i++) {
    if (G[i] <= n)
      cout << G[i] << " ";
  }
  cout << endl << 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;

int cnt;

void insertionSort(int A[], int n, int g) {
  for (int i = g; i <= n - 1; ++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) {
  cnt = 0;
  int g[] = {797161, 265720, 88573, 29524, 9841, 3280, 1093,
             364,    121,    40,    13,    4,    1};

  int M = sizeof(g) / sizeof(int);
  vector<int> G;
  for (int i = M - 1; i >= 0; --i)
    G.push_back(g[i]);
  int m = 0;
  while (m < M && G[m] <= n)
    ++m;
  G.resize(m);
  reverse(G.begin(), G.end());

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

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

int n;
int A[1000000];

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

Error: TLE

Fixed submission:

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

int cnt;

void insertionSort(int A[], int n, int g) {
  for (int i = g; i <= n - 1; ++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) {
  cnt = 0;

  int g[] = {531441, 177147, 59049, 19683, 6561, 2187, 729,
             243,    81,     27,    9,     3,    1};
  int M = sizeof(g) / sizeof(int);
  vector<int> G;
  for (int i = M - 1; i >= 0; --i)
    G.push_back(g[i]);
  int m = 0;
  while (m < M && G[m] <= n)
    ++m;
  G.resize(m);
  reverse(G.begin(), G.end());

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

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

int n;
int A[1000000];

int main() {
  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 <algorithm>
#include <iostream>
#include <vector>

using namespace std;
using int_vec = vector<long>;

void print(const int_vec &v) {
  int_vec::const_iterator it;
  for (it = v.begin(); it != v.end() - 1; ++it) {
    cout << *it << " ";
  }
  cout << *it << endl;
}

int insertion_sort(int_vec &v, int g) {
  int count = 0;
  for (int i = g; i < v.size(); ++i) {
    auto x = v[i];
    int j = i - g;
    for (; j >= 0 && v[j] > x; j -= g) {
      v[j + g] = v[j];
      ++count;
    }
    v[j + g] = x;
  }
  return count;
}

int shell_sort(int_vec &v) {
  int_vec G;
  for (int i = 1; i <= v.size(); i = i * 3 + 1) {
    G.push_back(i);
  }
  reverse(G.begin(), G.end());
  cout << G.size() << endl;
  print(G);

  int count = 0;
  for (auto it = G.begin(); it != G.end(); ++it) {
    count += insertion_sort(v, *it);
  }
  return count;
}

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

  int_vec v;
  for (int i = 0; i < n; ++i) {
    long x;
    cin >> x;
    v.push_back(x);
  }

  auto c = shell_sort(v);
  cout << c << endl;
  // cerr << "debug:" << v.size() << endl;
  for (auto it = v.begin(); it != v.end(); ++it) {
    cout << *it << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

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

using namespace std;
using int_vec = vector<int>;

void print(const int_vec &v) {
  int_vec::const_iterator it;
  for (it = v.begin(); it != v.end() - 1; ++it) {
    cout << *it << " ";
  }
  cout << *it << endl;
}

int insertion_sort(int_vec &v, int g) {
  int count = 0;
  for (int i = g; i < v.size(); ++i) {
    int x = v[i];
    int j = i - g;
    for (; j >= 0 && v[j] > x; j -= g) {
      v[j + g] = v[j];
      ++count;
    }
    v[j + g] = x;
  }
  return count;
}

int shell_sort(int_vec &v) {
  int m = min(static_cast<int>(v.size() / 3) - 1, 100);
  int_vec G;
  for (int i = 0; i <= m; ++i) {
    auto g = 3 * i + 1;
    G.push_back(g);
  }
  reverse(G.begin(), G.end());
  cout << G.size() << endl;
  print(G);

  int count = 0;
  for (auto it = G.begin(); it != G.end(); ++it) {
    count += insertion_sort(v, *it);
  }
  return count;
}

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

  int_vec v;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    v.push_back(x);
  }

  auto c = shell_sort(v);
  cout << c << endl;
  for (auto it = v.begin(); it != v.end(); ++it) {
    cout << *it << endl;
  }

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

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

#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); ++i)

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

int shellSort(int A[], int N, vector<int> &G) {
  int h = 1;
  while (h <= N) {
    G.pb(h);
    h = 3 * h + 1;
  }

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

  return cnt;
}

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

  vector<int> G;
  int cnt = shellSort(A, N, G);

  cout << G.size() << endl;
  for (int i = G.size() - 1; i > 0; --i) {
    cout << G[i] << " ";
  }
  cout << G[0] << endl;
  cout << cnt << endl;
  rep(i, N) cout << A[i] << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

#define pb push_back

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

int shellSort(int A[], int N, vector<int> &G) {
  int h = 1;
  while (h <= N) {
    G.pb(h);
    h = 3 * h + 1;
  }

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

  return cnt;
}

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

  vector<int> G;
  int cnt = shellSort(A, N, G);

  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 v : A)
    cout << v << endl;

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

#include <iostream>
using namespace std;

const int MAX = 1000000;
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 = 1;
  int g[MAX];
  int i = 1;
  g[0] = 1;
  while (1 + 3 * g[i - 1] <= n) {
    g[i] = 1 + 3 * g[i - 1];
    i++;
    m++;
  }
  cnt = 0;
  for (int j = m - 1; j >= 0; j--)
    insertionSort(a, n, g[j]);

  cout << m << endl;
  for (int j = m - 1; j >= 0; j--) {
    cout << g[j];
    if (j > 0)
      cout << " ";
    else
      cout << endl;
  }
  cout << cnt << endl;
}

int main() {
  int n;
  int a[MAX];
  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;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

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;
  int g[100];
  int i = 0;
  while (1 + 3 * i <= n) {
    g[i] = 1 + 3 * i;
    i++;
    m++;
  }
  cnt = 0;
  for (int j = m - 1; j >= 0; j--)
    insertionSort(a, n, g[j]);

  cout << m << endl;
  for (int j = m - 1; j >= 0; j--) {
    cout << g[j];
    if (j > 0)
      cout << " ";
    else
      cout << endl;
  }
  cout << cnt << endl;
}

int main() {
  int n;
  int a[100];
  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;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
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)
      printf(" ");
  }
  printf("\\n");
  printf("%d\\n", cnt);
  for (int i = 0; i < n; ++i) {
    printf("%d\\n", A[i]);
  }

  return 0;
}

Error: -11

Fixed submission:

#include "bits/stdc++.h"
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)
      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 <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

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

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

int main() {
  scanf("%d", &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 <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

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

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

int main() {
  int A[100];
  scanf("%d", &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;
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

void insertion_sort(vector<int> &list, int interval, int &count) {
  for (int i = interval; i < list.size(); ++i) {
    int key = list[i];
    int j = i - interval;
    while (j >= 0 && list[j] > key) {
      list[j + interval] = list[j];
      j = j - interval;
      count++;
    }
    list[j + interval] = key;
  }
}

void shell_sort(vector<int> &list, int &count) {
  int index = max(0, (int)(log10(list.size()) / log10(2)) - 1);
  vector<int> intervals;
  while (index >= 0) {
    intervals.push_back(pow(2, index));
    index--;
  }
  cout << intervals.size() << endl;
  for (auto iter = intervals.begin(); iter != intervals.end() - 1; ++iter) {
    cout << *iter << " ";
  }
  cout << intervals.back() << endl;
  for (int interval : intervals) {
    insertion_sort(list, interval, count);
  }
}

int main() {
  int N;
  cin >> N;
  string str;
  vector<int> list;
  for (int i = 0; i < N; ++i) {
    cin >> str;
    list.push_back(stoi(str));
  }

  int count = 0;
  shell_sort(list, count);
  cout << count << endl;
  for (auto iter = list.begin(); iter != list.end(); ++iter) {
    cout << *iter << endl;
  }
}

Error: WA

Fixed submission:

#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

void insertion_sort(vector<int> &list, int interval, int &count) {
  for (int i = interval; i < list.size(); ++i) {
    int key = list[i];
    int j = i - interval;
    while (j >= 0 && list[j] > key) {
      list[j + interval] = list[j];
      j = j - interval;
      count++;
    }
    list[j + interval] = key;
  }
}

void shell_sort(vector<int> &list, int &count) {
  int index = (int)(log10(list.size()) / log10(2)) - 1;
  vector<int> intervals;
  while (index >= 0) {
    intervals.push_back(pow(2, index));
    index--;
  }
  cout << intervals.size() << endl;
  for (auto iter = intervals.begin(); iter != intervals.end() - 1; ++iter) {
    cout << *iter << " ";
  }
  cout << intervals.back() << endl;
  for (int interval : intervals) {
    insertion_sort(list, interval, count);
  }
}

int main() {
  int N;
  cin >> N;
  string str;
  vector<int> list;
  for (int i = 0; i < N; ++i) {
    cin >> str;
    list.push_back(stoi(str));
  }

  int count = 0;
  shell_sort(list, count);
  cout << count << endl;
  for (auto iter = list.begin(); iter != list.end(); ++iter) {
    cout << *iter << endl;
  }
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdio>

int cnt;
int m;
int G[100];

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) {
  cnt = 0;
  m = 1;
  while (pow(2, m) < n) {
    m++;
    if (m == 100) {
      break;
    }
  }
  G[m - 1] = 1;
  for (int i = m - 2; i >= 0; i--) {
    G[i] = G[i + 1] * 2;
  }
  for (int i = 0; i < m; i++) {
    insertionSort(A, n, G[i]);
  }
}

int main() {
  int n;
  int A[1000000];
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }
  shellSort(A, n);
  printf("%d\\n", m);
  for (int i = 0; i < m; i++) {
    printf("%d", G[i]);
    if (i + 1 == m) {
      printf("\\n");
    } else {
      printf(" ");
    }
  }
  printf("%d\\n", cnt);
  for (int i = 0; i < n; i++) {
    printf("%d\\n", A[i]);
  }
}

Error: WA

Fixed submission:

#include <cmath>
#include <cstdio>

int cnt;
int m;
int G[100];

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) {
  cnt = 0;
  m = 1;
  while (pow(2, m) < n) {
    m++;
    if (m == 100) {
      break;
    }
  }
  G[m - 1] = 1;
  for (int i = m - 2; i >= 0; i--) {
    G[i] = G[i + 1] * 2;
  }
  for (int i = 0; i < m; i++) {
    insertionSort(A, n, G[i]);
  }
}

int main() {
  int n;
  int A[10000];
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }
  shellSort(A, n);
  printf("%d\\n", m);
  for (int i = 0; i < m; i++) {
    printf("%d", G[i]);
    if (i + 1 == m) {
      printf("\\n");
    } else {
      printf(" ");
    }
  }
  printf("%d\\n", cnt);
  for (int i = 0; i < n; i++) {
    printf("%d\\n", A[i]);
  }
}
Problem p02262 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

void insertionSort(vector<double> &a, int g, long &cnt) {
  int j;
  double v;
  for (int i = g; i < a.size(); i++) {
    v = a[i];
    j = i - g;
    while (j >= 0 && a[j] > v) {
      a[j + g] = a[j];
      j = j - g;
      cnt++;
      if (cnt > (int)pow(a.size(), 1.5)) {
        return;
      }
    }
    a[j + g] = v;
  }
}

int shellSort(vector<double> &a, vector<int> &g) {
  long cnt = 0;
  for (int i = 0; i < g.size(); i++) {
    insertionSort(a, g[i], cnt);
    if (cnt > (int)pow(a.size(), 1.5)) {
      break;
    }
  }
  return cnt;
}

int main() {
  int n;
  long cnt;
  cin >> n;
  vector<double> a(n, 0);
  vector<double> org(a);
  vector<int> g;
  for (int i = 0; i < n; i++) {
    cin >> org[i];
  }
  for (int m = 100; m >= 1; m--) {
    a = org;
    g.resize(m, 1);
    for (int i = 0; i < m; i++) {
      g[i] = (pow(3, m - i) - 1) / 2;
    }
    if (g[0] < 0 || n < g[0]) {
      continue;
    } else {
      cnt = shellSort(a, g);
      if (cnt <= pow(n, 1.5)) {
        break;
      }
    }
  }
  cout << setprecision(10) << g.size() << endl;
  for (int i = 0; i < g.size(); i++) {
    cout << g[i];
    if (i == g.size() - 1) {
      cout << endl;
    } else {
      cout << " ";
    }
  }
  cout << cnt << endl;
  for (int i = 0; i < n; i++) {
    cout << a[i] << endl;
  }
}


Error: TLE

Fixed submission:

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

using namespace std;

void insertionSort(vector<double> &a, int g, long &cnt) {
  int j;
  double v;
  for (int i = g; i < a.size(); i++) {
    v = a[i];
    j = i - g;
    while (j >= 0 && a[j] > v) {
      a[j + g] = a[j];
      j = j - g;
      cnt++;
      if (cnt > (int)pow(a.size(), 1.5)) {
        return;
      }
    }
    a[j + g] = v;
  }
}

int shellSort(vector<double> &a, vector<int> &g) {
  long cnt = 0;
  for (int i = 0; i < g.size(); i++) {
    insertionSort(a, g[i], cnt);
    if (cnt > (int)pow(a.size(), 1.5)) {
      break;
    }
  }
  return cnt;
}

int digit(int i) {
  int digit = 0;
  while (i != 0) {
    i /= 10;
    digit++;
  }
  return digit;
}

int main() {
  int n;
  long cnt;
  cin >> n;
  vector<double> a(n, 0);
  vector<double> org(a);
  vector<int> g;
  for (int i = 0; i < n; i++) {
    cin >> org[i];
  }
  for (int m = digit(n); m <= 100; m++) {
    a = org;
    g.resize(m, 1);
    for (int i = 0; i < m; i++) {
      g[i] = (pow(3, m - i) - 1) / 2;
    }
    cnt = shellSort(a, g);
    if (cnt <= pow(n, 1.5)) {
      break;
    }
  }
  cout << setprecision(10) << g.size() << endl;
  for (int i = 0; i < g.size(); i++) {
    cout << g[i];
    if (i == g.size() - 1) {
      cout << endl;
    } else {
      cout << " ";
    }
  }
  cout << cnt << endl;
  for (int i = 0; i < n; i++) {
    cout << a[i] << endl;
  }
}
Problem p02262 (C++) — buggy submission, verdict: Runtime Error

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

#define MAX_N 1000000

using namespace std;

long long A[MAX_N];
vector<int> G;
int cnt = 0;

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

void getG(int n) {
  int k = 1;
  while (k <= n) {
    G.push_back(k);
    k = 3 * k + 1;
  }
}

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

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

  getG(n);
  printf("%d\\n", G.size());
  for (i = G.size() - 1; i > 0; i--) {
    printf("%d ", G[i]);
  }
  printf("%d\\n", G[0]);

  for (i = G.size() - 1; i >= 0; i--) {
    insertSort(n, G[i]);
  }
  printf("%d\\n", cnt);
  printNums(n);
}

Error: 0

Fixed submission:

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

#define MAX_N 1000000

using namespace std;

long long A[MAX_N];
vector<int> G;
int cnt = 0;

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

void getG(int n) {
  int k = 1;
  while (k < n) {
    G.push_back(k);
    k = 3 * k + 1;
  }
}

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

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

  getG(n);
  printf("%d\\n", G.size());
  for (i = G.size() - 1; i > 0; i--) {
    printf("%d ", G[i]);
  }
  printf("%d\\n", G[0]);

  for (i = G.size() - 1; i >= 0; i--) {
    insertSort(n, G[i]);
  }
  printf("%d\\n", cnt);
  printNums(n);
}