Problem p02262 (C++)

Buggy submission (status: Runtime Error):
/**
 * Shell Sort
?¬????????????°?????????????????\?????????????????¨?????? n
????????´??°???????????°??? A
??????????????´?????????????????°????????§??????

1  insertionSort(A, n, g)
2      for i = g to n-1
3          v = A[i]
4          j = i - g
5          while j >= 0 && A[j] > v
6              A[j+g] = A[j]
7              j = j - g
8              cnt++
9          A[j+g] = v
10
11 shellSort(A, n)
12     cnt = 0
13     m = ?
14     G[] = {?, ?,..., ?}
15     for i = 0 to m-1
16         insertionSort(A, n, G[i])
shellSort(A, n) ????????????????????? g
????????¢??????????´??????????????±???¨???????????\???????????§??????
insertionSort(A, n, g) ?????????????????§??????????????? g
????????????????????°????????????????????????????????§??????????????¨?¨?????????????

???????????????????????? ?
??????????????????????????°???????????????????????????????????????n ??¨??°??? A
???????????????????????§???????????????????????? m???m ????????´??°
Gi(i=0,1,...,m???1)?????\???
A????????????????????????????????????????????°???????????????????????????????????????????????????????????\???????????¶??????
??????????????????????????????

1???m???100
0???Gi???n
cnt ????????? ???n1.5??? ????¶??????????????????????
??\???
1 ???????????´??° n ?????????????????????????¶???? n ????????? n ????????´??°
Ai(i=0,1,...,n???1)
????????????????????????

??????
1 ???????????´??° m???2 ????????? m ????????´??° Gi(i=0,1,...,m???1)
????????????????????§??????????????????????????? 3 ????????????G
?????¨????????´??????????????°???????????????????????´??????
cnt??????????????????????????????????????? ?¶???? n
????????´????????? Ai(i=0,1,...,n???1) ??????????????????????????????

??????????????§?????????????????\????????????????????°????§£??????????????????????????¶???????????????????????¨?????£?§£??¨???????????????

??¶?´?
1???n???1,000,000
0???Ai???10^9
??\?????? 1
5
5
1
4
3
2
????????? 1
2
4 1
3
1
2
3
4
5
??\?????? 2
3
3
2
1
????????? 2
1
1
3
1
2
3
 */

#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <vector>

// const unsigned short m = 1;
// const std::vector<unsigned int> G = {1};

unsigned int insertionSort(std::vector<unsigned int> &A,
                           const unsigned int &g) {
  unsigned int cnt = 0;
  unsigned int n = A.size();
  for (size_t i = g; i < n; i++) {
    unsigned 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;
}

unsigned int shellSort(std::vector<unsigned int> &A,
                       const std::vector<unsigned int> &G) {
  unsigned int cnt = 0;
  unsigned short m = G.size();
  for (size_t i = 0; i < m; i++) {
    cnt += insertionSort(A, G[i]);
  }
  return cnt;
}

void printVector(const std::vector<unsigned int> &v) {
  std::ostringstream oss;
  std::copy(v.begin(), v.end() - 1,
            std::ostream_iterator<unsigned int>(oss, " "));
  oss << v.back();
  std::cout << oss.str() << std::endl;
}

int main(int argc, char const *argv[]) {
  unsigned int n;
  std::string Param;
  std::istringstream iss;

  while (true) {

    getline(std::cin, Param);
    iss.str(Param);
    iss >> n;
    iss.clear();

    if (n < 1 || n > 1000000) {
      std::cout << "Error: 1 <= n <= 1,000,000" << std::endl;
    } else {
      break;
    }
  }

  std::vector<unsigned int> A(n, 0);
  std::vector<unsigned int> G;
  for (size_t i = 1; i < n; i = i * 3 + 1) {
    G.push_back(i);
  }
  std::sort(G.begin(), G.end(), std::greater<unsigned int>());
  const unsigned int m = G.size();

  for (size_t i = 0; i < n; i++) {
    getline(std::cin, Param);
    iss.str(Param);
    iss >> A[i];
    iss.clear();
  }
  unsigned int cnt = shellSort(A, G);

  std::cout << m << std::endl;
  printVector(G);
  std::cout << cnt << std::endl;
  for (size_t i = 0; i < n; i++) {
    std::cout << A[i] << std::endl;
  }
  return 0;
}

Fixed submission:
/**
 * Shell Sort
?¬????????????°?????????????????\?????????????????¨?????? n
????????´??°???????????°??? A
??????????????´?????????????????°????????§??????

1  insertionSort(A, n, g)
2      for i = g to n-1
3          v = A[i]
4          j = i - g
5          while j >= 0 && A[j] > v
6              A[j+g] = A[j]
7              j = j - g
8              cnt++
9          A[j+g] = v
10
11 shellSort(A, n)
12     cnt = 0
13     m = ?
14     G[] = {?, ?,..., ?}
15     for i = 0 to m-1
16         insertionSort(A, n, G[i])
shellSort(A, n) ????????????????????? g
????????¢??????????´??????????????±???¨???????????\???????????§??????
insertionSort(A, n, g) ?????????????????§??????????????? g
????????????????????°????????????????????????????????§??????????????¨?¨?????????????

???????????????????????? ?
??????????????????????????°???????????????????????????????????????n ??¨??°??? A
???????????????????????§???????????????????????? m???m ????????´??°
Gi(i=0,1,...,m???1)?????\???
A????????????????????????????????????????????°???????????????????????????????????????????????????????????\???????????¶??????
??????????????????????????????

1???m???100
0???Gi???n
cnt ????????? ???n1.5??? ????¶??????????????????????
??\???
1 ???????????´??° n ?????????????????????????¶???? n ????????? n ????????´??°
Ai(i=0,1,...,n???1)
????????????????????????

??????
1 ???????????´??° m???2 ????????? m ????????´??° Gi(i=0,1,...,m???1)
????????????????????§??????????????????????????? 3 ????????????G
?????¨????????´??????????????°???????????????????????´??????
cnt??????????????????????????????????????? ?¶???? n
????????´????????? Ai(i=0,1,...,n???1) ??????????????????????????????

??????????????§?????????????????\????????????????????°????§£??????????????????????????¶???????????????????????¨?????£?§£??¨???????????????

??¶?´?
1???n???1,000,000
0???Ai???10^9
??\?????? 1
5
5
1
4
3
2
????????? 1
2
4 1
3
1
2
3
4
5
??\?????? 2
3
3
2
1
????????? 2
1
1
3
1
2
3
 */

#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <vector>

// const unsigned short m = 1;
// const std::vector<unsigned int> G = {1};

unsigned int insertionSort(std::vector<unsigned int> &A,
                           const unsigned int &g) {
  unsigned int cnt = 0;
  unsigned int n = A.size();
  for (size_t i = g; i < n; i++) {
    unsigned 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;
}

unsigned int shellSort(std::vector<unsigned int> &A,
                       const std::vector<unsigned int> &G) {
  unsigned int cnt = 0;
  unsigned short m = G.size();
  for (size_t i = 0; i < m; i++) {
    cnt += insertionSort(A, G[i]);
  }
  return cnt;
}

void printVector(const std::vector<unsigned int> &v) {
  std::ostringstream oss;
  std::copy(v.begin(), v.end() - 1,
            std::ostream_iterator<unsigned int>(oss, " "));
  oss << v.back();
  std::cout << oss.str() << std::endl;
}

int main(int argc, char const *argv[]) {
  unsigned int n;
  std::string Param;
  std::istringstream iss;

  while (true) {

    getline(std::cin, Param);
    iss.str(Param);
    iss >> n;
    iss.clear();

    if (n < 1 || n > 1000000) {
      std::cout << "Error: 1 <= n <= 1,000,000" << std::endl;
    } else {
      break;
    }
  }

  std::vector<unsigned int> A(n, 0);
  std::vector<unsigned int> G;
  for (size_t i = 1; i <= n; i = i * 3 + 1) {
    G.push_back(i);
  }
  std::sort(G.begin(), G.end(), std::greater<unsigned int>());
  const unsigned int m = G.size();

  for (size_t i = 0; i < n; i++) {
    getline(std::cin, Param);
    iss.str(Param);
    iss >> A[i];
    iss.clear();
  }
  unsigned int cnt = shellSort(A, G);

  std::cout << m << std::endl;
  printVector(G);
  std::cout << cnt << std::endl;
  for (size_t i = 0; i < n; i++) {
    std::cout << A[i] << std::endl;
  }
  return 0;
}