problem_id
stringlengths
6
6
language
stringclasses
2 values
original_status
stringclasses
3 values
original_src
stringlengths
19
243k
changed_src
stringlengths
19
243k
change
stringclasses
3 values
i1
int64
0
8.44k
i2
int64
0
8.44k
j1
int64
0
8.44k
j2
int64
0
8.44k
error
stringclasses
270 values
stderr
stringlengths
0
226k
p02262
C++
Runtime Error
#include <iostream> #include <string> using namespace std; long d = 0, z = 0, b[1000000]; void ChentrucTiep(long long a[], long n, long h) { int x, k; for (int i = h; i < n; i++) { x = a[i]; k = i - h; while (k >= 0 && a[k] > x) { a[k + h] = a[k]; k -= h; d++; } a[k + h] = x; } } void shellSort(long long a[], long n) { for (int i = 1; i <= n;) { b[z] = i; z += 1; i = i * 3 + 1; } for (int x = z - 1; x >= 0; x--) ChentrucTiep(a, n, b[x]); } int main() { long n; long long a[100000]; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; shellSort(a, n); cout << z << endl; for (int i = z - 1; i >= 0; i--) { cout << b[i]; if (i) cout << " "; } cout << endl << d << endl; for (int i = 0; i < n; i++) { cout << a[i] << endl; } return 0; }
#include <iostream> #include <string> using namespace std; long d = 0, z = 0, b[1000000]; void ChentrucTiep(long long a[], long n, long h) { int x, k; for (int i = h; i < n; i++) { x = a[i]; k = i - h; while (k >= 0 && a[k] > x) { a[k + h] = a[k]; k -= h; d++; } a[k + h] = x; } } void shellSort(long long a[], long n) { for (int i = 1; i <= n;) { b[z] = i; z += 1; i = i * 3 + 1; } for (int x = z - 1; x >= 0; x--) ChentrucTiep(a, n, b[x]); } int main() { long n; long long a[1000000]; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; shellSort(a, n); cout << z << endl; for (int i = z - 1; i >= 0; i--) { cout << b[i]; if (i) cout << " "; } cout << endl << d << endl; for (int i = 0; i < n; i++) { cout << a[i] << endl; } return 0; }
replace
30
31
30
31
0
p02262
C++
Runtime Error
#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; }
#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; }
replace
7
11
7
11
0
p02262
C++
Runtime Error
#include <iostream> using namespace std; int insertion_sort(int *seq, int length, int g) { int temp, count = 0; int v, j; for (int i = g; i < length; i++) { v = seq[i]; j = i - g; while (j >= 0 && seq[j] > v) { seq[j + g] = seq[j]; j = j - g; count++; } seq[j + g] = v; } return count; } int shellSort(int *A, int n) { int cnt = 0, m; int i = 1; int G[1000000]; for (int i = 1, j = 0; i < n; j++, i *= 2) { G[j] = i; m = j; } cout << m + 1 << endl; for (int i = m; i >= 0; i--) { cnt += insertion_sort(A, n, G[i]); cout << G[i] << " "; } cout << endl; return cnt; } int main() { int N, count; int seq[100]; cin >> N; for (int i = 0; i < N; i++) cin >> seq[i]; count = shellSort(seq, N); cout << count << endl; for (int k = 0; k < N; k++) cout << seq[k] << endl; }
#include <iostream> using namespace std; int insertion_sort(int *seq, int length, int g) { int temp, count = 0; int v, j; for (int i = g; i < length; i++) { v = seq[i]; j = i - g; while (j >= 0 && seq[j] > v) { seq[j + g] = seq[j]; j = j - g; count++; } seq[j + g] = v; } return count; } int shellSort(int *A, int n) { int cnt = 0, m; int i = 1; int G[1000000]; for (int i = 1, j = 0; i < n; j++, i *= 2) { G[j] = i; m = j; } cout << m + 1 << endl; for (int i = m; i >= 0; i--) { cnt += insertion_sort(A, n, G[i]); cout << G[i] << " "; } cout << endl; return cnt; } int main() { int N, count; int seq[1000000]; cin >> N; for (int i = 0; i < N; i++) cin >> seq[i]; count = shellSort(seq, N); cout << count << endl; for (int k = 0; k < N; k++) cout << seq[k] << endl; }
replace
41
42
41
42
0
p02262
C++
Runtime Error
#include <cstdlib> #include <iostream> #include <sstream> #include <stdio.h> #include <string.h> using namespace std; #define BUF_SIZE 1024 int count = 0; void insertionSort(int array[], int n, int g) { int v, j; for (int i = g; i < n; i++) { v = array[i]; j = i - g; while (j >= 0 && array[j] > v) { array[j + g] = array[j]; //??°??????????????§??????????????? j = j - g; count++; } array[j + g] = v; } } void shellSort(int array[], int n) { int m = 0; int s = 1; while ((n / s) > 0) { s *= 2; m++; // 2^(mult-1)?????§?????????????????¨???????¨? } int G[20]; for (int k = 0, t = 1; k < m; k++) { G[k] = n / t; t *= 2; } for (int i = 0; i < m; i++) { insertionSort(array, n, G[i]); } printf("%d\n", m); for (int i = 0; i < m; i++) { if (i != m - 1) { printf("%d ", G[i]); } else { printf("%d\n", G[i]); } } printf("%d\n", count); for (int i = 0; i < n; i++) { printf("%d\n", array[i]); } } int main(int argc, char *argv[]) { char buf[BUF_SIZE]; int n = atoi(fgets(buf, BUF_SIZE, stdin)); int array[1000]; for (int i = 0; i < n; i++) { cin >> array[i]; } shellSort(array, n); }
#include <cstdlib> #include <iostream> #include <sstream> #include <stdio.h> #include <string.h> using namespace std; #define BUF_SIZE 1024 int count = 0; void insertionSort(int array[], int n, int g) { int v, j; for (int i = g; i < n; i++) { v = array[i]; j = i - g; while (j >= 0 && array[j] > v) { array[j + g] = array[j]; //??°??????????????§??????????????? j = j - g; count++; } array[j + g] = v; } } void shellSort(int array[], int n) { int m = 0; int s = 1; while ((n / s) > 0) { s *= 2; m++; // 2^(mult-1)?????§?????????????????¨???????¨? } int G[20]; for (int k = 0, t = 1; k < m; k++) { G[k] = n / t; t *= 2; } for (int i = 0; i < m; i++) { insertionSort(array, n, G[i]); } printf("%d\n", m); for (int i = 0; i < m; i++) { if (i != m - 1) { printf("%d ", G[i]); } else { printf("%d\n", G[i]); } } printf("%d\n", count); for (int i = 0; i < n; i++) { printf("%d\n", array[i]); } } int main(int argc, char *argv[]) { char buf[BUF_SIZE]; int n = atoi(fgets(buf, BUF_SIZE, stdin)); int array[1000000]; for (int i = 0; i < n; i++) { cin >> array[i]; } shellSort(array, n); }
replace
63
64
63
64
0
p02262
C++
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[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; } }
#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; } }
replace
39
40
39
40
0
p02262
C++
Runtime Error
#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; // 間隔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) { // 数列 G = { 1, 4, 13, 40, 121, 364, 1093, ... } を生成 for (int h = 1;;) { G.push_back(h); h = 3 * h + 1; } for (int i = G.size() - 1; i >= 0; i--) { // 逆順に G[i] = g を指定 insertionSort(A, n, G[i]); } } int main() { cin >> n; // より速い入力scanf関数を使用 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; }
#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; // 間隔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) { // 数列 G = { 1, 4, 13, 40, 121, 364, 1093, ... } を生成 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--) { // 逆順に G[i] = g を指定 insertionSort(A, n, G[i]); } } int main() { cin >> n; // より速い入力scanf関数を使用 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; }
insert
30
30
30
32
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p02262
C++
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[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; }
#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; }
replace
57
58
57
58
0
p02262
C++
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; }
#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; }
replace
37
38
37
38
0
p02262
C++
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"); }
#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"); }
replace
49
50
49
50
0
sh: 1: pause: not found
p02262
C++
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; 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; } }
#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; } }
replace
32
33
32
33
0
p02262
C++
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; }
#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; }
replace
31
32
31
32
0
p02262
C++
Runtime Error
#include <algorithm> #include <iostream> using namespace std; int n, a[1111111], c, g[20]; int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } g[0] = 1; c = 0; while (true) { g[c + 1] = g[c] * 3 + 1; if (g[++c] >= n) break; } int ret = 0; for (int i = c - 1; i >= 0; i--) { for (int j = g[i]; j < n; j--) { int v = a[j], w = j - g[i]; while (w >= 0 && a[w] > v) { a[w + g[i]] = a[w]; w -= g[i]; ret++; } a[w + g[i]] = v; } } cout << c << endl; for (int i = c - 1; i >= 0; i--) { if (i) cout << g[i] << ' '; else cout << g[i] << endl; } cout << ret << endl; for (int i = 0; i < n; i++) { cout << a[i] << endl; } return 0; }
#include <algorithm> #include <iostream> using namespace std; int n, a[1111111], c, g[20]; int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } g[0] = 1; c = 0; while (true) { g[c + 1] = g[c] * 3 + 1; if (g[++c] >= n) break; } int ret = 0; for (int i = c - 1; i >= 0; i--) { for (int j = g[i]; j < n; j++) { int v = a[j], w = j - g[i]; while (w >= 0 && a[w] > v) { a[w + g[i]] = a[w]; w -= g[i]; ret++; } a[w + g[i]] = v; } } cout << c << endl; for (int i = c - 1; i >= 0; i--) { if (i) cout << g[i] << ' '; else cout << g[i] << endl; } cout << ret << endl; for (int i = 0; i < n; i++) { cout << a[i] << endl; } return 0; }
replace
20
21
20
21
-11
p02262
C++
Runtime Error
#include <cstdio> #include <iostream> #include <vector> using namespace std; long long ShellSort(int elem[], int num) { int i, j, k; int h; int target; long long cnt = 0; vector<int> vect; for (h = 1;;) { if (h <= num) { vect.push_back(h); } else { break; } h = 3 * h + 1; } h = vect.size() - 1; cout << h + 1 << endl; for (i = h; i >= 0; i--) { int g = vect[i]; if (i != h) { cout << " "; } cout << g; for (j = g; j < num; j++) { target = elem[j]; for (k = j - g; k >= 0; k -= g) { if (elem[k] > target) { elem[k + g] = elem[k]; ++cnt; } else { break; } } elem[k + g] = target; } } cout << endl; return cnt; } int main(void) { int i; long long cnt; int num; int elem[100000]; scanf("%d", &num); for (i = 0; i < num; i++) { scanf("%d", &elem[i]); } cnt = ShellSort(elem, num); cout << cnt << endl; for (i = 0; i < num; i++) { cout << elem[i] << endl; } return 0; }
#include <cstdio> #include <iostream> #include <vector> using namespace std; long long ShellSort(int elem[], int num) { int i, j, k; int h; int target; long long cnt = 0; vector<int> vect; for (h = 1;;) { if (h <= num) { vect.push_back(h); } else { break; } h = 3 * h + 1; } h = vect.size() - 1; cout << h + 1 << endl; for (i = h; i >= 0; i--) { int g = vect[i]; if (i != h) { cout << " "; } cout << g; for (j = g; j < num; j++) { target = elem[j]; for (k = j - g; k >= 0; k -= g) { if (elem[k] > target) { elem[k + g] = elem[k]; ++cnt; } else { break; } } elem[k + g] = target; } } cout << endl; return cnt; } int main(void) { int i; long long cnt; int num; int elem[1000000]; scanf("%d", &num); for (i = 0; i < num; i++) { scanf("%d", &elem[i]); } cnt = ShellSort(elem, num); cout << cnt << endl; for (i = 0; i < num; i++) { cout << elem[i] << endl; } return 0; }
replace
51
52
51
52
0
p02262
C++
Memory Limit Exceeded
#ifndef _GLIBCXX_NO_ASSERT #include <cassert> #endif #include <cctype> #include <cerrno> #include <cfloat> #include <ciso646> #include <climits> #include <clocale> #include <cmath> #include <csetjmp> #include <csignal> #include <cstdarg> #include <cstddef> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #if __cplusplus >= 201103L #include <ccomplex> #include <cfenv> #include <cinttypes> #include <cstdalign> #include <cstdbool> #include <cstdint> #include <ctgmath> #include <cwchar> #include <cwctype> #endif #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector> #if __cplusplus >= 201103L #include <array> #include <atomic> #include <chrono> #include <condition_variable> #include <forward_list> #include <future> #include <initializer_list> #include <mutex> #include <random> #include <ratio> #include <regex> #include <scoped_allocator> #include <system_error> #include <thread> #include <tuple> #include <type_traits> #include <typeindex> #include <unordered_map> #include <unordered_set> #endif #define y0 qvya13579 #define y1 qvyb24680 #define j0 qvja13579 #define j1 qvjb24680 #define next qvne13579xt #define prev qvpr13579ev #define INF 1000000007 #define PI acos(-1.0) #define endl "\n" #define IOS \ cin.tie(0); \ ios::sync_with_stdio(false) #define M_P make_pair #define P_B push_back #define U_B upper_bound #define L_B lower_bound #define P_Q priority_queue #define FIR first #define SEC second #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define REP_R(i, n) for (int i = ((int)(n)-1); i >= 0; --i) #define FOR(i, m, n) for (int i = ((int)(m)); i < (int)(n); ++i) #define FOR_R(i, m, n) for (int i = ((int)(m)-1); i >= (int)(n); --i) #define ALL(v) (v).begin(), (v).end() #define RALL(v) (v).rbegin(), (v).rend() #define SIZ(x) ((int)(x).size()) #define COUT(x) cout << (x) << endl #define CIN(x) cin >> (x) #define CIN2(x, y) cin >> (x) >> (y) #define CIN3(x, y, z) cin >> (x) >> (y) >> (z) #define CIN4(x, y, z, w) cin >> (x) >> (y) >> (z) >> (w) #define SCAND(x) scanf("%d", &(x)) #define SCAND2(x, y) scanf("%d%d", &(x), &(y)) #define SCAND3(x, y, z) scanf("%d%d%d", &(x), &(y), &(z)) #define SCAND4(x, y, z, w) scanf("%d%d%d%d", &(x), &(y), &(z), &(w)) #define SCANLLD(x) scanf("%lld", &(x)) #define SCANLLD2(x, y) scanf("%lld%lld", &(x), &(y)) #define SCANLLD3(x, y, z) scanf("%lld%lld%lld", &(x), &(y), &(z)) #define SCANLLD4(x, y, z, w) scanf("%lld%lld%lld%lld", &(x), &(y), &(z), &(w)) #define PRINTD(x) printf("%d\n", (x)) #define PRINTLLD(x) printf("%lld\n", (x)) typedef long long int lli; using namespace std; int ctoi(char c) { if (c >= '0' and c <= '9') { return (int)(c - '0'); } return -1; } int alphabet_pos(char c) { if (c >= 'a' and c <= 'z') { return (int)(c - 'a'); } return -1; } int alphabet_pos_capital(char c) { if (c >= 'A' and c <= 'Z') { return (int)(c - 'A'); } return -1; } vector<string> split(string str, char ch) { int first = 0; int last = str.find_first_of(ch); if (last == string::npos) { last = SIZ(str); } vector<string> result; while (first < SIZ(str)) { string Ssubstr(str, first, last - first); result.push_back(Ssubstr); first = last + 1; last = str.find_first_of(ch, first); if (last == string::npos) { last = SIZ(str); } } return result; } /*--------------- the end of the template --------------------*/ int cnt = 0; void insertionSort(vector<int> &A, int n, int g) { FOR(i, g, n) { int val = A[i]; int j = i - g; while (j >= 0 and A[j] > val) { A[j + g] = A[j]; j -= g; ++cnt; } A[j + g] = val; } } void shellSort(vector<int> &A, int n) { vector<int> G; int k = 1; while (k <= n) { G.push_back(k); k = k * ((int)log(k + 1)) + 1; } PRINTD(SIZ(G)); REP_R(i, SIZ(G)) { if (i != 0) { printf("%d ", G[i]); } else { printf("%d\n", G[i]); } insertionSort(A, n, G[i]); } PRINTD(cnt); } int main() { // IOS; /* making cin faster */ int n; SCAND(n); vector<int> A(n); REP(i, n) { SCAND(A[i]); } shellSort(A, n); REP(i, n) { PRINTD(A[i]); } }
#ifndef _GLIBCXX_NO_ASSERT #include <cassert> #endif #include <cctype> #include <cerrno> #include <cfloat> #include <ciso646> #include <climits> #include <clocale> #include <cmath> #include <csetjmp> #include <csignal> #include <cstdarg> #include <cstddef> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #if __cplusplus >= 201103L #include <ccomplex> #include <cfenv> #include <cinttypes> #include <cstdalign> #include <cstdbool> #include <cstdint> #include <ctgmath> #include <cwchar> #include <cwctype> #endif #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector> #if __cplusplus >= 201103L #include <array> #include <atomic> #include <chrono> #include <condition_variable> #include <forward_list> #include <future> #include <initializer_list> #include <mutex> #include <random> #include <ratio> #include <regex> #include <scoped_allocator> #include <system_error> #include <thread> #include <tuple> #include <type_traits> #include <typeindex> #include <unordered_map> #include <unordered_set> #endif #define y0 qvya13579 #define y1 qvyb24680 #define j0 qvja13579 #define j1 qvjb24680 #define next qvne13579xt #define prev qvpr13579ev #define INF 1000000007 #define PI acos(-1.0) #define endl "\n" #define IOS \ cin.tie(0); \ ios::sync_with_stdio(false) #define M_P make_pair #define P_B push_back #define U_B upper_bound #define L_B lower_bound #define P_Q priority_queue #define FIR first #define SEC second #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define REP_R(i, n) for (int i = ((int)(n)-1); i >= 0; --i) #define FOR(i, m, n) for (int i = ((int)(m)); i < (int)(n); ++i) #define FOR_R(i, m, n) for (int i = ((int)(m)-1); i >= (int)(n); --i) #define ALL(v) (v).begin(), (v).end() #define RALL(v) (v).rbegin(), (v).rend() #define SIZ(x) ((int)(x).size()) #define COUT(x) cout << (x) << endl #define CIN(x) cin >> (x) #define CIN2(x, y) cin >> (x) >> (y) #define CIN3(x, y, z) cin >> (x) >> (y) >> (z) #define CIN4(x, y, z, w) cin >> (x) >> (y) >> (z) >> (w) #define SCAND(x) scanf("%d", &(x)) #define SCAND2(x, y) scanf("%d%d", &(x), &(y)) #define SCAND3(x, y, z) scanf("%d%d%d", &(x), &(y), &(z)) #define SCAND4(x, y, z, w) scanf("%d%d%d%d", &(x), &(y), &(z), &(w)) #define SCANLLD(x) scanf("%lld", &(x)) #define SCANLLD2(x, y) scanf("%lld%lld", &(x), &(y)) #define SCANLLD3(x, y, z) scanf("%lld%lld%lld", &(x), &(y), &(z)) #define SCANLLD4(x, y, z, w) scanf("%lld%lld%lld%lld", &(x), &(y), &(z), &(w)) #define PRINTD(x) printf("%d\n", (x)) #define PRINTLLD(x) printf("%lld\n", (x)) typedef long long int lli; using namespace std; int ctoi(char c) { if (c >= '0' and c <= '9') { return (int)(c - '0'); } return -1; } int alphabet_pos(char c) { if (c >= 'a' and c <= 'z') { return (int)(c - 'a'); } return -1; } int alphabet_pos_capital(char c) { if (c >= 'A' and c <= 'Z') { return (int)(c - 'A'); } return -1; } vector<string> split(string str, char ch) { int first = 0; int last = str.find_first_of(ch); if (last == string::npos) { last = SIZ(str); } vector<string> result; while (first < SIZ(str)) { string Ssubstr(str, first, last - first); result.push_back(Ssubstr); first = last + 1; last = str.find_first_of(ch, first); if (last == string::npos) { last = SIZ(str); } } return result; } /*--------------- the end of the template --------------------*/ int cnt = 0; void insertionSort(vector<int> &A, int n, int g) { FOR(i, g, n) { int val = A[i]; int j = i - g; while (j >= 0 and A[j] > val) { A[j + g] = A[j]; j -= g; ++cnt; } A[j + g] = val; } } void shellSort(vector<int> &A, int n) { vector<int> G; int k = 1; while (k <= n) { G.push_back(k); k = k * ((int)log(k + 2) + 2) + 1; } PRINTD(SIZ(G)); REP_R(i, SIZ(G)) { if (i != 0) { printf("%d ", G[i]); } else { printf("%d\n", G[i]); } insertionSort(A, n, G[i]); } PRINTD(cnt); } int main() { // IOS; /* making cin faster */ int n; SCAND(n); vector<int> A(n); REP(i, n) { SCAND(A[i]); } shellSort(A, n); REP(i, n) { PRINTD(A[i]); } }
replace
199
200
199
200
MLE
p02262
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <vector> unsigned insertion_sort(unsigned a[], unsigned n, unsigned gap = 1) { unsigned count = 0; for (unsigned i = gap; i < n; i++) { unsigned v = a[i]; int j = i - gap; while (j >= 0 && a[j] > v) { a[j + gap] = a[j]; j -= gap; count++; } a[j + gap] = v; } return count; } std::vector<unsigned> make_gap_table(unsigned n) { std::vector<unsigned> gaps; unsigned h = 1; unsigned i = 1; while (h <= n) { gaps.push_back(h); h = pow(4, i) + 3 * pow(2, i - 1) + 1; i++; } return gaps; } void shell_sort(unsigned a[], unsigned n) { auto gaps = make_gap_table(n); unsigned count = 0; for (int i = gaps.size() - 1; i >= 0; i--) { std::clog << gaps[i] << '\n'; count += insertion_sort(a, n, gaps[i]); } std::cout << gaps.size() << '\n'; for (int i = gaps.size() - 1; i; i--) { std::cout << gaps[i] << ' '; } std::cout << gaps[0] << '\n'; std::cout << count << '\n'; } int main() { std::ios::sync_with_stdio(false); unsigned n, buf; std::cin >> n; std::vector<unsigned> a(n); for (unsigned i = 0; i < n; i++) { std::cin >> buf; a[i] = buf; } shell_sort(a.data(), a.size()); for (auto &v : a) { std::cout << v << '\n'; } }
#include <algorithm> #include <cmath> #include <iostream> #include <vector> unsigned insertion_sort(unsigned a[], unsigned n, unsigned gap = 1) { unsigned count = 0; for (unsigned i = gap; i < n; i++) { unsigned v = a[i]; int j = i - gap; while (j >= 0 && a[j] > v) { a[j + gap] = a[j]; j -= gap; count++; } a[j + gap] = v; } return count; } std::vector<unsigned> make_gap_table(unsigned n) { std::vector<unsigned> gaps; unsigned h = 1; unsigned i = 1; while (h <= n) { gaps.push_back(h); h = pow(4, i) + 3 * pow(2, i - 1) + 1; i++; } return gaps; } void shell_sort(unsigned a[], unsigned n) { auto gaps = make_gap_table(n); unsigned count = 0; for (int i = gaps.size() - 1; i >= 0; i--) { count += insertion_sort(a, n, gaps[i]); } std::cout << gaps.size() << '\n'; for (int i = gaps.size() - 1; i; i--) { std::cout << gaps[i] << ' '; } std::cout << gaps[0] << '\n'; std::cout << count << '\n'; } int main() { std::ios::sync_with_stdio(false); unsigned n, buf; std::cin >> n; std::vector<unsigned> a(n); for (unsigned i = 0; i < n; i++) { std::cin >> buf; a[i] = buf; } shell_sort(a.data(), a.size()); for (auto &v : a) { std::cout << v << '\n'; } }
delete
36
37
36
36
0
1
p02262
C++
Runtime Error
#ifndef _GLIBCXX_NO_ASSERT #include <cassert> #endif #include <cctype> #include <cerrno> #include <cfloat> #include <ciso646> #include <climits> #include <clocale> #include <cmath> #include <csetjmp> #include <csignal> #include <cstdarg> #include <cstddef> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #if __cplusplus >= 201103L #include <ccomplex> #include <cfenv> #include <cinttypes> #include <cstdalign> #include <cstdbool> #include <cstdint> #include <ctgmath> #include <cwchar> #include <cwctype> #endif #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector> #if __cplusplus >= 201103L #include <array> #include <atomic> #include <chrono> #include <condition_variable> #include <forward_list> #include <future> #include <initializer_list> #include <mutex> #include <random> #include <ratio> #include <regex> #include <scoped_allocator> #include <system_error> #include <thread> #include <tuple> #include <type_traits> #include <typeindex> #include <unordered_map> #include <unordered_set> #endif #define y0 qvya13579 #define y1 qvyb24680 #define j0 qvja13579 #define j1 qvjb24680 #define next qvne13579xt #define prev qvpr13579ev #define INF 1000000007 #define PI acos(-1.0) #define endl "\n" #define IOS \ cin.tie(0); \ ios::sync_with_stdio(false) #define M_P make_pair #define P_B push_back #define U_B upper_bound #define L_B lower_bound #define P_Q priority_queue #define FIR first #define SEC second #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define REP_R(i, n) for (int i = ((int)(n)-1); i >= 0; --i) #define FOR(i, m, n) for (int i = ((int)(m)); i < (int)(n); ++i) #define FOR_R(i, m, n) for (int i = ((int)(m)-1); i >= (int)(n); --i) #define ALL(v) (v).begin(), (v).end() #define RALL(v) (v).rbegin(), (v).rend() #define SIZ(x) ((int)(x).size()) #define COUT(x) cout << (x) << endl #define CIN(x) cin >> (x) #define CIN2(x, y) cin >> (x) >> (y) #define CIN3(x, y, z) cin >> (x) >> (y) >> (z) #define CIN4(x, y, z, w) cin >> (x) >> (y) >> (z) >> (w) #define SCAND(x) scanf("%d", &(x)) #define SCAND2(x, y) scanf("%d%d", &(x), &(y)) #define SCAND3(x, y, z) scanf("%d%d%d", &(x), &(y), &(z)) #define SCAND4(x, y, z, w) scanf("%d%d%d%d", &(x), &(y), &(z), &(w)) #define SCANLLD(x) scanf("%lld", &(x)) #define SCANLLD2(x, y) scanf("%lld%lld", &(x), &(y)) #define SCANLLD3(x, y, z) scanf("%lld%lld%lld", &(x), &(y), &(z)) #define SCANLLD4(x, y, z, w) scanf("%lld%lld%lld%lld", &(x), &(y), &(z), &(w)) #define PRINTD(x) printf("%d\n", (x)) #define PRINTLLD(x) printf("%lld\n", (x)) typedef long long int lli; using namespace std; int ctoi(char c) { if (c >= '0' and c <= '9') { return (int)(c - '0'); } return -1; } int alphabet_pos(char c) { if (c >= 'a' and c <= 'z') { return (int)(c - 'a'); } return -1; } int alphabet_pos_capital(char c) { if (c >= 'A' and c <= 'Z') { return (int)(c - 'A'); } return -1; } vector<string> split(string str, char ch) { int first = 0; int last = str.find_first_of(ch); if (last == string::npos) { last = SIZ(str); } vector<string> result; while (first < SIZ(str)) { string Ssubstr(str, first, last - first); result.push_back(Ssubstr); first = last + 1; last = str.find_first_of(ch, first); if (last == string::npos) { last = SIZ(str); } } return result; } /*--------------- the end of the template --------------------*/ int cnt = 0; void insertionSort(vector<int> &A, int n, int g) { FOR(i, g, n) { int val = A[i]; int j = i - g; while (j >= 0 and A[j] > val) { A[j + g] = A[j]; j -= g; ++cnt; } A[j + g] = val; } } void shellSort(vector<int> &A, int n) { vector<int> G; int k = 0; while (k * k + 1 < n) { G.push_back(k * k + 1); } PRINTD(SIZ(G)); REP_R(i, SIZ(G)) { if (i != 0) { printf("%d ", G[i]); } else { printf("%d\n", G[i]); } insertionSort(A, n, G[i]); } PRINTD(cnt); } int main() { // IOS; /* making cin faster */ int n; SCAND(n); vector<int> A(n); REP(i, n) { SCAND(A[i]); } shellSort(A, n); REP(i, n) { PRINTD(A[i]); } }
#ifndef _GLIBCXX_NO_ASSERT #include <cassert> #endif #include <cctype> #include <cerrno> #include <cfloat> #include <ciso646> #include <climits> #include <clocale> #include <cmath> #include <csetjmp> #include <csignal> #include <cstdarg> #include <cstddef> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #if __cplusplus >= 201103L #include <ccomplex> #include <cfenv> #include <cinttypes> #include <cstdalign> #include <cstdbool> #include <cstdint> #include <ctgmath> #include <cwchar> #include <cwctype> #endif #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector> #if __cplusplus >= 201103L #include <array> #include <atomic> #include <chrono> #include <condition_variable> #include <forward_list> #include <future> #include <initializer_list> #include <mutex> #include <random> #include <ratio> #include <regex> #include <scoped_allocator> #include <system_error> #include <thread> #include <tuple> #include <type_traits> #include <typeindex> #include <unordered_map> #include <unordered_set> #endif #define y0 qvya13579 #define y1 qvyb24680 #define j0 qvja13579 #define j1 qvjb24680 #define next qvne13579xt #define prev qvpr13579ev #define INF 1000000007 #define PI acos(-1.0) #define endl "\n" #define IOS \ cin.tie(0); \ ios::sync_with_stdio(false) #define M_P make_pair #define P_B push_back #define U_B upper_bound #define L_B lower_bound #define P_Q priority_queue #define FIR first #define SEC second #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define REP_R(i, n) for (int i = ((int)(n)-1); i >= 0; --i) #define FOR(i, m, n) for (int i = ((int)(m)); i < (int)(n); ++i) #define FOR_R(i, m, n) for (int i = ((int)(m)-1); i >= (int)(n); --i) #define ALL(v) (v).begin(), (v).end() #define RALL(v) (v).rbegin(), (v).rend() #define SIZ(x) ((int)(x).size()) #define COUT(x) cout << (x) << endl #define CIN(x) cin >> (x) #define CIN2(x, y) cin >> (x) >> (y) #define CIN3(x, y, z) cin >> (x) >> (y) >> (z) #define CIN4(x, y, z, w) cin >> (x) >> (y) >> (z) >> (w) #define SCAND(x) scanf("%d", &(x)) #define SCAND2(x, y) scanf("%d%d", &(x), &(y)) #define SCAND3(x, y, z) scanf("%d%d%d", &(x), &(y), &(z)) #define SCAND4(x, y, z, w) scanf("%d%d%d%d", &(x), &(y), &(z), &(w)) #define SCANLLD(x) scanf("%lld", &(x)) #define SCANLLD2(x, y) scanf("%lld%lld", &(x), &(y)) #define SCANLLD3(x, y, z) scanf("%lld%lld%lld", &(x), &(y), &(z)) #define SCANLLD4(x, y, z, w) scanf("%lld%lld%lld%lld", &(x), &(y), &(z), &(w)) #define PRINTD(x) printf("%d\n", (x)) #define PRINTLLD(x) printf("%lld\n", (x)) typedef long long int lli; using namespace std; int ctoi(char c) { if (c >= '0' and c <= '9') { return (int)(c - '0'); } return -1; } int alphabet_pos(char c) { if (c >= 'a' and c <= 'z') { return (int)(c - 'a'); } return -1; } int alphabet_pos_capital(char c) { if (c >= 'A' and c <= 'Z') { return (int)(c - 'A'); } return -1; } vector<string> split(string str, char ch) { int first = 0; int last = str.find_first_of(ch); if (last == string::npos) { last = SIZ(str); } vector<string> result; while (first < SIZ(str)) { string Ssubstr(str, first, last - first); result.push_back(Ssubstr); first = last + 1; last = str.find_first_of(ch, first); if (last == string::npos) { last = SIZ(str); } } return result; } /*--------------- the end of the template --------------------*/ int cnt = 0; void insertionSort(vector<int> &A, int n, int g) { FOR(i, g, n) { int val = A[i]; int j = i - g; while (j >= 0 and A[j] > val) { A[j + g] = A[j]; j -= g; ++cnt; } A[j + g] = val; } } void shellSort(vector<int> &A, int n) { vector<int> G; int k = 1; while (k <= n) { G.push_back(k); k = 3 * k + 1; } PRINTD(SIZ(G)); REP_R(i, SIZ(G)) { if (i != 0) { printf("%d ", G[i]); } else { printf("%d\n", G[i]); } insertionSort(A, n, G[i]); } PRINTD(cnt); } int main() { // IOS; /* making cin faster */ int n; SCAND(n); vector<int> A(n); REP(i, n) { SCAND(A[i]); } shellSort(A, n); REP(i, n) { PRINTD(A[i]); } }
replace
195
199
195
200
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p02262
C++
Time Limit Exceeded
#include <iostream> unsigned gaps[] = {1, 8, 23, 77, 281, 1073, 4193, 16577, 65921, 262913}; // generated from pow(4,i)+3*pow(2,i-1)+1 (prefixed with 1) unsigned insertion_sort(unsigned a[], unsigned n, unsigned gap) { unsigned count = 0; for (unsigned i = gap; i < n; i++) { unsigned v = a[i]; int j; while ((j = i - gap) >= 0 && a[j] > v) { a[j + gap] = a[j]; j -= gap; count++; } a[j + gap] = v; } return count; } void shell_sort(unsigned a[], unsigned n) { unsigned gap_num = 0; while (gaps[++gap_num] < n) ; gap_num--; unsigned count = 0; for (int i = gap_num; i >= 0; i--) count += insertion_sort(a, n, gaps[i]); std::cout << (gap_num + 1) << '\n'; for (int i = gap_num; i; i--) std::cout << gaps[i] << ' '; std::cout << gaps[0] << '\n'; std::cout << count << '\n'; } int main() { std::ios::sync_with_stdio(false); unsigned n; std::cin >> n; auto a = new unsigned[n]; for (unsigned i = 0; i < n; i++) std::cin >> a[i]; shell_sort(a, n); for (unsigned i = 0; i < n; i++) std::cout << a[i] << '\n'; delete[] (a); }
#include <iostream> unsigned gaps[] = {1, 8, 23, 77, 281, 1073, 4193, 16577, 65921, 262913}; // generated from pow(4,i)+3*pow(2,i-1)+1 (prefixed with 1) unsigned insertion_sort(unsigned a[], unsigned n, unsigned gap) { unsigned count = 0; for (unsigned i = gap; i < n; i++) { unsigned v = a[i]; int j = i - gap; while (j >= 0 && a[j] > v) { a[j + gap] = a[j]; j -= gap; count++; } a[j + gap] = v; } return count; } void shell_sort(unsigned a[], unsigned n) { unsigned gap_num = 0; while (gaps[++gap_num] < n) ; gap_num--; unsigned count = 0; for (int i = gap_num; i >= 0; i--) count += insertion_sort(a, n, gaps[i]); std::cout << (gap_num + 1) << '\n'; for (int i = gap_num; i; i--) std::cout << gaps[i] << ' '; std::cout << gaps[0] << '\n'; std::cout << count << '\n'; } int main() { std::ios::sync_with_stdio(false); unsigned n; std::cin >> n; auto a = new unsigned[n]; for (unsigned i = 0; i < n; i++) std::cin >> a[i]; shell_sort(a, n); for (unsigned i = 0; i < n; i++) std::cout << a[i] << '\n'; delete[] (a); }
replace
9
11
9
11
TLE
p02262
C++
Runtime Error
#include <iostream> using std::cin; using std::cout; unsigned gaps[] = {1, 8, 23, 77, 281, 1073, 4193, 16577, 65921, 262913, 1050113}; // generated from pow(4,i)+3*pow(2,i-1)+1 (prefixed with 1) unsigned insertion_sort(unsigned a[], unsigned n, unsigned gap) { unsigned count = 0; for (unsigned i = gap; i < n; i++) { unsigned v = a[i]; int j = i - gap; while (j >= 0 && a[j] > v) { a[j + gap] = a[j]; j -= gap; count++; } a[j + gap] = v; } return count; } void shell_sort(unsigned a[], unsigned n) { int i; unsigned gap_num = 11; while (gaps[--gap_num] >= n) ; unsigned count = 0; for (i = gap_num; i >= 0; i--) count += insertion_sort(a, n, gaps[i]); cout << (gap_num + 1) << '\n'; for (i = gap_num; i; i--) cout << gaps[i] << ' '; cout << gaps[0] << '\n'; cout << count << '\n'; } int main() { std::ios::sync_with_stdio(false); unsigned n, i; cin >> n; auto a = new unsigned[n]; for (i = 0; i < n; i++) cin >> a[i]; shell_sort(a, n); for (i = 0; i < n; i++) cout << a[i] << '\n'; delete[] (a); }
#include <iostream> using std::cin; using std::cout; unsigned gaps[] = {1, 8, 23, 77, 281, 1073, 4193, 16577, 65921, 262913, 1050113}; // generated from pow(4,i)+3*pow(2,i-1)+1 (prefixed with 1) unsigned insertion_sort(unsigned a[], unsigned n, unsigned gap) { unsigned count = 0; for (unsigned i = gap; i < n; i++) { unsigned v = a[i]; int j = i - gap; while (j >= 0 && a[j] > v) { a[j + gap] = a[j]; j -= gap; count++; } a[j + gap] = v; } return count; } void shell_sort(unsigned a[], unsigned n) { int i; unsigned gap_num = 11; while (gaps[--gap_num] > n) ; unsigned count = 0; for (i = gap_num; i >= 0; i--) count += insertion_sort(a, n, gaps[i]); cout << (gap_num + 1) << '\n'; for (i = gap_num; i; i--) cout << gaps[i] << ' '; cout << gaps[0] << '\n'; cout << count << '\n'; } int main() { std::ios::sync_with_stdio(false); unsigned n, i; cin >> n; auto a = new unsigned[n]; for (i = 0; i < n; i++) cin >> a[i]; shell_sort(a, n); for (i = 0; i < n; i++) cout << a[i] << '\n'; delete[] (a); }
replace
26
27
26
27
0
p02262
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstdlib> #include <iostream> #include <vector> using namespace std; #define maxn 100000 int a[maxn]; int n; // size of the array vector<int> G; int cnt = 0; void insertionSort(int g) { for (int i = g; i < n; i++) { int key = a[i]; int j = i - g; while (j >= 0 && a[j] > key) { a[j + g] = a[j]; j = j - g; cnt++; } a[j + g] = key; } } void makeShellArray() { int num = 1; for (num;;) { if (num > n) break; G.push_back(num); num = 3 * num + 1; } } void shellSort() { int m = G.size(); for (int i = m - 1; i >= 0; i--) insertionSort(G[i]); } void print() { int m = G.size(); cout << m << endl; for (int i = m - 1; i >= 0; i--) { if (i != 0) cout << G[i] << " "; else cout << G[i] << endl; } cout << cnt << endl; for (int i = 0; i < n; i++) cout << a[i] << endl; } int main() { while (cin >> n) { for (int i = 0; i < n; i++) cin >> a[i]; makeShellArray(); shellSort(); print(); } return 0; }
#include <algorithm> #include <cstdio> #include <cstdlib> #include <iostream> #include <vector> using namespace std; #define maxn 1000005 int a[maxn]; int n; // size of the array vector<int> G; int cnt = 0; void insertionSort(int g) { for (int i = g; i < n; i++) { int key = a[i]; int j = i - g; while (j >= 0 && a[j] > key) { a[j + g] = a[j]; j = j - g; cnt++; } a[j + g] = key; } } void makeShellArray() { int num = 1; for (num;;) { if (num > n) break; G.push_back(num); num = 3 * num + 1; } } void shellSort() { int m = G.size(); for (int i = m - 1; i >= 0; i--) insertionSort(G[i]); } void print() { int m = G.size(); cout << m << endl; for (int i = m - 1; i >= 0; i--) { if (i != 0) cout << G[i] << " "; else cout << G[i] << endl; } cout << cnt << endl; for (int i = 0; i < n; i++) cout << a[i] << endl; } int main() { while (cin >> n) { for (int i = 0; i < n; i++) cin >> a[i]; makeShellArray(); shellSort(); print(); } return 0; }
replace
6
7
6
7
0
p02262
C++
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; }
#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; }
replace
17
18
17
18
0
p02262
C++
Runtime Error
#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; } }
#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; } }
replace
5
7
5
7
0
p02262
C++
Runtime Error
#include <iostream> #include <vector> unsigned gaps[] = {1, 8, 23, 77, 281, 1073, 4193, 16577, 65921, 262913}; // generated from pow(4,i)+3*pow(2,i-1)+1 (prefixed with 1) unsigned insertion_sort(unsigned a[], unsigned n, unsigned gap = 1) { unsigned count = 0; for (unsigned i = gap; i < n; i++) { unsigned v = a[i]; int j = i - gap; while (j >= 0 && a[j] > v) { a[j + gap] = a[j]; j -= gap; count++; } a[j + gap] = v; } return count; } void shell_sort(unsigned a[], unsigned n) { unsigned gap_num = 0; while (gaps[++gap_num] < n) ; gap_num--; unsigned count = 0; for (int i = gap_num; i >= 0; i--) count += insertion_sort(a, n, gaps[i]); std::cout << (gap_num + 1) << '\n'; for (int i = gap_num; i; i--) std::cerr << gaps[i] << ' '; std::cerr << gaps[0] << '\n'; std::cout << count << '\n'; } int main() { std::ios::sync_with_stdio(false); unsigned n; std::cin >> n; std::vector<unsigned> a(n); for (unsigned i = 0; i < n; i++) std::cin >> a[i]; shell_sort(a.data(), n); for (auto &v : a) std::cout << v << '\n'; }
#include <iostream> #include <vector> unsigned gaps[] = {1, 8, 23, 77, 281, 1073, 4193, 16577, 65921, 262913}; // generated from pow(4,i)+3*pow(2,i-1)+1 (prefixed with 1) unsigned insertion_sort(unsigned a[], unsigned n, unsigned gap = 1) { unsigned count = 0; for (unsigned i = gap; i < n; i++) { unsigned v = a[i]; int j = i - gap; while (j >= 0 && a[j] > v) { a[j + gap] = a[j]; j -= gap; count++; } a[j + gap] = v; } return count; } void shell_sort(unsigned a[], unsigned n) { unsigned gap_num = 0; while (gaps[++gap_num] < n) ; gap_num--; unsigned count = 0; for (int i = gap_num; i >= 0; i--) count += insertion_sort(a, n, gaps[i]); std::cout << (gap_num + 1) << '\n'; for (int i = gap_num; i; i--) std::cout << gaps[i] << ' '; std::cout << gaps[0] << '\n'; std::cout << count << '\n'; } int main() { std::ios::sync_with_stdio(false); unsigned n; std::cin >> n; std::vector<unsigned> a(n); for (unsigned i = 0; i < n; i++) std::cin >> a[i]; shell_sort(a.data(), n); for (auto &v : a) std::cout << v << '\n'; }
replace
34
36
34
36
0
1
p02262
C++
Runtime Error
#include <cstdio> #include <iostream> using namespace std; int m = 0, cnt = 0, G[100]; void shellSort(int a[], int n) { int i, j; cnt = 0; G[0] = 1; while (1) { G[m + 1] = 3 * G[m] + 1; if (G[m + 1] >= n) break; else m++; } m++; for (i = m - 1; i >= 0; i--) { int k, j, tmp; for (k = G[i]; k < n; k++) { tmp = a[k]; j = k - G[i]; while (j >= 0 && a[j] > tmp) { a[j + G[i]] = a[j]; j -= G[i]; cnt++; } a[j + G[i]] = tmp; } } } int main() { int i, j, n, a[100000]; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } shellSort(a, n); printf("%d\n", m); for (i = m - 1; i >= 0; i--) { printf("%d", G[i]); if (i != 0) printf(" "); else printf("\n"); } printf("%d\n", cnt); for (i = 0; i < n; i++) { printf("%d\n", a[i]); } return 0; }
#include <cstdio> #include <iostream> using namespace std; int m = 0, cnt = 0, G[100]; void shellSort(int a[], int n) { int i, j; cnt = 0; G[0] = 1; while (1) { G[m + 1] = 3 * G[m] + 1; if (G[m + 1] >= n) break; else m++; } m++; for (i = m - 1; i >= 0; i--) { int k, j, tmp; for (k = G[i]; k < n; k++) { tmp = a[k]; j = k - G[i]; while (j >= 0 && a[j] > tmp) { a[j + G[i]] = a[j]; j -= G[i]; cnt++; } a[j + G[i]] = tmp; } } } int main() { int i, j, n, a[1000000]; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } shellSort(a, n); printf("%d\n", m); for (i = m - 1; i >= 0; i--) { printf("%d", G[i]); if (i != 0) printf(" "); else printf("\n"); } printf("%d\n", cnt); for (i = 0; i < n; i++) { printf("%d\n", a[i]); } return 0; }
replace
34
35
34
35
0
p02262
C++
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; }
#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; }
replace
10
11
10
11
-11
p02262
C++
Runtime Error
#include <cmath> #include <iostream> #include <memory> constexpr int max_size() { return static_cast<int>(1e+6); } int insert(int *a, const int i, const int gear) { if (i < gear) { return 0; } if (a[i - gear] <= a[i]) { return 0; } std::swap(a[i - gear], a[i]); return 1 + insert(a, i - gear, gear); } int insertion_sort(int *arr, const int n, const int gear) { int insert_count = 0; for (int i = 0; i < n; i += gear) { insert_count += insert(arr, i, gear); } return insert_count; } int pow(const int a, const int x) { return x == 0 ? 1 : a * pow(a, x - 1); } void print(int *arr, const int n, const int i, char const delim) { if (i == n - 1) { std::cout << arr[i]; return; } std::cout << arr[i] << delim; print(arr, n, i + 1, delim); } void print(int *arr, const int n, char const delim) { print(arr, n, 0, delim); } void shell_sort(int *arr, const int n) { static const double log3 = std::log(3); const int gear_index = static_cast<int>(std::log(2 * n + 1) / log3 - 1); int gear = (pow(3, gear_index + 1) - 1) >> 1; int gear_cache[100]; int cache_idx = 0; int insert_count = 0; for (; gear > 0; gear = (gear - 1) / 3) { gear_cache[cache_idx] = gear; for (int i = 0; i < gear; ++i) { insert_count += insertion_sort(arr + i, n - i, gear); } ++cache_idx; } std::cout << cache_idx << std::endl; print(gear_cache, cache_idx, ' '); std::cout << std::endl; std::cout << insert_count << std::endl; return; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int n; std::cin >> n; constexpr int N = max_size(); const auto r = std::make_unique<int[]>(N); for (int i = 0; i < n; ++i) { std::cin >> r[i]; } shell_sort(r.get(), n); print(r.get(), n, '\n'); std::cout << std::endl; return 0; }
#include <cmath> #include <iostream> #include <memory> constexpr int max_size() { return static_cast<int>(1e+6); } int insert(int *a, const int i, const int gear) { if (i < gear) { return 0; } if (a[i - gear] <= a[i]) { return 0; } std::swap(a[i - gear], a[i]); return 1 + insert(a, i - gear, gear); } int insertion_sort(int *arr, const int n, const int gear) { int insert_count = 0; for (int i = 0; i < n; i += gear) { insert_count += insert(arr, i, gear); } return insert_count; } int pow(const int a, const int x) { return x == 0 ? 1 : a * pow(a, x - 1); } void print(int *arr, const int n, const int i, char const delim) { if (i == n - 1) { std::cout << arr[i]; return; } std::cout << arr[i] << delim; print(arr, n, i + 1, delim); } void print(int *arr, const int n, char const delim) { int i = 0; for (; i < n - 1; ++i) { std::cout << arr[i] << delim; } std::cout << arr[i]; } void shell_sort(int *arr, const int n) { static const double log3 = std::log(3); const int gear_index = static_cast<int>(std::log(2 * n + 1) / log3 - 1); int gear = (pow(3, gear_index + 1) - 1) >> 1; int gear_cache[100]; int cache_idx = 0; int insert_count = 0; for (; gear > 0; gear = (gear - 1) / 3) { gear_cache[cache_idx] = gear; for (int i = 0; i < gear; ++i) { insert_count += insertion_sort(arr + i, n - i, gear); } ++cache_idx; } std::cout << cache_idx << std::endl; print(gear_cache, cache_idx, ' '); std::cout << std::endl; std::cout << insert_count << std::endl; return; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int n; std::cin >> n; constexpr int N = max_size(); const auto r = std::make_unique<int[]>(N); for (int i = 0; i < n; ++i) { std::cin >> r[i]; } shell_sort(r.get(), n); print(r.get(), n, '\n'); std::cout << std::endl; return 0; }
replace
39
40
39
46
0
p02262
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int maxn = 105; int s[maxn], n, cnt; vector<int> v; void insertionsort(int g) { for (int i = g; i < n; i++) { int v = s[i], j = i - g; while (j >= 0 && s[j] > v) { s[j + g] = s[j]; j -= g; cnt++; } s[j + g] = v; } } void shellsort() { for (int h = 1;;) { if (h > n) { break; } v.push_back(h); h = h * 3 + 1; } int len = v.size() - 1; for (int i = len; i >= 0; i--) { insertionsort(v[i]); } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &s[i]); } shellsort(); cout << v.size() << endl; for (int i = v.size() - 1; i >= 0; i--) { printf("%d", v[i]); if (i) { printf(" "); } } printf("\n"); printf("%d\n", cnt); for (int i = 0; i < n; i++) { printf("%d\n", s[i]); } return 0; } /* 5 5 1 4 3 2 */
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 5; int s[maxn], n, cnt; vector<int> v; void insertionsort(int g) { for (int i = g; i < n; i++) { int v = s[i], j = i - g; while (j >= 0 && s[j] > v) { s[j + g] = s[j]; j -= g; cnt++; } s[j + g] = v; } } void shellsort() { for (int h = 1;;) { if (h > n) { break; } v.push_back(h); h = h * 3 + 1; } int len = v.size() - 1; for (int i = len; i >= 0; i--) { insertionsort(v[i]); } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &s[i]); } shellsort(); cout << v.size() << endl; for (int i = v.size() - 1; i >= 0; i--) { printf("%d", v[i]); if (i) { printf(" "); } } printf("\n"); printf("%d\n", cnt); for (int i = 0; i < n; i++) { printf("%d\n", s[i]); } return 0; } /* 5 5 1 4 3 2 */
replace
5
6
5
6
0
p02262
C++
Runtime Error
#include <iostream> #include <math.h> using namespace std; int insertionSort(int, int, int); void shellSort(int *, int); int cnt = 0; int main(void) { int A[100], N; cin >> N; for (int i = 0; i < N; i++) { cin >> A[i]; } shellSort(A, N); } int insertionSort(int A[], int n, int g) { int v, j, cnt = 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; cnt++; } A[j + g] = v; } return (cnt); } void shellSort(int A[], int n) { int cnt = 0; int m = 0; int G[13] = {}; do { if (m == 0) { G[0] = n - 1; } else { G[m] = G[m - 1] / 2; } m++; } while (G[m - 1] > 1); cout << m << endl; for (int i = 0; i < m; i++) { cnt += insertionSort(A, n, G[i]); cout << G[i] << " "; } cout << endl << cnt << endl; for (int i = 0; i < n; i++) { cout << A[i] << endl; } }
#include <iostream> #include <math.h> using namespace std; int insertionSort(int, int, int); void shellSort(int *, int); int cnt = 0; int main(void) { int A[1000000], N; cin >> N; for (int i = 0; i < N; i++) { cin >> A[i]; } shellSort(A, N); } int insertionSort(int A[], int n, int g) { int v, j, cnt = 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; cnt++; } A[j + g] = v; } return (cnt); } void shellSort(int A[], int n) { int cnt = 0; int m = 0; int G[13] = {}; do { if (m == 0) { G[0] = n - 1; } else { G[m] = G[m - 1] / 2; } m++; } while (G[m - 1] > 1); cout << m << endl; for (int i = 0; i < m; i++) { cnt += insertionSort(A, n, G[i]); cout << G[i] << " "; } cout << endl << cnt << endl; for (int i = 0; i < n; i++) { cout << A[i] << endl; } }
replace
7
8
7
8
0
p02262
C++
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; }
/** * 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; }
replace
149
150
149
150
0
p02262
C++
Time Limit Exceeded
#include <iostream> #include <string> #include <vector> using namespace std; //?????§????????????=?????\??????????????????????´???¨??????????????? void InsertionSort(vector<long> &A, long n, long g, long &cnt) { long i, v, j; // g????????????????????????,n???????´???° // for (i = g; i < n; ++i) { v = A[i]; // v = A[3]; j = i - g; // j=3-2=1; while (j >= 0 && A[j] > v) { A[j + g] = A[j]; //????????????A[3]???A[1]?????£??\ j = j - g;// ++cnt; } A[j + g] = v; } } int main() { int m; long n, i, j; long cnt = 0; long G[13] = {1, 4, 13, 40, 121, 364, 1093, 3280, 9841, 29524, 88573, 265720, 797161}; cin >> n; vector<long> A; A.resize(n, 0); for (i = 0; i < n; i++) { cin >> A[i]; } for (i = 0; i < 12; i++) { if ((A.size() > G[i] && A.size() < G[i + 1]) || i == 12) { j = i; break; } } cout << j + 1 << endl; for (i = j; i >= 0; --i) { cout << G[i]; if (i != 0) cout << " "; InsertionSort(A, n, G[i], cnt); } cout << endl; cout << cnt << endl; for (i = 0; i < n; i++) { cout << A[i] << endl; } return 0; }
#include <iostream> #include <string> #include <vector> using namespace std; //?????§????????????=?????\??????????????????????´???¨??????????????? void InsertionSort(vector<long> &A, long n, long g, long &cnt) { long i, v, j; // g????????????????????????,n???????´???° // for (i = g; i < n; ++i) { v = A[i]; // v = A[3]; j = i - g; // j=3-2=1; while (j >= 0 && A[j] > v) { A[j + g] = A[j]; //????????????A[3]???A[1]?????£??\ j = j - g;// ++cnt; } A[j + g] = v; } } int main() { int m; long n, i, j; long cnt = 0; long G[13] = {1, 4, 13, 40, 121, 364, 1093, 3280, 9841, 29524, 88573, 265720, 797161}; cin >> n; vector<long> A; A.resize(n, 0); for (i = 0; i < n; i++) { cin >> A[i]; } for (i = 0; i < 13; i++) { if ((A.size() >= G[i] && A.size() < G[i + 1]) || i == 12) { j = i; break; } } cout << j + 1 << endl; for (i = j; i >= 0; --i) { cout << G[i]; if (i != 0) cout << " "; InsertionSort(A, n, G[i], cnt); } cout << endl; cout << cnt << endl; for (i = 0; i < n; i++) { cout << A[i] << endl; } return 0; }
replace
41
43
41
43
TLE
p02262
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) #define rrep(i, n) for (int i = n - 1; i >= 0; i--) using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pi; typedef long double lb; const int INF = 1 << 27; const int MOD = (int)1e9 + 7; int cnt; void read(int *n, int a[]) { cin >> *n; rep(i, *n) cin >> a[i]; return; } void insertion_sort(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; } return; } void shell_sort(int a[], int n) { cnt = 0; int m = 0; int g[100]; int k = n; g[0] = n - 1; for (m = 0; k > 0; k /= 2) m++; for (int i = 1; i < m; i++) g[i] = g[i - 1] / 2; cout << m << endl; rep(i, m) cout << g[i] << ' '; cout << endl; rep(i, m) insertion_sort(a, n, g[i]); return; } void print_array(int a[], int n) { rep(i, n) cout << a[i] << endl; } int main(void) { int n; int a[10000]; read(&n, a); shell_sort(a, n); cout << cnt << endl; print_array(a, n); return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) #define rrep(i, n) for (int i = n - 1; i >= 0; i--) using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pi; typedef long double lb; const int INF = 1 << 27; const int MOD = (int)1e9 + 7; int cnt; void read(int *n, int a[]) { cin >> *n; rep(i, *n) cin >> a[i]; return; } void insertion_sort(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; } return; } void shell_sort(int a[], int n) { cnt = 0; int m = 0; int g[100]; int k = n; g[0] = n - 1; for (m = 0; k > 0; k /= 2) m++; for (int i = 1; i < m; i++) g[i] = g[i - 1] / 2; cout << m << endl; rep(i, m) cout << g[i] << ' '; cout << endl; rep(i, m) insertion_sort(a, n, g[i]); return; } void print_array(int a[], int n) { rep(i, n) cout << a[i] << endl; } int main(void) { int n; int a[1000010]; read(&n, a); shell_sort(a, n); cout << cnt << endl; print_array(a, n); return 0; }
replace
58
59
58
59
0
p02262
C++
Runtime Error
#include <cstdio> int G[101]; int A[1000002]; 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 -= g; cnt++; } A[j + g] = v; } return cnt; } int shell_sort(int *A, int n) { int cnt = 0; G[0] = 1; int m = 0; while (G[m] + 3 < n) { G[m + 1] = G[m] + 3; m++; } m++; printf("%d\n", m); for (int i = m - 1; i >= 0; --i) { if (i < m - 1) putchar(' '); printf("%d", G[i]); cnt += insertion_sort(A, n, G[i]); } printf("\n%d\n", cnt); for (int i = 0; i < n; ++i) printf("%d\n", A[i]); } int main(int argc, char const *argv[]) { int N; scanf("%d", &N); for (int i = 0; i < N; ++i) scanf("%d", &A[i]); shell_sort(A, N); return 0; }
#include <cstdio> int G[101]; int A[1000002]; 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 -= g; cnt++; } A[j + g] = v; } return cnt; } int shell_sort(int *A, int n) { int cnt = 0; G[0] = 1; int m = 0; do { G[m + 1] = G[m] * 3 + 1; } while (G[++m] < n); printf("%d\n", m); for (int i = m - 1; i >= 0; --i) { if (i < m - 1) putchar(' '); printf("%d", G[i]); cnt += insertion_sort(A, n, G[i]); } printf("\n%d\n", cnt); for (int i = 0; i < n; ++i) printf("%d\n", A[i]); } int main(int argc, char const *argv[]) { int N; scanf("%d", &N); for (int i = 0; i < N; ++i) scanf("%d", &A[i]); shell_sort(A, N); return 0; }
replace
24
29
24
27
0
p02262
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; int cnt = 0; void insertionSort(vector<int> &A, int g) { for (int i = g; i < A.size(); ++i) { if (A[i - g] > A[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 solve() { int N; cin >> N; vector<int> A(N); for (int i = 0; i < N; ++i) { cin >> A[i]; } cnt = 0; vector<int> G; int h = 1; while (N > h) { G.push_back(h); h = 3 * h + 1; } for (int i = G.size() - 1; i >= 0; --i) { insertionSort(A, G[i]); } 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 < A.size(); ++i) { cout << A[i] << endl; } } int main() { solve(); return (0); }
#include <iostream> #include <vector> using namespace std; int cnt = 0; void insertionSort(vector<int> &A, int g) { for (int i = g; i < A.size(); ++i) { if (A[i - g] > A[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 solve() { int N; cin >> N; vector<int> A(N); for (int i = 0; i < N; ++i) { cin >> A[i]; } cnt = 0; vector<int> G; int h = 1; while (N >= h) { G.push_back(h); h = 3 * h + 1; } for (int i = G.size() - 1; i >= 0; --i) { insertionSort(A, G[i]); } 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 < A.size(); ++i) { cout << A[i] << endl; } } int main() { solve(); return (0); }
replace
31
32
31
32
0
p02262
C++
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 (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]); } }
#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]); } }
replace
39
40
39
40
0
p02262
C++
Runtime Error
#include <stdio.h> #include <string.h> struct NUM { int cnt; int m; int G[100]; }; int InsertionSort(int A[], int n, int g) { int i, v, j, cnt = 0; 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; } return cnt; } struct NUM ShellSort(int A[], int n) { struct NUM num; int cnt = 0; int m = 0; int G[100]; for (int i = n; i > 0; i = i / 2) { G[m] = i; m++; } for (int i = 0; i < m; i++) { cnt += InsertionSort(A, n, G[i]); } num.cnt = cnt; num.m = m; for (int i = 0; i < m; i++) num.G[i] = G[i]; return num; } int main() { int N, A[1000]; struct NUM num; scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%d", &A[i]); } num = ShellSort(A, N); printf("%d\n", num.m); for (int i = 0; i < num.m; i++) { if (i == num.m - 1) { printf("%d\n", num.G[i]); break; } printf("%d ", num.G[i]); } printf("%d\n", num.cnt); for (int i = 0; i < N; i++) { printf("%d\n", A[i]); } return 0; }
#include <stdio.h> #include <string.h> struct NUM { int cnt; int m; int G[100]; }; int InsertionSort(int A[], int n, int g) { int i, v, j, cnt = 0; 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; } return cnt; } struct NUM ShellSort(int A[], int n) { struct NUM num; int cnt = 0; int m = 0; int G[100]; for (int i = n; i > 0; i = i / 2) { G[m] = i; m++; } for (int i = 0; i < m; i++) { cnt += InsertionSort(A, n, G[i]); } num.cnt = cnt; num.m = m; for (int i = 0; i < m; i++) num.G[i] = G[i]; return num; } int main() { int N, A[1000000]; struct NUM num; scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%d", &A[i]); } num = ShellSort(A, N); printf("%d\n", num.m); for (int i = 0; i < num.m; i++) { if (i == num.m - 1) { printf("%d\n", num.G[i]); break; } printf("%d ", num.G[i]); } printf("%d\n", num.cnt); for (int i = 0; i < N; i++) { printf("%d\n", A[i]); } return 0; }
replace
44
45
44
45
0
p02262
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstring> #include <fstream> #include <iostream> #include <sstream> #include <string> #include <vector> using namespace std; class cShell { int m_n; int cnt; vector<int> vA; vector<int> vg; void insertionSort(int g); public: cShell(int n, vector<int> &v) { vA = v; m_n = n; cnt = 0; } void shellSort(); int getm() { return vg.size(); } string getGapToStr(); vector<int> getData() { return vA; } int getCnt() { return cnt; } }; void cShell::insertionSort(int g) { for (int i = g; i < m_n; ++i) { int v = vA[i]; int j = i - g; while (j >= 0 && vA[j] > v) { vA[j + g] = vA[j]; j = j - g; cnt++; } vA[j + g] = v; } } void cShell::shellSort() { int cnt = 0; int m = vA.size(); while (m /= 2) { vg.push_back(m); } for (int i = 0; i < vg.size(); ++i) { insertionSort(vg[i]); } } string cShell::getGapToStr() { ostringstream oss; for (int i = 0; i < vg.size() - 1; ++i) { oss << vg[i] << " "; } oss << vg[vg.size() - 1]; return oss.str(); } int main() { int n; cin >> n; cin.ignore(); vector<int> va; for (int i = 0; i < n; ++i) { int t; cin >> t; va.push_back(t); } cShell cs(n, va); cs.shellSort(); cout << cs.getm() << endl; cout << cs.getGapToStr() << endl; cout << cs.getCnt() << endl; va = cs.getData(); for (int i = 0; i < va.size(); ++i) { cout << va[i] << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstring> #include <fstream> #include <iostream> #include <sstream> #include <string> #include <vector> using namespace std; class cShell { int m_n; int cnt; vector<int> vA; vector<int> vg; void insertionSort(int g); public: cShell(int n, vector<int> &v) { vA = v; m_n = n; cnt = 0; } void shellSort(); int getm() { return vg.size(); } string getGapToStr(); vector<int> getData() { return vA; } int getCnt() { return cnt; } }; void cShell::insertionSort(int g) { for (int i = g; i < m_n; ++i) { int v = vA[i]; int j = i - g; while (j >= 0 && vA[j] > v) { vA[j + g] = vA[j]; j = j - g; cnt++; } vA[j + g] = v; } } void cShell::shellSort() { int cnt = 0; int m = vA.size(); if (m / 2 > 0) { while (m /= 2) { vg.push_back(m); } } else { vg.push_back(1); } for (int i = 0; i < vg.size(); ++i) { insertionSort(vg[i]); } } string cShell::getGapToStr() { ostringstream oss; for (int i = 0; i < vg.size() - 1; ++i) { oss << vg[i] << " "; } oss << vg[vg.size() - 1]; return oss.str(); } int main() { int n; cin >> n; cin.ignore(); vector<int> va; for (int i = 0; i < n; ++i) { int t; cin >> t; va.push_back(t); } cShell cs(n, va); cs.shellSort(); cout << cs.getm() << endl; cout << cs.getGapToStr() << endl; cout << cs.getCnt() << endl; va = cs.getData(); for (int i = 0; i < va.size(); ++i) { cout << va[i] << endl; } return 0; }
replace
53
55
53
59
0
p02262
C++
Runtime Error
#include <iostream> #include <ostream> #include <vector> void trace_space(std::vector<int> v) { for (std::vector<int>::iterator i = v.begin(); i != v.end(); ++i) { std::cout << *i << " "; } std::cout << std::endl; } void trace_endl(std::vector<int> v) { for (std::vector<int>::iterator i = v.begin(); i != v.end(); ++i) { std::cout << *i << std::endl; } } void insertion_sort(std::vector<int> &v, int n, int g, int &cnt) { for (int i = g; i < n; i++) { int t = v[i]; int j = i - g; while (j >= 0 && v[j] > t) { v[j + g] = v[j]; j = j - g; cnt++; } v[j + g] = t; } } void shell_sort(std::vector<int> &v, int n) { std::vector<int> G; for (int g = 1; g < n; g = 3 * g + 1) G.push_back(g); int c = 0; for (int i = 0; i < G.size(); i++) { insertion_sort(v, n, G[G.size() - i - 1], c); } // std::cout << G.size() << std::endl; for (int i = 0; i < G.size() - 1; i++) { std::cout << G[G.size() - i - 1] << " "; } std::cout << G[0] << std::endl; std::cout << c << std::endl; } int main() { int n; std::cin >> n; std::vector<int> a; a.resize(n, 0); for (int i = 0; i < n; i++) { std::cin >> a[i]; } shell_sort(a, n); trace_endl(a); return 0; }
#include <iostream> #include <ostream> #include <vector> void trace_space(std::vector<int> v) { for (std::vector<int>::iterator i = v.begin(); i != v.end(); ++i) { std::cout << *i << " "; } std::cout << std::endl; } void trace_endl(std::vector<int> v) { for (std::vector<int>::iterator i = v.begin(); i != v.end(); ++i) { std::cout << *i << std::endl; } } void insertion_sort(std::vector<int> &v, int n, int g, int &cnt) { for (int i = g; i < n; i++) { int t = v[i]; int j = i - g; while (j >= 0 && v[j] > t) { v[j + g] = v[j]; j = j - g; cnt++; } v[j + g] = t; } } void shell_sort(std::vector<int> &v, int n) { std::vector<int> G; for (int g = 1; g <= n; g = 3 * g + 1) G.push_back(g); int c = 0; for (int i = 0; i < G.size(); i++) { insertion_sort(v, n, G[G.size() - i - 1], c); } // std::cout << G.size() << std::endl; for (int i = 0; i < G.size() - 1; i++) { std::cout << G[G.size() - i - 1] << " "; } std::cout << G[0] << std::endl; std::cout << c << std::endl; } int main() { int n; std::cin >> n; std::vector<int> a; a.resize(n, 0); for (int i = 0; i < n; i++) { std::cin >> a[i]; } shell_sort(a, n); trace_endl(a); return 0; }
replace
32
33
32
33
0
p02262
C++
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; }
#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; }
replace
41
42
41
42
0
p02262
C++
Runtime Error
#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; }
#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; }
replace
5
7
5
7
-11
p02262
C++
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; }
#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; }
replace
28
29
28
29
-11
p02262
C++
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() { 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; }
#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; }
delete
40
41
40
40
0
p02262
C++
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[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]); } }
#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]); } }
replace
39
40
39
40
0
p02262
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <iostream> #include <vector> using namespace std; int cnt = 0; vector<int> G; void insertsort(int A[], int n, int g) { for (int i = g; i < n; i++) { int temp = A[i], j = i - g; while (j >= 0 && A[j] > temp) { A[j + g] = A[j]; j -= g; cnt++; } A[j + g] = temp; } return; } void shellsort(int A[], int n) { // generate the G array 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++) insertsort(A, n, G[i]); return; } int main() { int n, A[1000010]; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &A[i]); 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%d\n", cnt); for (int i = 0; i < n; i++) printf("%d\n", A[i]); return 0; }
#include <algorithm> #include <cstdio> #include <iostream> #include <vector> using namespace std; int cnt = 0; vector<int> G; void insertsort(int A[], int n, int g) { for (int i = g; i < n; i++) { int temp = A[i], j = i - g; while (j >= 0 && A[j] > temp) { A[j + g] = A[j]; j -= g; cnt++; } A[j + g] = temp; } return; } void shellsort(int A[], int n) { // generate the G array 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--) insertsort(A, n, G[i]); return; } int main() { int n, A[1000010]; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &A[i]); 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%d\n", cnt); for (int i = 0; i < n; i++) printf("%d\n", A[i]); return 0; }
replace
30
31
30
31
-11
p02262
C++
Runtime Error
#include <bits/stdc++.h> int count; 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 and A[j] > v) { A[j + G] = A[j]; j = j - G; count += 1; } A[j + G] = v; } return; } void shellSort(int A[], int N) { count = 0; int G[7]; int m = 0; int temp = N; for (int i = 0; temp > 0; ++i) { G[i] = temp; temp /= 2; m = i + 1; } for (int i = 0; i < m; ++i) { insertionSort(A, N, G[i]); } std::cout << m << std::endl; for (int i = 0; i < m - 1; ++i) { std::cout << G[i] << " "; } std::cout << G[m - 1] << std::endl; std::cout << count << std::endl; for (int i = 0; i < N; ++i) { std::cout << A[i] << std::endl; } return; } int main() { int N; int *A; A = new int[1040000]; std::cin >> N; for (int i = 0; i < N; ++i) { std::cin >> A[i]; } shellSort(A, N); delete[] A; return 0; }
#include <bits/stdc++.h> int count; 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 and A[j] > v) { A[j + G] = A[j]; j = j - G; count += 1; } A[j + G] = v; } return; } void shellSort(int A[], int N) { count = 0; int G[64]; int m = 0; int temp = N; for (int i = 0; temp > 0; ++i) { G[i] = temp; temp /= 2; m = i + 1; } for (int i = 0; i < m; ++i) { insertionSort(A, N, G[i]); } std::cout << m << std::endl; for (int i = 0; i < m - 1; ++i) { std::cout << G[i] << " "; } std::cout << G[m - 1] << std::endl; std::cout << count << std::endl; for (int i = 0; i < N; ++i) { std::cout << A[i] << std::endl; } return; } int main() { int N; int *A; A = new int[1040000]; std::cin >> N; for (int i = 0; i < N; ++i) { std::cin >> A[i]; } shellSort(A, N); delete[] A; return 0; }
replace
20
21
20
21
0
p02262
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <iostream> #include <math.h> #include <vector> #define Lens 100005 #define Nil -1 using namespace std; int N, sum; int a[Lens]; vector<int> b, G; void selectionsort(int a[], int g) { for (int i = g; i < N; i++) { int j = i - g; int mini = a[i]; while (j >= 0 && a[j] > mini) { a[j + g] = a[j]; sum++; j -= g; } a[j + g] = mini; } } void shell() { int h = 1; while (h <= N) { G.push_back(h); h = 3 * h + 1; } } int main() { cin >> N; for (int i = 0; i < N; i++) { cin >> a[i]; } shell(); sum = 0; cout << G.size() << endl; for (int i = G.size() - 1; i >= 0; i--) { cout << G[i]; if (i > 0) cout << ' '; if (i == 0) cout << endl; } for (int i = G.size() - 1; i >= 0; i--) { int g = G[i]; selectionsort(a, g); } cout << sum << endl; for (int i = 0; i < N; i++) { cout << a[i] << endl; } return 0; }
#include <algorithm> #include <cstdio> #include <iostream> #include <math.h> #include <vector> #define Lens 1000005 #define Nil -1 using namespace std; int N, sum; int a[Lens]; vector<int> b, G; void selectionsort(int a[], int g) { for (int i = g; i < N; i++) { int j = i - g; int mini = a[i]; while (j >= 0 && a[j] > mini) { a[j + g] = a[j]; sum++; j -= g; } a[j + g] = mini; } } void shell() { int h = 1; while (h <= N) { G.push_back(h); h = 3 * h + 1; } } int main() { cin >> N; for (int i = 0; i < N; i++) { cin >> a[i]; } shell(); sum = 0; cout << G.size() << endl; for (int i = G.size() - 1; i >= 0; i--) { cout << G[i]; if (i > 0) cout << ' '; if (i == 0) cout << endl; } for (int i = G.size() - 1; i >= 0; i--) { int g = G[i]; selectionsort(a, g); } cout << sum << endl; for (int i = 0; i < N; i++) { cout << a[i] << endl; } return 0; }
replace
5
6
5
6
0
p02262
C++
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); }
#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); }
replace
20
21
20
21
0
p02262
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; void printa(int A[], int len, string separator) { cout << A[0]; for (int i = 1; i < len; i++) cout << separator << A[i]; cout << endl; } int insertionSort(int A[], int len, int g) { int cnt = 0; for (int i = g; i < len; 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(int A[], int len) { int cnt = 0; vector<int> G; for (int h = 1; h < len; h = 3 * h + 1) G.insert(G.begin(), h); int m = G.size(); for (int i = 0; i < m; i++) cnt += insertionSort(A, len, G[i]); cout << m << endl; printa(G.data(), m, " "); cout << cnt << endl; printa(A, len, "\n"); } int main() { int n, *a; cin >> n; a = new int[n]; for (int i = 0; i < n; i++) cin >> a[i]; shellSort(a, n); delete[] a; return 0; }
#include <iostream> #include <vector> using namespace std; void printa(int A[], int len, string separator) { cout << A[0]; for (int i = 1; i < len; i++) cout << separator << A[i]; cout << endl; } int insertionSort(int A[], int len, int g) { int cnt = 0; for (int i = g; i < len; 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(int A[], int len) { int cnt = 0; vector<int> G; for (int h = 1; h <= len; h = 3 * h + 1) G.insert(G.begin(), h); int m = G.size(); for (int i = 0; i < m; i++) cnt += insertionSort(A, len, G[i]); cout << m << endl; printa(G.data(), m, " "); cout << cnt << endl; printa(A, len, "\n"); } int main() { int n, *a; cin >> n; a = new int[n]; for (int i = 0; i < n; i++) cin >> a[i]; shellSort(a, n); delete[] a; return 0; }
replace
30
31
30
31
0
p02262
C++
Runtime Error
#include <cmath> #include <iostream> using namespace std; void insertionSort(int *, int, int); void shellSort(int *, int); void write(int *, int, int *, int); int cnt = 0; int main() { int n, A[100010]; cin >> n; for (int i = 0; i < n; i++) { cin >> A[i]; } shellSort(A, n); } 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; int m = 0, G[110]; G[0] = 1; int copy[1000010]; for (int i = 0; i < n; i++) { copy[i] = A[i]; } do { G[0] = 3 * G[0] + 1; m++; } while (G[0] <= n / 9); G[0] /= 3; for (int i = 1; i < m; i++) { G[i] = G[i - 1] / 3; } for (int i = 0; i < m; i++) { insertionSort(A, n, G[i]); } if (cnt < pow(n, 1.5)) write(G, m, A, n); else { // cerr << "!!" << endl; cnt = 0; for (int i = m; i != -1; i--) { if (i == 0) G[0] = 3 * G[1] + 1; else G[i] = G[i - 1]; } for (int i = 0; i <= m; i++) { insertionSort(copy, n, G[i]); } write(G, m + 1, copy, n); } } void write(int *G, int m, int *A, int n) { cout << m << endl; for (int i = 0; i < m; i++) { cout << G[i]; if (i == m - 1) cout << endl; else cout << " "; } cout << cnt << endl; for (int i = 0; i < n; i++) { cout << A[i] << endl; } }
#include <cmath> #include <iostream> using namespace std; void insertionSort(int *, int, int); void shellSort(int *, int); void write(int *, int, int *, int); int cnt = 0; int main() { int n, A[1000010]; cin >> n; for (int i = 0; i < n; i++) { cin >> A[i]; } shellSort(A, n); } 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; int m = 0, G[110]; G[0] = 1; int copy[1000010]; for (int i = 0; i < n; i++) { copy[i] = A[i]; } do { G[0] = 3 * G[0] + 1; m++; } while (G[0] <= n / 9); G[0] /= 3; for (int i = 1; i < m; i++) { G[i] = G[i - 1] / 3; } for (int i = 0; i < m; i++) { insertionSort(A, n, G[i]); } if (cnt < pow(n, 1.5)) write(G, m, A, n); else { // cerr << "!!" << endl; cnt = 0; for (int i = m; i != -1; i--) { if (i == 0) G[0] = 3 * G[1] + 1; else G[i] = G[i - 1]; } for (int i = 0; i <= m; i++) { insertionSort(copy, n, G[i]); } write(G, m + 1, copy, n); } } void write(int *G, int m, int *A, int n) { cout << m << endl; for (int i = 0; i < m; i++) { cout << G[i]; if (i == m - 1) cout << endl; else cout << " "; } cout << cnt << endl; for (int i = 0; i < n; i++) { cout << A[i] << endl; } }
replace
9
10
9
10
0
p02262
C++
Runtime Error
#include <cmath> #include <iostream> #include <vector> using namespace std; int main() { int n, cnt; vector<int> nums; auto insertionSort = [&nums, &n, &cnt](int d) { for (int i = d; i < n; ++i) { int top = nums[i]; int j; for (j = i - d; i >= 0 && nums[j] > top; j -= d) { nums[j + d] = nums[j]; ++cnt; } nums[j + d] = top; } }; auto shellSort = [&nums, &n, insertionSort]() { int maxid = log(2 * n + 1) / log(3) - 1; cout << maxid + 1 << endl; for (int id = maxid; id >= 0; --id) { unsigned int d = (pow(3, id + 1) - 1) / 2; cout << d << (id == 0 ? "\n" : " "); insertionSort(d); } }; cin >> n; nums = vector<int>(n); for (int &c : nums) cin >> c; cnt = 0; shellSort(); cout << cnt << endl; for (auto &&c : nums) cout << c << endl; return 0; }
#include <cmath> #include <iostream> #include <vector> using namespace std; int main() { int n, cnt; vector<int> nums; auto insertionSort = [&nums, &n, &cnt](int d) { for (int i = d; i < n; ++i) { int top = nums[i]; int j; for (j = i - d; j >= 0 && nums[j] > top; j -= d) { nums[j + d] = nums[j]; ++cnt; } nums[j + d] = top; } }; auto shellSort = [&nums, &n, insertionSort]() { int maxid = log(2 * n + 1) / log(3) - 1; cout << maxid + 1 << endl; for (int id = maxid; id >= 0; --id) { unsigned int d = (pow(3, id + 1) - 1) / 2; cout << d << (id == 0 ? "\n" : " "); insertionSort(d); } }; cin >> n; nums = vector<int>(n); for (int &c : nums) cin >> c; cnt = 0; shellSort(); cout << cnt << endl; for (auto &&c : nums) cout << c << endl; return 0; }
replace
12
13
12
13
0
p02262
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; int m; vector<int> G; 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) { int cnt = 0; int x = 3; int y = 1; while (y <= N) { G.push_back(y); x *= 3; y = (x - 1) / 2; } reverse(G.begin(), G.end()); m = G.size(); for (int i = 0; i < m; ++i) { cnt += insertionSort(A, N, G[i]); } return cnt; } int main() { int N; cin >> N; int A[114514]; for (int i = 0; i < N; ++i) cin >> A[i]; int res = shellSort(A, N); cout << m << endl; for (int i = 0; i < m; ++i) { if (i) cout << " "; cout << G[i]; if (i == m - 1) cout << endl; } cout << res << endl; for (int i = 0; i < N; ++i) { cout << A[i] << endl; } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int m; vector<int> G; 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) { int cnt = 0; int x = 3; int y = 1; while (y <= N) { G.push_back(y); x *= 3; y = (x - 1) / 2; } reverse(G.begin(), G.end()); m = G.size(); for (int i = 0; i < m; ++i) { cnt += insertionSort(A, N, G[i]); } return cnt; } int main() { int N; cin >> N; int A[1145149]; for (int i = 0; i < N; ++i) cin >> A[i]; int res = shellSort(A, N); cout << m << endl; for (int i = 0; i < m; ++i) { if (i) cout << " "; cout << G[i]; if (i == m - 1) cout << endl; } cout << res << endl; for (int i = 0; i < N; ++i) { cout << A[i] << endl; } return 0; }
replace
39
40
39
40
0
p02262
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <list> #include <string> #include <vector> using namespace std; template <typename T> void WriteLine_Container(const T &container); void InsertionSort(vector<int> &v, int n, int g, int &cnt); void ShellSort(vector<int> &v, int n); int main() { int len; cin >> len; vector<int> A(len); for (int i = 0; i < len; i++) cin >> A[i]; ShellSort(A, A.size()); for_each(A.begin(), A.end(), [](int x) { cout << x << endl; }); return 0; } template <typename T> void WriteLine_Container(T &container) { auto it = container.begin(); while (true) { cout << *it; it++; if (it != container.end()) cout << " "; } cout << endl; } void InsertionSort(vector<int> &v, int n, int g, int &cnt) { for (int i = g; i < n; i++) { int temp = v[i]; int j = i - g; while (j >= 0 && v[j] > temp) { v[j + g] = v[j]; j = j - g; cnt++; } v[j + g] = temp; } } void ShellSort(vector<int> &v, int n) { int cnt = 0; list<int> li; li.push_front(1); for (int i = 4; i < v.size(); i = i * 3 + 1) { li.push_front(i); } int m = li.size(); cout << m << endl; WriteLine_Container(li); for_each(li.begin(), li.end(), [&](int x) { InsertionSort(v, n, x, cnt); }); cout << cnt << endl; }
#include <algorithm> #include <iostream> #include <list> #include <string> #include <vector> using namespace std; template <typename T> void WriteLine_Container(const T &container); void InsertionSort(vector<int> &v, int n, int g, int &cnt); void ShellSort(vector<int> &v, int n); int main() { int len; cin >> len; vector<int> A(len); for (int i = 0; i < len; i++) cin >> A[i]; ShellSort(A, A.size()); for_each(A.begin(), A.end(), [](int x) { cout << x << endl; }); return 0; } template <typename T> void WriteLine_Container(T &container) { auto it = container.begin(); while (true) { cout << *it; it++; if (it == container.end()) break; else cout << " "; } cout << endl; } void InsertionSort(vector<int> &v, int n, int g, int &cnt) { for (int i = g; i < n; i++) { int temp = v[i]; int j = i - g; while (j >= 0 && v[j] > temp) { v[j + g] = v[j]; j = j - g; cnt++; } v[j + g] = temp; } } void ShellSort(vector<int> &v, int n) { int cnt = 0; list<int> li; li.push_front(1); for (int i = 4; i < v.size(); i = i * 3 + 1) { li.push_front(i); } int m = li.size(); cout << m << endl; WriteLine_Container(li); for_each(li.begin(), li.end(), [&](int x) { InsertionSort(v, n, x, cnt); }); cout << cnt << endl; }
replace
32
33
32
35
TLE
p02262
C++
Runtime Error
#include <iostream> #include <utility> using namespace std; void output(int n, int elements[]) { for (int i = 0; i < n; i++) cout << elements[i] << '\n'; } int insertion_sort(int n, int elements[], int g) { int cnt = 0; for (int i = g; i < n; i++) { int key = elements[i], j = i - g; while (0 <= j && key < elements[j]) { elements[j + g] = elements[j]; j -= g; cnt++; } elements[j + g] = key; } return cnt; } void shell_sort(int n, int elements[]) { int cnt = 0, m = 0, g[13] = {1, 4, 13, 40, 121, 364, 1093, 3280, 9841, 29524, 88573, 265720, 797161}; for (; g[m] <= n; m++) ; cout << m << '\n'; for (int i = m - 1; 0 <= i; i--) { cout << g[i] << (i == 0 ? "" : " "); cnt += insertion_sort(n, elements, g[i]); } cout << '\n'; cout << cnt << '\n'; } int main() { std::ios::sync_with_stdio(false); int n; cin >> n; int *elements = new int[n]; for (int i = 0; i < n; i++) cin >> elements[i]; shell_sort(n, elements); output(n, elements); delete[] elements; return 0; }
#include <iostream> #include <utility> using namespace std; void output(int n, int elements[]) { for (int i = 0; i < n; i++) cout << elements[i] << '\n'; } int insertion_sort(int n, int elements[], int g) { int cnt = 0; for (int i = g; i < n; i++) { int key = elements[i], j = i - g; while (0 <= j && key < elements[j]) { elements[j + g] = elements[j]; j -= g; cnt++; } elements[j + g] = key; } return cnt; } void shell_sort(int n, int elements[]) { int cnt = 0, m = 0, g[13] = {1, 4, 13, 40, 121, 364, 1093, 3280, 9841, 29524, 88573, 265720, 797161}; for (; g[m] <= n && m < 13; m++) ; cout << m << '\n'; for (int i = m - 1; 0 <= i; i--) { cout << g[i] << (i == 0 ? "" : " "); cnt += insertion_sort(n, elements, g[i]); } cout << '\n'; cout << cnt << '\n'; } int main() { std::ios::sync_with_stdio(false); int n; cin >> n; int *elements = new int[n]; for (int i = 0; i < n; i++) cin >> elements[i]; shell_sort(n, elements); output(n, elements); delete[] elements; return 0; }
replace
28
29
28
29
0
p02262
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; vector<int> A; int cnt; void insertionSort(int n, int g) { int j, v; 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 n) { cnt = 0; int siz = 0; vector<int> G; while (1) { siz = siz * 3 + 1; if (siz >= n) break; G.push_back(siz); } cout << G.size() << endl; for (int i = G.size() - 1; i >= 0; i--) { insertionSort(n, G[i]); cout << G[i] << ((i != 0) ? " " : "\n"); } } int main(void) { int n, t; cin >> n; for (int i = 0; i < n; i++) { cin >> t; A.push_back(t); } shellSort(n); cout << cnt << endl; for (int i : A) { cout << i << endl; } }
#include <bits/stdc++.h> using namespace std; vector<int> A; int cnt; void insertionSort(int n, int g) { int j, v; 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 n) { cnt = 0; int siz = 0; vector<int> G; while (1) { siz = siz * 3 + 1; if (siz >= n && siz != 1) break; G.push_back(siz); } cout << G.size() << endl; for (int i = G.size() - 1; i >= 0; i--) { insertionSort(n, G[i]); cout << G[i] << ((i != 0) ? " " : "\n"); } } int main(void) { int n, t; cin >> n; for (int i = 0; i < n; i++) { cin >> t; A.push_back(t); } shellSort(n); cout << cnt << endl; for (int i : A) { cout << i << endl; } }
replace
23
24
23
24
TLE
p02263
C++
Time Limit Exceeded
#include <cstdio> #include <cstdlib> #include <iostream> using namespace std; #define MAX 100 #define max 1000000 void initialize(); int isEmpty(); int isFull(); void push(int); int pop(); int top; int S[max]; int main() { char c[max]; int sum = 0; int a, b; initialize(); while (cin >> c) { if (c[0] == '-') { a = pop(); b = pop(); push(b - a); } else if (c[0] == '+') { push(pop() + pop()); } else if (c[0] == '*') { push(pop() * pop()); } else { push(atoi(c)); } } printf("%d\n", S[1]); return 0; } void initialize() { top = 0; } int isEmpty() { if (top == 0) return 1; return 0; } int isFull() { if (top >= MAX - 1) return 1; return 0; } void push(int x) { if (isFull()) fprintf(stderr, "Overflow"); top++; S[top] = x; } int pop() { if (isEmpty()) fprintf(stderr, "Anderflow"); top--; return S[top + 1]; }
#include <cstdio> #include <cstdlib> #include <iostream> using namespace std; #define MAX 200 #define max 1000000 void initialize(); int isEmpty(); int isFull(); void push(int); int pop(); int top; int S[max]; int main() { char c[max]; int sum = 0; int a, b; initialize(); while (cin >> c) { if (c[0] == '-') { a = pop(); b = pop(); push(b - a); } else if (c[0] == '+') { push(pop() + pop()); } else if (c[0] == '*') { push(pop() * pop()); } else { push(atoi(c)); } } printf("%d\n", S[1]); return 0; } void initialize() { top = 0; } int isEmpty() { if (top == 0) return 1; return 0; } int isFull() { if (top >= MAX - 1) return 1; return 0; } void push(int x) { if (isFull()) fprintf(stderr, "Overflow"); top++; S[top] = x; } int pop() { if (isEmpty()) fprintf(stderr, "Anderflow"); top--; return S[top + 1]; }
replace
4
5
4
5
TLE
p02263
C++
Runtime Error
#include <cstdio> #include <cstdlib> #include <iostream> using namespace std; int top, S[1000]; void push(int x); int pop(); int main() { top = 0; int a, b; char c; while (scanf("%s", &c)) { switch (c) { case '+': b = pop(); a = pop(); push(a + b); break; case '-': b = pop(); a = pop(); push(a - b); break; case '*': b = pop(); a = pop(); push(a * b); break; default: push(atoi(&c)); break; } } cout << pop() << endl; } void push(int x) { ++top; S[top] = x; } int pop() { --top; return S[top + 1]; }
#include <cstdio> #include <cstdlib> #include <iostream> using namespace std; int top, S[1000]; void push(int x); int pop(); int main() { top = 0; int a, b; char c; while (scanf("%s", &c) != EOF) { switch (c) { case '+': b = pop(); a = pop(); push(a + b); break; case '-': b = pop(); a = pop(); push(a - b); break; case '*': b = pop(); a = pop(); push(a * b); break; default: push(atoi(&c)); break; } } cout << pop() << endl; } void push(int x) { ++top; S[top] = x; } int pop() { --top; return S[top + 1]; }
replace
15
16
15
16
-11
p02263
C++
Runtime Error
#include <iostream> #include <string> using namespace std; const int MAX = 100; int top, stack[MAX]; void initialize() { top = 0; } void push(int num) { stack[++top] = num; } int pop() { top--; return stack[top + 1]; } int main() { string str; int a, b; initialize(); while (cin >> str) { // cout << str << endl; if (str == "+") { a = pop(); b = pop(); // cout << a + b << ' ' << endl << endl; push(a + b); } else if (str == "-") { a = pop(); b = pop(); // cout << b - a << ' ' << endl << endl; push(b - a); } else if (str == "*") { a = pop(); b = pop(); // cout << a * b << ' ' << endl << endl; push(a * b); } else { push(stoi(str)); } } cout << pop() << endl; }
#include <iostream> #include <string> using namespace std; const int MAX = 1000; int top, stack[MAX]; void initialize() { top = 0; } void push(int num) { stack[++top] = num; } int pop() { top--; return stack[top + 1]; } int main() { string str; int a, b; initialize(); while (cin >> str) { // cout << str << endl; if (str == "+") { a = pop(); b = pop(); // cout << a + b << ' ' << endl << endl; push(a + b); } else if (str == "-") { a = pop(); b = pop(); // cout << b - a << ' ' << endl << endl; push(b - a); } else if (str == "*") { a = pop(); b = pop(); // cout << a * b << ' ' << endl << endl; push(a * b); } else { push(stoi(str)); } } cout << pop() << endl; }
replace
4
5
4
5
0
p02263
C++
Runtime Error
#include <iostream> #include <iterator> #include <string> #include <vector> using namespace std; int const MAX = 100; int top; int s[MAX]; void initalise() { top = 0; } bool isEmpty() { return top == 0; } bool isFull() { return top >= MAX - 1; } void push(int x) { if (isFull()) cout << "エラー(オーバーフロー)" << endl; top++; s[top] = x; } int pop() { if (isEmpty()) cout << "エラー(アンダーフロー)" << endl; top--; return s[top + 1]; } int main() { using ite_t = istream_iterator<string>; ite_t it(cin); ite_t last; while (it != last) { if (*it == "+") { int ope2 = pop(); int ope1 = pop(); push(ope1 + ope2); } else if (*it == "-") { int ope2 = pop(); int ope1 = pop(); push(ope1 - ope2); } else if (*it == "*") { int ope2 = pop(); int ope1 = pop(); push(ope1 * ope2); } else { push(stoi(*it)); } it++; } cout << pop() << endl; }
#include <iostream> #include <iterator> #include <string> #include <vector> using namespace std; int const MAX = 1000; int top; int s[MAX]; void initalise() { top = 0; } bool isEmpty() { return top == 0; } bool isFull() { return top >= MAX - 1; } void push(int x) { if (isFull()) cout << "エラー(オーバーフロー)" << endl; top++; s[top] = x; } int pop() { if (isEmpty()) cout << "エラー(アンダーフロー)" << endl; top--; return s[top + 1]; } int main() { using ite_t = istream_iterator<string>; ite_t it(cin); ite_t last; while (it != last) { if (*it == "+") { int ope2 = pop(); int ope1 = pop(); push(ope1 + ope2); } else if (*it == "-") { int ope2 = pop(); int ope1 = pop(); push(ope1 - ope2); } else if (*it == "*") { int ope2 = pop(); int ope1 = pop(); push(ope1 * ope2); } else { push(stoi(*it)); } it++; } cout << pop() << endl; }
replace
6
7
6
7
0
p02263
C++
Memory Limit Exceeded
#include <iostream> #include <sstream> #include <string> #include <vector> using namespace std; int main() { vector<int> NumberRow; while (true) { string in; cin >> in; if (in == "END") { break; } if (in != "+" && in != "-" && in != "*") { int Num; stringstream ss; ss << in; ss >> Num; NumberRow.push_back(Num); } int M = NumberRow.size() - 2; if (in == "+") { int c = NumberRow[M] + NumberRow[M + 1]; NumberRow[M] = c; NumberRow.pop_back(); } else if (in == "-") { int c = NumberRow[M] - NumberRow[M + 1]; NumberRow[M] = c; NumberRow.pop_back(); } else if (in == "*") { int c = NumberRow[M] * NumberRow[M + 1]; NumberRow[M] = c; NumberRow.pop_back(); } } cout << NumberRow[0] << endl; return 0; }
#include <iostream> #include <sstream> #include <string> #include <vector> using namespace std; int main() { vector<int> NumberRow; while (true) { string in; cin >> in; if (cin.good() == false) { break; } if (in != "+" && in != "-" && in != "*") { int Num; stringstream ss; ss << in; ss >> Num; NumberRow.push_back(Num); } int M = NumberRow.size() - 2; if (in == "+") { int c = NumberRow[M] + NumberRow[M + 1]; NumberRow[M] = c; NumberRow.pop_back(); } else if (in == "-") { int c = NumberRow[M] - NumberRow[M + 1]; NumberRow[M] = c; NumberRow.pop_back(); } else if (in == "*") { int c = NumberRow[M] * NumberRow[M + 1]; NumberRow[M] = c; NumberRow.pop_back(); } } cout << NumberRow[0] << endl; return 0; }
replace
11
12
11
12
MLE
p02263
C++
Runtime Error
#include <stdio.h> #include <stdlib.h> #include <string.h> void add(int num[], int *pa) { *pa = (*pa) - 1; num[(*pa) - 1] += num[*pa]; num[*pa] = 0; } void minus(int num[], int *pa) { *pa = (*pa) - 1; num[(*pa) - 1] -= num[*pa]; num[*pa] = 0; } void multiply(int num[], int *pa) { *pa = (*pa) - 1; num[(*pa) - 1] *= num[*pa]; num[*pa] = 0; } int main() { int i, n, j, k, num[200]; char s[1000]; int curnum = 0; while (scanf("%s", s) != EOF) { if (s[0] == '+') { add(num, &curnum); } else if (s[0] == '-') { minus(num, &curnum); } else if (s[0] == '*') { multiply(num, &curnum); } else { sscanf(s, "%d", num[curnum]); curnum++; } } printf("%d\n", num[0]); }
#include <stdio.h> #include <stdlib.h> #include <string.h> void add(int num[], int *pa) { *pa = (*pa) - 1; num[(*pa) - 1] += num[*pa]; num[*pa] = 0; } void minus(int num[], int *pa) { *pa = (*pa) - 1; num[(*pa) - 1] -= num[*pa]; num[*pa] = 0; } void multiply(int num[], int *pa) { *pa = (*pa) - 1; num[(*pa) - 1] *= num[*pa]; num[*pa] = 0; } int main() { int i, n, j, k, num[200]; char s[1000]; int curnum = 0; while (scanf("%s", s) != EOF) { if (s[0] == '+') { add(num, &curnum); } else if (s[0] == '-') { minus(num, &curnum); } else if (s[0] == '*') { multiply(num, &curnum); } else { sscanf(s, "%d", &num[curnum]); curnum++; } } printf("%d\n", num[0]); }
replace
34
35
34
35
-11
p02263
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <functional> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string.h> #include <vector> #define INF 999999 #define EPS 1.0e-6 using namespace std; int main() { stack<int> s; char b[315]; scanf("%[^\n]", b); int I = 0; while (I < strlen(b)) { if (b[I] == '+' || b[I] == '-' || b[I] == '*') { int j = s.top(); s.pop(); int i = s.top(); s.pop(); int k; if (b[I] == '+') k = i + j; if (b[I] == '-') k = i - j; if (b[I] == '*') k = i * j; s.push(k); I += 2; } else if (b[I] != ' ') { int j = 1; int num = b[I] - '0'; I++; while (b[I] != ' ') { num *= 10; num += (int)(b[I] - '0'); I++; } s.push(num); } else I++; } int ans = s.top(); s.pop(); printf("%d\n", ans); return 0; }
#include <algorithm> #include <cstdio> #include <functional> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string.h> #include <vector> #define INF 999999 #define EPS 1.0e-6 using namespace std; int main() { stack<int> s; char b[1050]; scanf("%[^\n]", b); int I = 0; while (I < strlen(b)) { if (b[I] == '+' || b[I] == '-' || b[I] == '*') { int j = s.top(); s.pop(); int i = s.top(); s.pop(); int k; if (b[I] == '+') k = i + j; if (b[I] == '-') k = i - j; if (b[I] == '*') k = i * j; s.push(k); I += 2; } else if (b[I] != ' ') { int j = 1; int num = b[I] - '0'; I++; while (b[I] != ' ') { num *= 10; num += (int)(b[I] - '0'); I++; } s.push(num); } else I++; } int ans = s.top(); s.pop(); printf("%d\n", ans); return 0; }
replace
18
19
18
19
0
p02263
C++
Runtime Error
#include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; #define MAX 100 int top; int S[MAX]; void initialize() { top = 0; } bool isEmpty() { return top == 0; } bool isFull() { return top >= MAX - 1; } void push(int x) { // if (isFull()){ // cout << "Error" <<endl; // exit(-1); // } top++; S[top] = x; } int pop() { // if (isEmpty()){ // cout << "Error" <<endl; // exit(-1); // } top--; return S[top + 1]; } int main() { int a, b; initialize(); char s[MAX]; while (scanf("%s", s) != EOF) { if (s[0] == '+') { a = pop(); b = pop(); push(a + b); } else if (s[0] == '-') { b = pop(); a = pop(); push(a - b); } else if (s[0] == '*') { a = pop(); b = pop(); push(a * b); } else { push(atoi(s)); } } printf("%d\n", pop()); return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> // using namespace std; #define MAX 1000 int top; int S[MAX]; void initialize() { top = 0; } bool isEmpty() { return top == 0; } bool isFull() { return top >= MAX - 1; } void push(int x) { // if (isFull()){ // cout << "Error" <<endl; // exit(-1); // } top++; S[top] = x; } int pop() { // if (isEmpty()){ // cout << "Error" <<endl; // exit(-1); // } top--; return S[top + 1]; } int main() { int a, b; initialize(); char s[MAX]; while (scanf("%s", s) != EOF) { if (s[0] == '+') { a = pop(); b = pop(); push(a + b); } else if (s[0] == '-') { b = pop(); a = pop(); push(a - b); } else if (s[0] == '*') { a = pop(); b = pop(); push(a * b); } else { push(atoi(s)); } } printf("%d\n", pop()); return 0; }
replace
3
5
3
5
0
p02263
C++
Runtime Error
#include <ctype.h> #include <iostream> #include <sstream> #include <stack> #include <string> using namespace std; stack<int> s; int main() { while (!s.empty()) { s.pop(); } while (cin.eof()) { string tmp = ""; cin >> tmp; stringstream ss; int a = 0; if (isdigit(tmp[0])) { ss << tmp; ss >> a; s.push(a); } else { string sign; sign = tmp; int left, right; right = s.top(); s.pop(); left = s.top(); s.pop(); switch (sign[0]) { case '+': s.push(left + right); break; case '-': s.push(left - right); break; case '*': s.push(left * right); break; case '/': s.push(left / right); break; default: break; } // cout << s.top() << endl; } } cout << s.top() << endl; return 0; }
#include <ctype.h> #include <iostream> #include <sstream> #include <stack> #include <string> using namespace std; stack<int> s; int main() { while (!s.empty()) { s.pop(); } string tmp = ""; while (getline(cin, tmp, ' ')) { stringstream ss; int a = 0; if (isdigit(tmp[0])) { ss << tmp; ss >> a; s.push(a); } else { string sign; sign = tmp; int left, right; right = s.top(); s.pop(); left = s.top(); s.pop(); switch (sign[0]) { case '+': s.push(left + right); break; case '-': s.push(left - right); break; case '*': s.push(left * right); break; case '/': s.push(left / right); break; default: break; } // cout << s.top() << endl; } } cout << s.top() << endl; return 0; }
replace
14
18
14
16
-11
p02263
C++
Runtime Error
#include <algorithm> //std::sort #include <bitset> //std::bitset #include <chrono> //std::chrono(C++11) #include <cmath> //abs,std::pow,sqrt,sin,cos,round,floor,ceil #include <cstdlib> //abs #include <ctime> //localtime_s #include <fstream> //std::ifstream,std::ofstream #include <functional> //std::greater #include <iomanip> //std::setprecision,std::setw,std::setfill #include <iostream> //std::cout, std::cin #include <numeric> //std::accumulate #include <queue> //std::queue #include <random> //std::random(C++11) #include <stack> //std::stack #include <string> //std::string,std::to_string(C++11) #include <valarray> //std::valarray #include <vector> //std::vector const static double de_PI = 3.14159265358979323846; const static int de_MOD = 1000000007; const static int de_MAX = 999999999; const static int de_MIN = -999999999; int main(void) { std::ifstream in("123.txt"); std::cin.rdbuf(in.rdbuf()); // std::ofstream ofs("456.csv"); // std::chrono::system_clock::time_point t_st = // std::chrono::system_clock::now(); std::string temp; int n1 = 0, n2 = 0; std::stack<int> A; while (std::cin >> temp) { switch (temp[0]) { case '+': n1 = A.top(); A.pop(); n2 = A.top(); A.pop(); A.push(n1 + n2); break; case '-': n1 = A.top(); A.pop(); n2 = A.top(); A.pop(); A.push(n2 - n1); break; case '*': n1 = A.top(); A.pop(); n2 = A.top(); A.pop(); A.push(n1 * n2); break; default: A.push(std::stoi(temp)); } } std::cout << A.top() << std::endl; // std::chrono::system_clock::time_point t_ed = // std::chrono::system_clock::now(); std::cout << // std::chrono::duration_cast<std::chrono::milliseconds>(t_ed - t_st).count() // << "ms" << std::endl; }
#include <algorithm> //std::sort #include <bitset> //std::bitset #include <chrono> //std::chrono(C++11) #include <cmath> //abs,std::pow,sqrt,sin,cos,round,floor,ceil #include <cstdlib> //abs #include <ctime> //localtime_s #include <fstream> //std::ifstream,std::ofstream #include <functional> //std::greater #include <iomanip> //std::setprecision,std::setw,std::setfill #include <iostream> //std::cout, std::cin #include <numeric> //std::accumulate #include <queue> //std::queue #include <random> //std::random(C++11) #include <stack> //std::stack #include <string> //std::string,std::to_string(C++11) #include <valarray> //std::valarray #include <vector> //std::vector const static double de_PI = 3.14159265358979323846; const static int de_MOD = 1000000007; const static int de_MAX = 999999999; const static int de_MIN = -999999999; int main(void) { // std::ifstream in("123.txt"); std::cin.rdbuf(in.rdbuf()); // std::ofstream ofs("456.csv"); // std::chrono::system_clock::time_point t_st = // std::chrono::system_clock::now(); std::string temp; int n1 = 0, n2 = 0; std::stack<int> A; while (std::cin >> temp) { switch (temp[0]) { case '+': n1 = A.top(); A.pop(); n2 = A.top(); A.pop(); A.push(n1 + n2); break; case '-': n1 = A.top(); A.pop(); n2 = A.top(); A.pop(); A.push(n2 - n1); break; case '*': n1 = A.top(); A.pop(); n2 = A.top(); A.pop(); A.push(n1 * n2); break; default: A.push(std::stoi(temp)); } } std::cout << A.top() << std::endl; // std::chrono::system_clock::time_point t_ed = // std::chrono::system_clock::now(); std::cout << // std::chrono::duration_cast<std::chrono::milliseconds>(t_ed - t_st).count() // << "ms" << std::endl; }
replace
25
27
25
26
-11
p02263
C++
Runtime Error
#include <stdio.h> #include <stdlib.h> #include <string.h> int top, S[100]; void push(int x) { S[++top] = x; } int pop() { top--; return S[top + 1]; } // int ToInt(char *s){ // int res=s[0]-'0'; // for(int i=1;s[i]!=0;i++){ // res = res*10+s[i]-'0'; // } // // return res; // } int main() { int a, b; top = 0; char s[105]; while (scanf("%s", s) != EOF) { if (s[0] == '+') { a = pop(); b = pop(); push(a + b); } else if (s[0] == '-') { b = pop(); a = pop(); push(a - b); } else if (s[0] == '*') { a = pop(); b = pop(); push(a * b); } else { push(atoi(s)); } } printf("%d\n", pop()); return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> int top, S[105]; void push(int x) { S[++top] = x; } int pop() { top--; return S[top + 1]; } // int ToInt(char *s){ // int res=s[0]-'0'; // for(int i=1;s[i]!=0;i++){ // res = res*10+s[i]-'0'; // } // // return res; // } int main() { int a, b; top = 0; char s[105]; while (scanf("%s", s) != EOF) { if (s[0] == '+') { a = pop(); b = pop(); push(a + b); } else if (s[0] == '-') { b = pop(); a = pop(); push(a - b); } else if (s[0] == '*') { a = pop(); b = pop(); push(a * b); } else { push(atoi(s)); } } printf("%d\n", pop()); return 0; }
replace
4
5
4
5
0
p02263
C++
Runtime Error
#include <cassert> #include <stdio.h> #include <stdlib.h> #include <string.h> int top, S[1000]; void push(int x) { S[++top] = x; } int pop() { top--; return S[top + 1]; } int main() { int a, b, x, no, np; top = 0; char s[100]; no = np = 0; while (scanf("%s", s) != EOF) { if (s[0] == '+') { a = pop(); b = pop(); push(a + b); no++; } else if (s[0] == '-') { b = pop(); a = pop(); push(a - b); no++; } else if (s[0] == '*') { a = pop(); b = pop(); push(a * b); no++; } else { x = atoi(s); assert(0 <= x && x <= 1000000); push(x); np++; } } assert(2 <= np && np <= 100); assert(1 <= no && no <= 9); printf("%d\n", pop()); return 0; }
#include <cassert> #include <stdio.h> #include <stdlib.h> #include <string.h> int top, S[1000]; void push(int x) { S[++top] = x; } int pop() { top--; return S[top + 1]; } int main() { int a, b, x, no, np; top = 0; char s[100]; no = np = 0; while (scanf("%s", s) != EOF) { if (s[0] == '+') { a = pop(); b = pop(); push(a + b); no++; } else if (s[0] == '-') { b = pop(); a = pop(); push(a - b); no++; } else if (s[0] == '*') { a = pop(); b = pop(); push(a * b); no++; } else { x = atoi(s); assert(0 <= x && x <= 1000000); push(x); np++; } } assert(2 <= np && np <= 100); assert(1 <= no && no <= 99); printf("%d\n", pop()); return 0; }
replace
43
44
43
44
0
p02263
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <stack> #include <string> #include <vector> using namespace std; #define FOR(i, start, end) for (int i = (start); i < (end); ++i) #define REP(i, n) FOR(i, 0, n) const int OP_PLUS = -1; const int OP_MINUS = -2; const int OP_MUL = -3; vector<int> read_expr() { vector<int> expr; int c; string buf; while ((c = getchar()) != EOF) { if (isspace(c)) { if (!buf.empty()) { expr.push_back(atoi(buf.c_str())); buf.clear(); } if (c == '\n') break; } else if (isdigit(c)) { buf += static_cast<char>(c); } else if (c == '+') { expr.push_back(OP_PLUS); } else if (c == '-') { expr.push_back(OP_MINUS); } else if (c == '*') { expr.push_back(OP_MUL); } } return expr; } int interpret(const vector<int> &expr) { stack<int> s; for (int sym : expr) { if (sym >= 0) { s.push(sym); } else if (sym == OP_PLUS) { int rhs = s.top(); s.pop(); int lhs = s.top(); s.pop(); s.push(lhs + rhs); } else if (sym == OP_MINUS) { int rhs = s.top(); s.pop(); int lhs = s.top(); s.pop(); s.push(lhs - rhs); } else if (sym == OP_MUL) { int rhs = s.top(); s.pop(); int lhs = s.top(); s.pop(); s.push(lhs * rhs); } } return s.top(); } int main() { vector<int> expr = read_expr(); #if 0 REP(i, expr.size()) { printf("%d ", expr[i]); } #endif int result = interpret(expr); printf("%d\n", result); fflush(stdout); return 0; }
/** * ????????? runtime error ????????? */ #include <algorithm> #include <cassert> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <stack> #include <string> #include <vector> using namespace std; #define FOR(i, start, end) for (int i = (start); i < (end); ++i) #define REP(i, n) FOR(i, 0, n) const int OP_PLUS = -1; const int OP_MINUS = -2; const int OP_MUL = -3; vector<int> read_expr() { vector<int> expr; int c; string buf; while ((c = getchar()) != EOF) { if (isspace(c)) { if (!buf.empty()) { expr.push_back(atoi(buf.c_str())); buf.clear(); } if (c == '\n') break; } else if (isdigit(c)) { buf += static_cast<char>(c); } else if (c == '+') { expr.push_back(OP_PLUS); } else if (c == '-') { expr.push_back(OP_MINUS); } else if (c == '*') { expr.push_back(OP_MUL); } } return expr; } int interpret(const vector<int> &expr) { stack<int> s; for (int sym : expr) { if (sym >= 0) { s.push(sym); } else if (sym == OP_PLUS) { int rhs = s.top(); s.pop(); int lhs = s.top(); s.pop(); s.push(lhs + rhs); } else if (sym == OP_MINUS) { int rhs = s.top(); s.pop(); int lhs = s.top(); s.pop(); s.push(lhs - rhs); } else if (sym == OP_MUL) { int rhs = s.top(); s.pop(); int lhs = s.top(); s.pop(); s.push(lhs * rhs); } } return s.top(); } int main() { vector<int> expr = read_expr(); #if 0 REP(i, expr.size()) { printf("%d ", expr[i]); } #endif int result = interpret(expr); printf("%d\n", result); fflush(stdout); return 0; }
insert
0
0
0
4
0
p02263
C++
Runtime Error
#include <stdio.h> #include <stdlib.h> #define MAX 1000 void initialize(); int isEmpty(); int isFull(); void push(int x); int pop(); int top; int S[100]; int main() { int i, n, ans, n1, n2, n3; char c[100]; initialize(); while (1) { if (scanf("%s", c) == EOF) break; if (c[0] == '+' || c[0] == '-' || c[0] == '*') { n1 = pop(); n2 = pop(); if (c[0] == '+') n3 = n1 + n2; if (c[0] == '-') n3 = n2 - n1; if (c[0] == '*') n3 = n1 * n2; push(n3); } else { n = atoi(c); push(n); } } printf("%d\n", pop()); return 0; } void initialize() { top = 0; } int isEmpty() { return top == 0; } int isFull() { return top >= MAX - 1; } void push(int x) { if (isFull()) fprintf(stderr, "overflow\n"); top++; S[top] = x; } int pop() { if (isEmpty()) fprintf(stderr, "underflow\n"); top--; return S[top + 1]; }
#include <stdio.h> #include <stdlib.h> #define MAX 1000 void initialize(); int isEmpty(); int isFull(); void push(int x); int pop(); int top; int S[1000]; int main() { int i, n, ans, n1, n2, n3; char c[100]; initialize(); while (1) { if (scanf("%s", c) == EOF) break; if (c[0] == '+' || c[0] == '-' || c[0] == '*') { n1 = pop(); n2 = pop(); if (c[0] == '+') n3 = n1 + n2; if (c[0] == '-') n3 = n2 - n1; if (c[0] == '*') n3 = n1 * n2; push(n3); } else { n = atoi(c); push(n); } } printf("%d\n", pop()); return 0; } void initialize() { top = 0; } int isEmpty() { return top == 0; } int isFull() { return top >= MAX - 1; } void push(int x) { if (isFull()) fprintf(stderr, "overflow\n"); top++; S[top] = x; } int pop() { if (isEmpty()) fprintf(stderr, "underflow\n"); top--; return S[top + 1]; }
replace
12
13
12
13
0
p02263
C++
Runtime Error
#include <cstdio> #include <cstdlib> #include <iostream> using namespace std; int top, S[100]; void push(int x) { S[++top] = x; } int pop() { return S[top--]; } int main() { char str[10]; int a, b; top = 0; while (scanf("%s", str) != EOF) { if (str[0] == '+') { a = pop(); b = pop(); push(a + b); } else if (str[0] == '-') { a = pop(); b = pop(); push(b - a); } else if (str[0] == '*') { a = pop(); b = pop(); push(a * b); } else { push(atoi(str)); } } cout << pop() << endl; }
#include <cstdio> #include <cstdlib> #include <iostream> using namespace std; int top, S[101]; void push(int x) { S[++top] = x; } int pop() { return S[top--]; } int main() { char str[10]; int a, b; top = 0; while (scanf("%s", str) != EOF) { if (str[0] == '+') { a = pop(); b = pop(); push(a + b); } else if (str[0] == '-') { a = pop(); b = pop(); push(b - a); } else if (str[0] == '*') { a = pop(); b = pop(); push(a * b); } else { push(atoi(str)); } } cout << pop() << endl; }
replace
5
6
5
6
0
p02263
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; #define N 100 class Stack { public: void initialize(); bool isEmpty(); bool isFull(); void push(const int); int pop(); Stack(const int); ~Stack(); private: int top, STACKSIZE; vector<int> S; }; void addition(Stack &s) { int a = s.pop(), b = s.pop(); s.push(a + b); } void subtraction(Stack &s) { int a = s.pop(), b = s.pop(); s.push(b - a); } void multiplication(Stack &s) { int a = s.pop(), b = s.pop(); s.push(b * a); } int main() { Stack _s(N); char s[N]; while (scanf("%s", s) != EOF) { if (s[0] == '+') { addition(_s); } else if (s[0] == '-') { subtraction(_s); } else if (s[0] == '*') { multiplication(_s); } else { _s.push(atoi(s)); } } cout << _s.pop() << endl; return 0; } Stack::Stack(const int s) : top(0) { STACKSIZE = s; vector<int> tmp(STACKSIZE, 0); S = tmp; } Stack::~Stack() { // cout << "STACK DELETED" << endl; } void Stack::initialize() { top = 0; } bool Stack::isEmpty() { return (top == 0); } bool Stack::isFull() { return (top >= STACKSIZE - 1); } void Stack::push(const int x) { if (isFull()) { cout << "Overflow!" << endl; exit(1); } top += 1; S[top] = x; } int Stack::pop() { if (isEmpty()) { cout << "Underflow" << endl; exit(2); } top -= 1; return (S[top + 1]); }
#include <iostream> #include <vector> using namespace std; #define N 101 class Stack { public: void initialize(); bool isEmpty(); bool isFull(); void push(const int); int pop(); Stack(const int); ~Stack(); private: int top, STACKSIZE; vector<int> S; }; void addition(Stack &s) { int a = s.pop(), b = s.pop(); s.push(a + b); } void subtraction(Stack &s) { int a = s.pop(), b = s.pop(); s.push(b - a); } void multiplication(Stack &s) { int a = s.pop(), b = s.pop(); s.push(b * a); } int main() { Stack _s(N); char s[N]; while (scanf("%s", s) != EOF) { if (s[0] == '+') { addition(_s); } else if (s[0] == '-') { subtraction(_s); } else if (s[0] == '*') { multiplication(_s); } else { _s.push(atoi(s)); } } cout << _s.pop() << endl; return 0; } Stack::Stack(const int s) : top(0) { STACKSIZE = s; vector<int> tmp(STACKSIZE, 0); S = tmp; } Stack::~Stack() { // cout << "STACK DELETED" << endl; } void Stack::initialize() { top = 0; } bool Stack::isEmpty() { return (top == 0); } bool Stack::isFull() { return (top >= STACKSIZE - 1); } void Stack::push(const int x) { if (isFull()) { cout << "Overflow!" << endl; exit(1); } top += 1; S[top] = x; } int Stack::pop() { if (isEmpty()) { cout << "Underflow" << endl; exit(2); } top -= 1; return (S[top + 1]); }
replace
4
5
4
5
0
p02263
C++
Runtime Error
#include <cstdlib> #include <iostream> #include <sstream> #include <stdexcept> #include <stdio.h> #include <string> using namespace std; #define abs(N) ((N > 0) ? N : -1 * N) #define lt(A, B) ((A > B) ? B : A) #define gt(A, B) ((A < B) ? B : A) #define print(S) (cout << S) #define println(S) (cout << S << endl) template <typename T> class stack { public: const static int MAX = 3; stack<T>() { pos = 0; } void empty() { pos = 0; } bool isEmpty() { return (pos == 0); } bool isFull() { return (pos == MAX); } T pop() { if (isEmpty()) { throw exception(); } return body[--pos]; } void push(T el) { if (isFull()) { throw exception(); } body[pos++] = el; } T top() { if (isEmpty()) { throw exception(); } return body[pos - 1]; } private: T body[MAX]; int pos; }; int main() { string buf, str; char cstr[1000]; cin.getline(cstr, sizeof(cstr)); str = string(cstr); stringstream ss(str); stack<int> st; while (getline(ss, buf, ' ')) { // println("-----"+buf+"-----"); if (buf == "+" || buf == "-" || buf == "*") { int r1, r2, ret; r1 = st.pop(); r2 = st.pop(); if (buf == "+") { ret = r1 + r2; } if (buf == "-") { ret = r2 - r1; } if (buf == "*") { ret = r1 * r2; } st.push(ret); } else { st.push(atoi(buf.c_str())); } } println(st.top()); return 0; }
#include <cstdlib> #include <iostream> #include <sstream> #include <stdexcept> #include <stdio.h> #include <string> using namespace std; #define abs(N) ((N > 0) ? N : -1 * N) #define lt(A, B) ((A > B) ? B : A) #define gt(A, B) ((A < B) ? B : A) #define print(S) (cout << S) #define println(S) (cout << S << endl) template <typename T> class stack { public: const static int MAX = 10000; stack<T>() { pos = 0; } void empty() { pos = 0; } bool isEmpty() { return (pos == 0); } bool isFull() { return (pos == MAX); } T pop() { if (isEmpty()) { throw exception(); } return body[--pos]; } void push(T el) { if (isFull()) { throw exception(); } body[pos++] = el; } T top() { if (isEmpty()) { throw exception(); } return body[pos - 1]; } private: T body[MAX]; int pos; }; int main() { string buf, str; char cstr[1000]; cin.getline(cstr, sizeof(cstr)); str = string(cstr); stringstream ss(str); stack<int> st; while (getline(ss, buf, ' ')) { // println("-----"+buf+"-----"); if (buf == "+" || buf == "-" || buf == "*") { int r1, r2, ret; r1 = st.pop(); r2 = st.pop(); if (buf == "+") { ret = r1 + r2; } if (buf == "-") { ret = r2 - r1; } if (buf == "*") { ret = r1 * r2; } st.push(ret); } else { st.push(atoi(buf.c_str())); } } println(st.top()); return 0; }
replace
16
17
16
17
0
p02263
C++
Runtime Error
#include <cstdio> #include <cstdlib> #include <iostream> #include <stack> #include <string> using namespace std; int main() { stack<int> num; string s; int a, b; while (cin >> s) { if (s >= "1") num.push(atoi(s.c_str())); else if (s == "+" || s == "-" || s == "*") { a = num.top(); num.pop(); b = num.top(); num.pop(); if (s == "+") num.push(a + b); else if (s == "-") num.push(b - a); else num.push(a * b); } } cout << num.top() << endl; }
#include <cstdio> #include <cstdlib> #include <iostream> #include <stack> #include <string> using namespace std; int main() { stack<int> num; string s; int a, b; while (cin >> s) { if (s >= "0") num.push(atoi(s.c_str())); else if (s == "+" || s == "-" || s == "*") { a = num.top(); num.pop(); b = num.top(); num.pop(); if (s == "+") num.push(a + b); else if (s == "-") num.push(b - a); else num.push(a * b); } } cout << num.top() << endl; }
replace
13
14
13
14
0
p02263
C++
Runtime Error
#include <algorithm> #include <cctype> #include <cmath> #include <iomanip> #include <iostream> #include <stack> #include <string> #include <vector> using namespace std; int main() { int a, b; stack<int> st; string op; while (cin >> op) { cin >> op; if (isdigit(op[0])) st.push(stoi(op)); else { b = st.top(); st.pop(); a = st.top(); st.pop(); switch (op[0]) { case '+': st.push(a + b); break; case '-': st.push(a - b); break; case '*': st.push(a * b); break; } } } cout << st.top() << endl; }
#include <algorithm> #include <cctype> #include <cmath> #include <iomanip> #include <iostream> #include <stack> #include <string> #include <vector> using namespace std; int main() { int a, b; stack<int> st; string op; while (cin >> op) { // cout << op << endl; if (isdigit(op[0])) st.push(stoi(op)); else { b = st.top(); st.pop(); a = st.top(); st.pop(); switch (op[0]) { case '+': st.push(a + b); break; case '-': st.push(a - b); break; case '*': st.push(a * b); break; } } } cout << st.top() << endl; }
replace
16
17
16
17
-11
p02263
C++
Runtime Error
#include <iostream> #include <stack> #include <stdlib.h> #include <string> using namespace std; int main() { string word; stack<int> S; int n, n1, n2, p; while (cin >> word) { if (word == "+") { n1 = S.top(); S.pop(); n2 = S.top(); S.pop(); n = n1 + n2; S.push(n); } else if (word == "-") { n1 = S.top(); S.pop(); n2 = S.top(); S.pop(); n = n2 - n1; S.push(n); } else if (word == "*") { n1 = S.top(); S.pop(); n2 = S.top(); S.pop(); n = n1 * n2; S.push(n); } else { p = atoi(word.c_str()); S.push(p); } } cout << S.top() << endl; return S.top(); }
#include <iostream> #include <stack> #include <stdlib.h> #include <string> using namespace std; int main() { string word; stack<int> S; int n, n1, n2, p; while (cin >> word) { if (word == "+") { n1 = S.top(); S.pop(); n2 = S.top(); S.pop(); n = n1 + n2; S.push(n); } else if (word == "-") { n1 = S.top(); S.pop(); n2 = S.top(); S.pop(); n = n2 - n1; S.push(n); } else if (word == "*") { n1 = S.top(); S.pop(); n2 = S.top(); S.pop(); n = n1 * n2; S.push(n); } else { p = atoi(word.c_str()); S.push(p); } } cout << S.top() << endl; return 0; }
replace
38
39
38
39
3
p02263
C++
Runtime Error
#include <iostream> #include <sstream> #include <string> using namespace std; class Stack { private: int sp; int S[10]; public: Stack() { sp = 0; } bool isFull() const { return sp == 100; } bool isEmpty() const { return sp == 0; } void push(int d) { if (isFull()) return; S[sp] = d; sp++; } int top() { if (isEmpty()) return -1; return S[sp - 1]; } void pop() { if (isEmpty()) return; sp--; } }; int main() { Stack s; string expr; getline(cin, expr); istringstream is(expr); while (!is.eof()) { string op; is >> op; int tmp1, tmp2; switch (op[0]) { case '+': tmp1 = s.top(); s.pop(); tmp2 = s.top(); s.pop(); s.push(tmp1 + tmp2); break; case '-': tmp1 = s.top(); s.pop(); tmp2 = s.top(); s.pop(); s.push(tmp2 - tmp1); break; case '*': tmp1 = s.top(); s.pop(); tmp2 = s.top(); s.pop(); s.push(tmp1 * tmp2); break; default: istringstream is2(op); is2 >> tmp1; s.push(tmp1); break; } } cout << s.top() << endl; return 0; }
#include <iostream> #include <sstream> #include <string> using namespace std; class Stack { private: int sp; int S[100]; public: Stack() { sp = 0; } bool isFull() const { return sp == 100; } bool isEmpty() const { return sp == 0; } void push(int d) { if (isFull()) return; S[sp] = d; sp++; } int top() { if (isEmpty()) return -1; return S[sp - 1]; } void pop() { if (isEmpty()) return; sp--; } }; int main() { Stack s; string expr; getline(cin, expr); istringstream is(expr); while (!is.eof()) { string op; is >> op; int tmp1, tmp2; switch (op[0]) { case '+': tmp1 = s.top(); s.pop(); tmp2 = s.top(); s.pop(); s.push(tmp1 + tmp2); break; case '-': tmp1 = s.top(); s.pop(); tmp2 = s.top(); s.pop(); s.push(tmp2 - tmp1); break; case '*': tmp1 = s.top(); s.pop(); tmp2 = s.top(); s.pop(); s.push(tmp1 * tmp2); break; default: istringstream is2(op); is2 >> tmp1; s.push(tmp1); break; } } cout << s.top() << endl; return 0; }
replace
8
9
8
9
0
p02263
C++
Runtime Error
#include <iostream> #include <stack> using namespace std; int main() { stack<int> t; string s; int a, b, c; while (cin >> s) { if ("0" <= s && s <= "9") { if (s.size() >= 2) { a = s.size(); b = 1; for (int i = 0; i < a - 1; i++) b *= 10; c = 0; for (int i = 0; i < a; i++) { c += (s[i] - '0') * b; b /= 10; } t.push(c); } else t.push(s[0] - '0'); } else if (s == "+") { a = t.top(); t.pop(); b = t.top(); t.pop(); t.push(a + b); } else if (s == "-") { a = t.top(); t.pop(); b = t.top(); t.pop(); t.push(b - a); } else if (s == "*") { a = t.top(); t.pop(); b = t.top(); t.pop(); t.push(a * b); } } cout << t.top() << endl; return 0; }
#include <iostream> #include <stack> using namespace std; int main() { stack<int> t; string s; int a, b, c; while (cin >> s) { if ('0' <= s[0] && s[0] <= '9') { if (s.size() >= 2) { a = s.size(); b = 1; for (int i = 0; i < a - 1; i++) b *= 10; c = 0; for (int i = 0; i < a; i++) { c += (s[i] - '0') * b; b /= 10; } t.push(c); } else t.push(s[0] - '0'); } else if (s == "+") { a = t.top(); t.pop(); b = t.top(); t.pop(); t.push(a + b); } else if (s == "-") { a = t.top(); t.pop(); b = t.top(); t.pop(); t.push(b - a); } else if (s == "*") { a = t.top(); t.pop(); b = t.top(); t.pop(); t.push(a * b); } } cout << t.top() << endl; return 0; }
replace
9
10
9
10
0
p02263
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; void push(int A); int pop(void); int S[1000]; int Top = 0; int main(void) { string str; int a, b; Top = 0; while (str != "-1") { cin >> str; if (str[0] == '+') { a = pop(); b = pop(); push(a + b); } else if (str[0] == '-') { b = pop(); a = pop(); push(a - b); } else if (str[0] == '*') { a = pop(); b = pop(); push(a * b); } else { push(atoi(str.c_str())); } } printf("%d\n", pop()); return 0; } void push(int A) { S[++Top] = A; } int pop(void) { Top--; return S[Top + 1]; }
#include "bits/stdc++.h" using namespace std; void push(int A); int pop(void); int S[1000]; int Top = 0; int main(void) { string str; int a, b; Top = 0; while (cin >> str) { if (str[0] == '+') { a = pop(); b = pop(); push(a + b); } else if (str[0] == '-') { b = pop(); a = pop(); push(a - b); } else if (str[0] == '*') { a = pop(); b = pop(); push(a * b); } else { push(atoi(str.c_str())); } } printf("%d\n", pop()); return 0; } void push(int A) { S[++Top] = A; } int pop(void) { Top--; return S[Top + 1]; }
replace
14
16
14
15
-6
terminate called after throwing an instance of 'std::__ios_failure' what(): basic_ios::clear: iostream error
p02263
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstdlib> #include <iostream> #include <sstream> #include <stdlib.h> #include <string> #include <vector> using namespace std; int top, s[100]; int pop() { top--; return s[top + 1]; } void push(int a) { s[++top] = a; // cout<<a<<endl; } int main() { char temp[100] = {}; int a, b; while (cin >> temp) { if (temp[0] == '+') { a = pop(); b = pop(); push(a + b); } else if (temp[0] == '-') { a = pop(); b = pop(); push(b - a); // cout<<a<<"-"<<b<<endl; } else if (temp[0] == '*') push(pop() * pop()); else { push(atoi(temp)); // cout<<temp<<endl; } } cout << pop() << endl; return 0; }
#include <algorithm> #include <cstdio> #include <cstdlib> #include <iostream> #include <sstream> #include <stdlib.h> #include <string> #include <vector> using namespace std; int top, s[101]; int pop() { top--; return s[top + 1]; } void push(int a) { s[++top] = a; // cout<<a<<endl; } int main() { char temp[100] = {}; int a, b; while (cin >> temp) { if (temp[0] == '+') { a = pop(); b = pop(); push(a + b); } else if (temp[0] == '-') { a = pop(); b = pop(); push(b - a); // cout<<a<<"-"<<b<<endl; } else if (temp[0] == '*') push(pop() * pop()); else { push(atoi(temp)); // cout<<temp<<endl; } } cout << pop() << endl; return 0; }
replace
9
10
9
10
0
p02263
C++
Runtime Error
#include <cstdlib> #include <iostream> using namespace std; int top, S[100]; void push(int x) { S[++top] = x; } int pop() { top--; return S[top + 1]; } int main() { int a, b; top = 0; string s; while (cin >> s) { if (s[0] == '+') { a = pop(); b = pop(); push(a + b); } else if (s[0] == '-') { b = pop(); a = pop(); push(a - b); } else if (s[0] == '*') { a = pop(); b = pop(); push(a * b); } else { push(atoi(s.c_str())); } } cout << pop() << endl; return 0; }
#include <cstdlib> #include <iostream> using namespace std; int top, S[101]; void push(int x) { S[++top] = x; } int pop() { top--; return S[top + 1]; } int main() { int a, b; top = 0; string s; while (cin >> s) { if (s[0] == '+') { a = pop(); b = pop(); push(a + b); } else if (s[0] == '-') { b = pop(); a = pop(); push(a - b); } else if (s[0] == '*') { a = pop(); b = pop(); push(a * b); } else { push(atoi(s.c_str())); } } cout << pop() << endl; return 0; }
replace
5
6
5
6
0
p02263
C++
Runtime Error
#include <cstdio> #include <iostream> using namespace std; int main() { string s; long int stk[50]; int indx = 0; int i = 0; getline(cin, s); while (i < (int)s.size()) { if (s[i] == ' ') { } else if (s[i] == '+') { stk[indx - 2] += stk[indx - 1]; indx--; } else if (s[i] == '-') { stk[indx - 2] -= stk[indx - 1]; indx--; } else if (s[i] == '*') { stk[indx - 2] *= stk[indx - 1]; indx--; } else { stk[indx] = 0; while (s[i] != ' ') { stk[indx] *= 10; stk[indx] += s[i] - '0'; i++; } i--; indx++; } i++; } cout << stk[0] << endl; return 0; }
#include <cstdio> #include <iostream> using namespace std; int main() { string s; int stk[100]; int indx = 0; int i = 0; getline(cin, s); while (i < (int)s.size()) { if (s[i] == ' ') { } else if (s[i] == '+') { stk[indx - 2] += stk[indx - 1]; indx--; } else if (s[i] == '-') { stk[indx - 2] -= stk[indx - 1]; indx--; } else if (s[i] == '*') { stk[indx - 2] *= stk[indx - 1]; indx--; } else { stk[indx] = 0; while (s[i] != ' ') { stk[indx] *= 10; stk[indx] += s[i] - '0'; i++; } i--; indx++; } i++; } cout << stk[0] << endl; return 0; }
replace
7
8
7
8
0
p02263
C++
Runtime Error
#include <iostream> #include <sstream> #include <stack> #include <string> using namespace std; int main() { string str; getline(cin, str); stack<int> sta; stringstream ss(str); string item; while (getline(ss, item, ' ')) { if ('0' < item[0] && item[0] < '9') { sta.push(stoi(item)); continue; } int r = sta.top(); sta.pop(); int l = sta.top(); sta.pop(); switch (item[0]) { case '+': sta.push(l + r); break; case '-': sta.push(l - r); break; case '*': sta.push(l * r); break; default: break; } } cout << sta.top() << endl; return 0; }
#include <iostream> #include <sstream> #include <stack> #include <string> using namespace std; int main() { string str; getline(cin, str); stack<int> sta; stringstream ss(str); string item; while (getline(ss, item, ' ')) { if ('0' <= item[0] && item[0] <= '9') { sta.push(stoi(item)); continue; } int r = sta.top(); sta.pop(); int l = sta.top(); sta.pop(); switch (item[0]) { case '+': sta.push(l + r); break; case '-': sta.push(l - r); break; case '*': sta.push(l * r); break; default: break; } } cout << sta.top() << endl; return 0; }
replace
15
16
15
16
0
p02263
C++
Runtime Error
#include <cstdlib> #include <iostream> #include <string> using namespace std; struct stack { public: int top; int stk[5]; void reset() { top = 0; } void push(int x) { top++; stk[top] = x; } int pop() { top--; return stk[top + 1]; } }; int main() { string in; stack stack; stack.reset(); while (true) { cin >> in; if (cin.eof()) break; else if (in == "+") { int a = stack.pop(); int b = stack.pop(); stack.push(a + b); } else if (in == "*") { int a = stack.pop(); int b = stack.pop(); stack.push(a * b); } else if (in == "-") { int a = stack.pop(); int b = stack.pop(); stack.push(b - a); } else { stack.push(atoi(in.c_str())); } } cout << stack.pop() << endl; }
#include <cstdlib> #include <iostream> #include <string> using namespace std; struct stack { public: int top; int stk[10000]; void reset() { top = 0; } void push(int x) { top++; stk[top] = x; } int pop() { top--; return stk[top + 1]; } }; int main() { string in; stack stack; stack.reset(); while (true) { cin >> in; if (cin.eof()) break; else if (in == "+") { int a = stack.pop(); int b = stack.pop(); stack.push(a + b); } else if (in == "*") { int a = stack.pop(); int b = stack.pop(); stack.push(a * b); } else if (in == "-") { int a = stack.pop(); int b = stack.pop(); stack.push(b - a); } else { stack.push(atoi(in.c_str())); } } cout << stack.pop() << endl; }
replace
8
9
8
9
0
p02263
C++
Runtime Error
#include <iostream> #include <string> using namespace std; template <typename T, int N> class stack { private: int size; T array[N]; public: stack() { size = 0; } bool push(T x) { if (isFull()) { return false; } array[size] = x; size++; return true; } T pop() { size--; return array[size]; } bool isEmpty() { if (size == 0) { return true; } else { return false; } } bool isFull() { if (size >= N) { return true; } else { return false; } } }; int main() { stack<int, 20000> st; string a; int j = 0; while (j < 7) { j++; cin >> a; if (a[0] == '+') { int a = st.pop(); int b = st.pop(); st.push(b + a); } else if (a[0] == '-') { int a = st.pop(); int b = st.pop(); st.push(b - a); } else if (a[0] == '*') { int a = st.pop(); int b = st.pop(); st.push(b * a); } else { int x = 0; for (int i = 0; i < a.size(); i++) { x *= 10; x += a[i] - '0'; } st.push(x); } } cout << st.pop() << "\n"; return 0; }
#include <iostream> #include <string> using namespace std; template <typename T, int N> class stack { private: int size; T array[N]; public: stack() { size = 0; } bool push(T x) { if (isFull()) { return false; } array[size] = x; size++; return true; } T pop() { size--; return array[size]; } bool isEmpty() { if (size == 0) { return true; } else { return false; } } bool isFull() { if (size >= N) { return true; } else { return false; } } }; int main() { stack<int, 20000> st; string a; while (cin >> a) { if (a[0] == '+') { int a = st.pop(); int b = st.pop(); st.push(b + a); } else if (a[0] == '-') { int a = st.pop(); int b = st.pop(); st.push(b - a); } else if (a[0] == '*') { int a = st.pop(); int b = st.pop(); st.push(b * a); } else { int x = 0; for (int i = 0; i < a.size(); i++) { x *= 10; x += a[i] - '0'; } st.push(x); } } cout << st.pop() << "\n"; return 0; }
replace
48
52
48
49
0
p02263
C++
Runtime Error
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; int s[1000], top = 0; void push(int x) { s[++top] = x; } int pop() { --top; return s[top + 1]; } int main() { int c, b; char a[100]; while (scanf("%s", a)) { if (a[0] == '+') { b = pop(); c = pop(); push(b + c); } else if (a[0] == '-') { b = pop(); c = pop(); push(c - b); } else if (a[0] == '*') { b = pop(); c = pop(); push(b * c); } else { push(atoi(a)); } } printf("%d\n", pop()); return 0; }
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; int s[1000], top = 0; void push(int x) { s[++top] = x; } int pop() { --top; return s[top + 1]; } int main() { int c, b; char a[100]; while (scanf("%s", a) != EOF) { if (a[0] == '+') { b = pop(); c = pop(); push(b + c); } else if (a[0] == '-') { b = pop(); c = pop(); push(c - b); } else if (a[0] == '*') { b = pop(); c = pop(); push(b * c); } else { push(atoi(a)); } } printf("%d\n", pop()); return 0; }
replace
16
17
16
17
-11
p02264
C++
Runtime Error
#include <iostream> #include <string> using namespace std; class Process { private: string name; int time; public: Process() = default; Process(string name, int time) { this->name = name; this->time = time; } string getName() { return this->name; } int getTime() { return this->time; } void setTime(int time) { this->time = time; } }; int pos_min = 0; int pos_max = 0; Process queue[100000]; void enqueue(Process &process) { queue[pos_max] = process; pos_max++; } Process dequeue() { Process process = queue[pos_min]; pos_min++; return process; } bool isEmpty() { return pos_min == pos_max; } int main() { int num, quantum; scanf("%d %d", &num, &quantum); for (int i = 0; i < num; i++) { char name[100]; int time; scanf("%s %d", name, &time); Process process(name, time); enqueue(process); } int cost = 0; while (true) { Process process = dequeue(); if (process.getTime() > quantum) { cost += quantum; process.setTime(process.getTime() - quantum); enqueue(process); } else if (process.getTime() <= quantum) { cost += process.getTime(); printf("%s %d\n", process.getName().c_str(), cost); if (isEmpty()) { break; } } } return 0; }
#include <iostream> #include <string> using namespace std; class Process { private: string name; int time; public: Process() = default; Process(string name, int time) { this->name = name; this->time = time; } string getName() { return this->name; } int getTime() { return this->time; } void setTime(int time) { this->time = time; } }; int pos_min = 0; int pos_max = 0; Process queue[1000000]; void enqueue(Process &process) { queue[pos_max] = process; pos_max++; } Process dequeue() { Process process = queue[pos_min]; pos_min++; return process; } bool isEmpty() { return pos_min == pos_max; } int main() { int num, quantum; scanf("%d %d", &num, &quantum); for (int i = 0; i < num; i++) { char name[100]; int time; scanf("%s %d", name, &time); Process process(name, time); enqueue(process); } int cost = 0; while (true) { Process process = dequeue(); if (process.getTime() > quantum) { cost += quantum; process.setTime(process.getTime() - quantum); enqueue(process); } else if (process.getTime() <= quantum) { cost += process.getTime(); printf("%s %d\n", process.getName().c_str(), cost); if (isEmpty()) { break; } } } return 0; }
replace
23
24
23
24
0
p02264
C++
Runtime Error
#include <cstdio> #include <cstring> #include <iostream> #include <queue> #define LEN 100005 using namespace std; typedef struct node { char name[100]; int time; } P; P Q[LEN + 1]; int head, tail, n; void enqueue(P x) { Q[tail + 1] = x; } P dequeue() { return Q[head]; } int main(int argc, char *argv[]) { int i, q; scanf("%d %d", &n, &q); for (i = 0; i < n; i++) { scanf("%s", Q[i].name); scanf("%d", &Q[i].time); } int elaps = 0; P u; head = 0, tail = n; while (head != tail) { u = Q[head++]; if (u.time > q) { u.time -= q; Q[tail++] = u; elaps += q; } else { elaps = elaps + u.time; printf("%s %d\n", u.name, elaps); } } return 0; }
#include <cstdio> #include <cstring> #include <iostream> #include <queue> #define LEN 300005 using namespace std; typedef struct node { char name[100]; int time; } P; P Q[LEN + 1]; int head, tail, n; void enqueue(P x) { Q[tail + 1] = x; } P dequeue() { return Q[head]; } int main(int argc, char *argv[]) { int i, q; scanf("%d %d", &n, &q); for (i = 0; i < n; i++) { scanf("%s", Q[i].name); scanf("%d", &Q[i].time); } int elaps = 0; P u; head = 0, tail = n; while (head != tail) { u = Q[head++]; if (u.time > q) { u.time -= q; Q[tail++] = u; elaps += q; } else { elaps = elaps + u.time; printf("%s %d\n", u.name, elaps); } } return 0; }
replace
4
5
4
5
0
p02264
C++
Runtime Error
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; #define MAX 100000 int tail = 0; int head = 0; typedef struct _queue { char name[10]; int time; } queuecell; queuecell queue[MAX]; void enqueue(char *name, int time) { tail++; strcpy(queue[tail].name, name); queue[tail].time = time; head++; } queuecell dequeue() { queuecell cell; strcpy(cell.name, queue[head].name); cell.time = queue[head].time; head++; return cell; } int main() { int n, maxtime; int sumtime = 0; cin >> n; cin >> maxtime; queuecell cell; for (int i = 0; i < n; i++) { cin >> queue[i].name; cin >> queue[i].time; } tail = n - 1; while (tail >= head) { if (queue[head].time > maxtime) { sumtime += maxtime; enqueue(queue[head].name, queue[head].time - maxtime); } else { cell = dequeue(); sumtime += cell.time; printf("%s %d\n", cell.name, sumtime); } } }
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; #define MAX 200000 int tail = 0; int head = 0; typedef struct _queue { char name[10]; int time; } queuecell; queuecell queue[MAX]; void enqueue(char *name, int time) { tail++; strcpy(queue[tail].name, name); queue[tail].time = time; head++; } queuecell dequeue() { queuecell cell; strcpy(cell.name, queue[head].name); cell.time = queue[head].time; head++; return cell; } int main() { int n, maxtime; int sumtime = 0; cin >> n; cin >> maxtime; queuecell cell; for (int i = 0; i < n; i++) { cin >> queue[i].name; cin >> queue[i].time; } tail = n - 1; while (tail >= head) { if (queue[head].time > maxtime) { sumtime += maxtime; enqueue(queue[head].name, queue[head].time - maxtime); } else { cell = dequeue(); sumtime += cell.time; printf("%s %d\n", cell.name, sumtime); } } }
replace
4
5
4
5
0
p02264
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; class Process { public: string name; int time; }; class myqueue { public: int size; int begin; int end; Process v[111111]; myqueue() { size = 111111; begin = 0; end = 0; //[begin, end) } void push(Process a) { v[end] = a; end = (end + 1) % size; } Process front() { return v[begin]; } void pop() { begin++; } bool isEmpty() { return begin == end; } void pr() { for (int i = begin; i < end; i++) { cout << v[i].name << " "; } cout << endl; } }; int main(void) { int n, q; myqueue que; cin >> n >> q; for (int i = 0; i < n; i++) { Process tmp; cin >> tmp.name >> tmp.time; que.push(tmp); } int t = 0; while (!que.isEmpty()) { Process a = que.front(); que.pop(); if (a.time > q) { a.time -= q; que.push(a); t += q; } else { t += a.time; cout << a.name << " " << t << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; class Process { public: string name; int time; }; class myqueue { public: int size; int begin; int end; Process v[111111]; myqueue() { size = 111111; begin = 0; end = 0; //[begin, end) } void push(Process a) { v[end] = a; end = (end + 1) % size; } Process front() { return v[begin]; } void pop() { begin = (begin + 1) % size; } bool isEmpty() { return begin == end; } void pr() { for (int i = begin; i < end; i++) { cout << v[i].name << " "; } cout << endl; } }; int main(void) { int n, q; myqueue que; cin >> n >> q; for (int i = 0; i < n; i++) { Process tmp; cin >> tmp.name >> tmp.time; que.push(tmp); } int t = 0; while (!que.isEmpty()) { Process a = que.front(); que.pop(); if (a.time > q) { a.time -= q; que.push(a); t += q; } else { t += a.time; cout << a.name << " " << t << endl; } } return 0; }
replace
26
27
26
27
0
p02264
C++
Runtime Error
#include <cstdlib> #include <iostream> #include <sstream> #include <stdexcept> #include <stdio.h> #include <string> using namespace std; #define abs(N) ((N > 0) ? N : -1 * N) #define lt(A, B) ((A > B) ? B : A) #define gt(A, B) ((A < B) ? B : A) #define print(S) (cout << S) #define println(S) (cout << S << endl) template <typename T> class queue { public: const static int LEN = 10000; queue<T>() { head = tail = 0; } void empty() { head = tail = 0; } bool isEmpty() { return (head == tail); } bool isFull() { return ((tail + 1) % LEN == head); } T dequeue() { if (isEmpty()) { throw exception(); } if (head == LEN - 1) { head = 0; return body[LEN - 1]; } return body[head++]; } void enqueue(T el) { if (isFull()) { throw exception(); } if (tail == LEN - 1) { tail = 0; body[LEN - 1] = el; } else { body[tail++] = el; } } T top() { if (isEmpty()) { throw exception(); } return body[head]; } private: T body[LEN]; int head, tail; }; typedef struct { string name; int time; } process; int main() { queue<process> que; int n, q; cin >> n >> q; for (int i = 0; i < n; ++i) { process p; cin >> p.name >> p.time; que.enqueue(p); } int t = 0; while (!que.isEmpty()) { process p = que.dequeue(); if (p.time > q) { p.time -= q; que.enqueue(p); t += q; } else { t += p.time; cout << p.name << " " << t << endl; ; } } return 0; }
#include <cstdlib> #include <iostream> #include <sstream> #include <stdexcept> #include <stdio.h> #include <string> using namespace std; #define abs(N) ((N > 0) ? N : -1 * N) #define lt(A, B) ((A > B) ? B : A) #define gt(A, B) ((A < B) ? B : A) #define print(S) (cout << S) #define println(S) (cout << S << endl) template <typename T> class queue { public: const static int LEN = 1000000; queue<T>() { head = tail = 0; } void empty() { head = tail = 0; } bool isEmpty() { return (head == tail); } bool isFull() { return ((tail + 1) % LEN == head); } T dequeue() { if (isEmpty()) { throw exception(); } if (head == LEN - 1) { head = 0; return body[LEN - 1]; } return body[head++]; } void enqueue(T el) { if (isFull()) { throw exception(); } if (tail == LEN - 1) { tail = 0; body[LEN - 1] = el; } else { body[tail++] = el; } } T top() { if (isEmpty()) { throw exception(); } return body[head]; } private: T body[LEN]; int head, tail; }; typedef struct { string name; int time; } process; int main() { queue<process> que; int n, q; cin >> n >> q; for (int i = 0; i < n; ++i) { process p; cin >> p.name >> p.time; que.enqueue(p); } int t = 0; while (!que.isEmpty()) { process p = que.dequeue(); if (p.time > q) { p.time -= q; que.enqueue(p); t += q; } else { t += p.time; cout << p.name << " " << t << endl; ; } } return 0; }
replace
16
17
16
17
0
p02264
C++
Memory Limit Exceeded
#include <iostream> #include <string> using namespace std; struct queue { string name; int time; }; int tail, head; queue Q[10000000]; void enqueue(queue x); queue dequeue(); int main() { int n, q, t = 0, sum = 0; string str; cin >> n >> q; tail = n; head = 0; for (int i = 0; i < n; i++) cin >> Q[i].name >> Q[i].time; while (sum != n) { if (Q[head].time <= q) t += Q[head].time; else t += q; Q[head].time -= q; if (Q[head].time <= 0) { cout << Q[head].name << ' ' << t << endl; head++; sum++; } else enqueue(dequeue()); } return 0; } void enqueue(queue x) { Q[tail++] = x; } queue dequeue() { return Q[head++]; }
#include <iostream> #include <string> using namespace std; struct queue { string name; int time; }; int tail, head; queue Q[1000000]; void enqueue(queue x); queue dequeue(); int main() { int n, q, t = 0, sum = 0; string str; cin >> n >> q; tail = n; head = 0; for (int i = 0; i < n; i++) cin >> Q[i].name >> Q[i].time; while (sum != n) { if (Q[head].time <= q) t += Q[head].time; else t += q; Q[head].time -= q; if (Q[head].time <= 0) { cout << Q[head].name << ' ' << t << endl; head++; sum++; } else enqueue(dequeue()); } return 0; } void enqueue(queue x) { Q[tail++] = x; } queue dequeue() { return Q[head++]; }
replace
10
11
10
11
MLE
p02264
C++
Runtime Error
#include <iostream> #include <queue> #include <string> using namespace std; int main() { int n; int q; int time_cnt = 0; std::queue<string> name; std::queue<int> time; std::string name1[10000]; int time1[10000]; std::string k_name; int k_time; std::cin >> n >> q; for (int i = 0; i < n; i++) { std::cin >> name1[i] >> time1[i]; name.push(name1[i]); time.push(time1[i]); } while (name.size() != 0) { if (time.front() <= q) { time_cnt += time.front(); std::cout << name.front() << " " << time_cnt << '\n'; name.pop(); time.pop(); } else { time.front() -= q; time_cnt += q; k_name = name.front(); k_time = time.front(); name.pop(); time.pop(); name.push(k_name); time.push(k_time); } } return 0; }
#include <iostream> #include <queue> #include <string> using namespace std; int main() { int n; int q; int time_cnt = 0; std::queue<string> name; std::queue<int> time; std::string name1[100000]; int time1[100000]; std::string k_name; int k_time; std::cin >> n >> q; for (int i = 0; i < n; i++) { std::cin >> name1[i] >> time1[i]; name.push(name1[i]); time.push(time1[i]); } while (name.size() != 0) { if (time.front() <= q) { time_cnt += time.front(); std::cout << name.front() << " " << time_cnt << '\n'; name.pop(); time.pop(); } else { time.front() -= q; time_cnt += q; k_name = name.front(); k_time = time.front(); name.pop(); time.pop(); name.push(k_name); time.push(k_time); } } return 0; }
replace
12
14
12
14
0
p02264
C++
Runtime Error
#include <stdio.h> #include <string.h> #define LEN 100005 /* プロセスを表す構造体 */ typedef struct pp { char name[100]; int t; } P; P Q[LEN]; int head, tail, n; void enqueue(P x) { Q[tail] = x; tail = (tail + 1) % LEN; } P dequeue() { P x = Q[head]; head = (head + 1) % LEN; return x; } int min(int a, int b) { return a < b ? a : b; } /* 最小値を返す */ int main() { int elaps = 0, c; int i, q; P u; scanf("%d %d", &n, &q); /* 全てのプロセスをキューに順番に追加する */ for (i = 1; i <= n; i++) { scanf("%s", Q[i].name); scanf("%d", &Q[i].t); } head = 1; tail = n + 1; /* シミュレーション */ while (head != tail) { u = dequeue(); c = min(q, u.t); /* q または必要な時間 u.t だけ処理を行う */ u.t -= c; /* 残りの必要時間を計算 */ elaps += c; /* 経過時間を計算 */ if (u.t > 0) enqueue(u); /* 処理が完了していなければキューに追加 */ else printf("%s %s", u.name, elaps); } return 0; }
#include <stdio.h> #include <string.h> #define LEN 100005 /* プロセスを表す構造体 */ typedef struct pp { char name[100]; int t; } P; P Q[LEN]; int head, tail, n; void enqueue(P x) { Q[tail] = x; tail = (tail + 1) % LEN; } P dequeue() { P x = Q[head]; head = (head + 1) % LEN; return x; } int min(int a, int b) { return a < b ? a : b; } /* 最小値を返す */ int main() { int elaps = 0, c; int i, q; P u; scanf("%d %d", &n, &q); /* 全てのプロセスをキューに順番に追加する */ for (i = 1; i <= n; i++) { scanf("%s", Q[i].name); scanf("%d", &Q[i].t); } head = 1; tail = n + 1; /* シミュレーション */ while (head != tail) { u = dequeue(); c = min(q, u.t); /* q または必要な時間 u.t だけ処理を行う */ u.t -= c; /* 残りの必要時間を計算 */ elaps += c; /* 経過時間を計算 */ if (u.t > 0) enqueue(u); /* 処理が完了していなければキューに追加 */ else printf("%s %d\n", u.name, elaps); } return 0; }
replace
49
50
49
50
-11
p02264
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <vector> using namespace std; #define Rep(b, e, i) for (int i = b; i <= e; i++) #define Repr(e, b, i) for (int i = e; i >= b; i--) #define rep(n, i) Rep(0, n - 1, i) #define repr(n, i) Repr(n - 1, 0, i) #define all(v) (v).begin(), (v).end() #define pb(v) push_back(v) #define uniq(v) (v).erase(unique(all(v)), (v).end()) #define bitcnt(x) __builtin_popcount(x) #define fst first #define snd second typedef pair<string, int> P; void solve(void) { int N, Q; cin >> N >> Q; queue<P> q; rep(N, i) { string name; int t; cin >> name >> t; q.push(P(name, t)); } int acct = 0; while (!q.empty()) { P &p = q.front(); q.pop(); if (p.snd <= Q) { acct += p.snd; cout << p.fst << ' ' << acct << endl; continue; } p.second -= Q; acct += Q; q.push(p); } } int main(void) { solve(); // cout << "yui(*-v・)yui" << endl; return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <vector> using namespace std; #define Rep(b, e, i) for (int i = b; i <= e; i++) #define Repr(e, b, i) for (int i = e; i >= b; i--) #define rep(n, i) Rep(0, n - 1, i) #define repr(n, i) Repr(n - 1, 0, i) #define all(v) (v).begin(), (v).end() #define pb(v) push_back(v) #define uniq(v) (v).erase(unique(all(v)), (v).end()) #define bitcnt(x) __builtin_popcount(x) #define fst first #define snd second typedef pair<string, int> P; void solve(void) { int N, Q; cin >> N >> Q; queue<P> q; rep(N, i) { string name; int t; cin >> name >> t; q.push(P(name, t)); } int acct = 0; while (!q.empty()) { P p = q.front(); q.pop(); if (p.snd <= Q) { acct += p.snd; cout << p.fst << ' ' << acct << endl; continue; } p.second -= Q; acct += Q; q.push(p); } } int main(void) { solve(); // cout << "yui(*-v・)yui" << endl; return 0; }
replace
42
43
42
43
0
p02264
C++
Runtime Error
#include <iostream> #include <utility> #include <vector> using namespace std; struct Process { string name; int time; }; int const MAX = 100000; int head; int tail; Process Q[MAX]; void initalize() { head = tail = 0; } bool isEmpty() { return head == tail; } bool isFull() { return head == (tail + 1) % MAX; } void enqueue(Process x) { if (isFull()) { cout << "エラー(オーバーフロー)" << endl; return; } Q[tail] = x; if (head + 1 == MAX) { head = 0; } else { tail++; } } Process denqueue() { if (isEmpty()) { cout << "エラー(アンダーフロー)" << endl; } Process x = Q[head]; if (head + 1 == MAX) { head = 0; } else { head++; } return x; } int main() { int n, q; cin >> n >> q; for (int i = 0; i < n; ++i) { string name; int time; cin >> name >> time; enqueue(Process{name, time}); } int elapsed_time = 0; while (!isEmpty()) { Process p = denqueue(); if (p.time > q) { elapsed_time += q; p.time -= q; enqueue(p); } else { elapsed_time += p.time; cout << p.name << " " << elapsed_time << endl; } } }
#include <iostream> #include <utility> #include <vector> using namespace std; struct Process { string name; int time; }; int const MAX = 1000000; int head; int tail; Process Q[MAX]; void initalize() { head = tail = 0; } bool isEmpty() { return head == tail; } bool isFull() { return head == (tail + 1) % MAX; } void enqueue(Process x) { if (isFull()) { cout << "エラー(オーバーフロー)" << endl; return; } Q[tail] = x; if (head + 1 == MAX) { head = 0; } else { tail++; } } Process denqueue() { if (isEmpty()) { cout << "エラー(アンダーフロー)" << endl; } Process x = Q[head]; if (head + 1 == MAX) { head = 0; } else { head++; } return x; } int main() { int n, q; cin >> n >> q; for (int i = 0; i < n; ++i) { string name; int time; cin >> name >> time; enqueue(Process{name, time}); } int elapsed_time = 0; while (!isEmpty()) { Process p = denqueue(); if (p.time > q) { elapsed_time += q; p.time -= q; enqueue(p); } else { elapsed_time += p.time; cout << p.name << " " << elapsed_time << endl; } } }
replace
10
11
10
11
0
p02264
C++
Runtime Error
#include <iostream> using namespace std; struct Process { string name; int time; int count; }; class Queue { private: Process store[100000]; int head, tail; Process firstProcess; public: Queue() { head = 0; tail = 0; } void enqueue(string name, int time) { Process p; p.name = name; p.time = time; store[tail] = p; tail++; } Process dequeue() { firstProcess = store[head]; head++; return firstProcess; } }; int main() { Queue q; int n, rr, time, diff, total; string name; Process p; cin >> n >> rr; for (int i = 0; i < n; i++) { cin >> name >> time; q.enqueue(name, time); } total = 0; while (n) { p = q.dequeue(); diff = p.time - rr; if (diff > 0) { total += rr; q.enqueue(p.name, diff); } else { total += p.time; cout << p.name << " " << total << endl; n--; } } return 0; }
#include <iostream> using namespace std; struct Process { string name; int time; int count; }; class Queue { private: Process store[1000000]; int head, tail; Process firstProcess; public: Queue() { head = 0; tail = 0; } void enqueue(string name, int time) { Process p; p.name = name; p.time = time; store[tail] = p; tail++; } Process dequeue() { firstProcess = store[head]; head++; return firstProcess; } }; int main() { Queue q; int n, rr, time, diff, total; string name; Process p; cin >> n >> rr; for (int i = 0; i < n; i++) { cin >> name >> time; q.enqueue(name, time); } total = 0; while (n) { p = q.dequeue(); diff = p.time - rr; if (diff > 0) { total += rr; q.enqueue(p.name, diff); } else { total += p.time; cout << p.name << " " << total << endl; n--; } } return 0; }
replace
11
12
11
12
0
p02264
C++
Runtime Error
#include <cstdio> #include <cstdlib> #include <iostream> using namespace std; #define M 100000 typedef struct PT { char name[11]; int time; } PT; PT a[M + 1]; int head = 0, tail = 0; void Init() { head = tail = 0; } bool IsFull() { return tail - head == -1 || tail - head == M; } bool IsEmpty() { return head == tail; } PT dequeue() { if (IsEmpty()) { cout << "EMPTY" << endl; exit(1); } if (head == M) { head = 0; return a[M]; } ++head; return a[head - 1]; } void enqueue(PT x) { if (IsFull()) { cout << "FULL" << endl; exit(1); } a[tail] = x; ++tail; if (tail == M + 1) tail = 0; } int main() { PT in; int i, n, q, time = 0; cin >> n >> q; for (i = 0; i < n; i++) { // cin>>in.name>>in.time; scanf("%s%d", in.name, &in.time); enqueue(in); } while (!IsEmpty()) { in = dequeue(); if (in.time <= q) { time += in.time; // cout<<in.name<<' '<<time<<endl; printf("%s %d\n", in.time, time); } else { time += q; in.time -= q; enqueue(in); } } return 0; }
#include <cstdio> #include <cstdlib> #include <iostream> using namespace std; #define M 100000 typedef struct PT { char name[11]; int time; } PT; PT a[M + 1]; int head = 0, tail = 0; void Init() { head = tail = 0; } bool IsFull() { return tail - head == -1 || tail - head == M; } bool IsEmpty() { return head == tail; } PT dequeue() { if (IsEmpty()) { cout << "EMPTY" << endl; exit(1); } if (head == M) { head = 0; return a[M]; } ++head; return a[head - 1]; } void enqueue(PT x) { if (IsFull()) { cout << "FULL" << endl; exit(1); } a[tail] = x; ++tail; if (tail == M + 1) tail = 0; } int main() { PT in; int i, n, q, time = 0; cin >> n >> q; for (i = 0; i < n; i++) { // cin>>in.name>>in.time; scanf("%s%d", in.name, &in.time); enqueue(in); } while (!IsEmpty()) { in = dequeue(); if (in.time <= q) { time += in.time; // cout<<in.name<<' '<<time<<endl; printf("%s %d\n", in.name, time); } else { time += q; in.time -= q; enqueue(in); } } return 0; }
replace
50
51
50
51
-11
p02264
C++
Runtime Error
#include <iostream> #include <stdlib.h> #define N_MAX 1000000 using namespace std; typedef struct _a { char name[10]; int time; } P; int main() { int i; int n; int q; int head; int tail; int res_time = 0; char temp[10]; P A[N_MAX]; cin >> n; cin >> q; for (i = 0; i < n; i++) { cin >> A[i].name; cin >> A[i].time; } head = 0; tail = n - 1; while (1) { // process // ??????????????´??° A[head].time -= q; // ???????????? if (A[head].time <= 0) { res_time += q + A[head].time; cout << A[head].name << ' ' << res_time << endl; if (tail == head) { break; } } else { // ?????£?????´??° tail++; if (tail == N_MAX) { tail = 0; } A[tail] = A[head]; res_time += q; } // ?????´??° head++; if (head == N_MAX) { head = 0; } } return 0; }
#include <iostream> #include <stdlib.h> #define N_MAX 99999 using namespace std; typedef struct _a { char name[10]; int time; } P; int main() { int i; int n; int q; int head; int tail; int res_time = 0; char temp[10]; P A[N_MAX]; cin >> n; cin >> q; for (i = 0; i < n; i++) { cin >> A[i].name; cin >> A[i].time; } head = 0; tail = n - 1; while (1) { // process // ??????????????´??° A[head].time -= q; // ???????????? if (A[head].time <= 0) { res_time += q + A[head].time; cout << A[head].name << ' ' << res_time << endl; if (tail == head) { break; } } else { // ?????£?????´??° tail++; if (tail == N_MAX) { tail = 0; } A[tail] = A[head]; res_time += q; } // ?????´??° head++; if (head == N_MAX) { head = 0; } } return 0; }
replace
3
4
3
4
-11
p02264
C++
Memory Limit Exceeded
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <sstream> #include <string> #include <vector> using namespace std; int top = 0; int last = 0; int s[10000010]; string name[10000010]; void push(string a, int n) { s[last] = n; name[last] = a; ++last; } int front_time() { return s[top]; } string front_name() { return name[top]; } void pop() { ++top; } int main() { cin.tie(0); ios::sync_with_stdio(false); int n, q; cin >> n >> q; for (int i = 0; i < n; ++i) { string name; int t; cin >> name >> t; push(name, t); } int ntime = 0; while (top < last) { int a = front_time(); if (a <= q) { ntime += a; cout << front_name() << " " << ntime << endl; pop(); } else { a -= q; ntime += q; push(front_name(), a); pop(); } } return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <sstream> #include <string> #include <vector> using namespace std; int top = 0; int last = 0; int s[1000010]; string name[1000010]; void push(string a, int n) { s[last] = n; name[last] = a; ++last; } int front_time() { return s[top]; } string front_name() { return name[top]; } void pop() { ++top; } int main() { cin.tie(0); ios::sync_with_stdio(false); int n, q; cin >> n >> q; for (int i = 0; i < n; ++i) { string name; int t; cin >> name >> t; push(name, t); } int ntime = 0; while (top < last) { int a = front_time(); if (a <= q) { ntime += a; cout << front_name() << " " << ntime << endl; pop(); } else { a -= q; ntime += q; push(front_name(), a); pop(); } } return 0; }
replace
11
13
11
13
MLE
p02264
C++
Runtime Error
#include <iostream> using namespace std; struct Task { char name[10]; int rtime; }; Task task[100000]; int head, tail, n, q, len = 1000001; void unit() { head = 0; tail = 0; cin >> n >> q; } void enquene(Task t) { task[tail] = t; tail = (tail + 1) % len; } Task dequene() { Task t = task[head]; head = (head + 1) % len; return t; } int main() { unit(); Task t; int ftime = 0; for (int i = 0; i < n; i++) { cin >> t.name; cin >> t.rtime; enquene(t); } while (head != tail) { t = dequene(); if (t.rtime > q) { t.rtime -= q; ftime += q; enquene(t); } else { ftime += t.rtime; cout << t.name << " " << ftime << endl; } } return 0; }
#include <iostream> using namespace std; struct Task { char name[10]; int rtime; }; Task task[100000]; int head, tail, n, q, len = 100000; void unit() { head = 0; tail = 0; cin >> n >> q; } void enquene(Task t) { task[tail] = t; tail = (tail + 1) % len; } Task dequene() { Task t = task[head]; head = (head + 1) % len; return t; } int main() { unit(); Task t; int ftime = 0; for (int i = 0; i < n; i++) { cin >> t.name; cin >> t.rtime; enquene(t); } while (head != tail) { t = dequene(); if (t.rtime > q) { t.rtime -= q; ftime += q; enquene(t); } else { ftime += t.rtime; cout << t.name << " " << ftime << endl; } } return 0; }
replace
9
10
9
10
0
p02264
C++
Memory Limit Exceeded
#include <iostream> #include <string> using namespace std; pair<string, int> D[10000000]; int n, q, b[100000]; string s[100000]; int main() { scanf("%d%d", &n, &q); int cnt = 0, t = 0; for (int i = 0; i < n; i++) { cin >> s[i] >> b[i]; D[cnt] = make_pair(s[i], b[i]); cnt++; } for (int i = 0; i < cnt; i++) { if (D[i].second > q) { t += q; D[cnt] = make_pair(D[i].first, D[i].second - q); cnt++; } else { t += D[i].second; cout << D[i].first << ' ' << t << endl; } } return 0; }
#include <iostream> #include <string> using namespace std; pair<string, int> D[1000000]; int n, q, b[100000]; string s[100000]; int main() { scanf("%d%d", &n, &q); int cnt = 0, t = 0; for (int i = 0; i < n; i++) { cin >> s[i] >> b[i]; D[cnt] = make_pair(s[i], b[i]); cnt++; } for (int i = 0; i < cnt; i++) { if (D[i].second > q) { t += q; D[cnt] = make_pair(D[i].first, D[i].second - q); cnt++; } else { t += D[i].second; cout << D[i].first << ' ' << t << endl; } } return 0; }
replace
3
4
3
4
MLE
p02264
C++
Runtime Error
//============================================================================ // Name : AOJCpp.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <algorithm> #include <iostream> // #include <queue> using namespace std; static const int MAX = 100010; class process { private: string name; int time; public: void setName(string s); void setTime(int t); string getName(); int getTime(); }; void process::setName(string s) { name = s; } void process::setTime(int i) { time = i; } string process::getName() { return name; } int process::getTime() { return time; } class queue { private: int head; int tail; process q[MAX]; public: void initialize(); process front(); void pop(); void push(process p); bool empty(); }; void queue::initialize() { head = 0; tail = 0; } process queue::front() { return q[head]; } void queue::pop() { if (head >= MAX) { head = 0; } else { head++; } } void queue::push(process p) { q[tail] = p; if (tail >= MAX) { tail = 0; } else { tail++; } } bool queue::empty() { if (head == tail) { return true; } return false; } int main() { int n, q; cin >> n >> q; queue que; que.initialize(); string name; int time; for (int i = 0; i < n; i++) { cin >> name; cin >> time; process p; p.setName(name); p.setTime(time); que.push(p); } int total = 0; while (!que.empty()) { process p = que.front(); que.pop(); time = p.getTime(); if (time > q) { p.setTime(time - q); total += q; que.push(p); } else { total += time; // p.setTime(total); cout << p.getName() << " " << total << endl; } } return 0; }
//============================================================================ // Name : AOJCpp.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <algorithm> #include <iostream> // #include <queue> using namespace std; static const int MAX = 500000; class process { private: string name; int time; public: void setName(string s); void setTime(int t); string getName(); int getTime(); }; void process::setName(string s) { name = s; } void process::setTime(int i) { time = i; } string process::getName() { return name; } int process::getTime() { return time; } class queue { private: int head; int tail; process q[MAX]; public: void initialize(); process front(); void pop(); void push(process p); bool empty(); }; void queue::initialize() { head = 0; tail = 0; } process queue::front() { return q[head]; } void queue::pop() { if (head >= MAX) { head = 0; } else { head++; } } void queue::push(process p) { q[tail] = p; if (tail >= MAX) { tail = 0; } else { tail++; } } bool queue::empty() { if (head == tail) { return true; } return false; } int main() { int n, q; cin >> n >> q; queue que; que.initialize(); string name; int time; for (int i = 0; i < n; i++) { cin >> name; cin >> time; process p; p.setName(name); p.setTime(time); que.push(p); } int total = 0; while (!que.empty()) { process p = que.front(); que.pop(); time = p.getTime(); if (time > q) { p.setTime(time - q); total += q; que.push(p); } else { total += time; // p.setTime(total); cout << p.getName() << " " << total << endl; } } return 0; }
replace
12
13
12
13
0
p02264
C++
Runtime Error
//============================================================================ // Name : AOJCpp.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <algorithm> #include <iostream> // #include <queue> using namespace std; static const int MAX = 100001; class process { private: string name; int time; public: void setName(string s); void setTime(int t); string getName(); int getTime(); }; void process::setName(string s) { name = s; } void process::setTime(int i) { time = i; } string process::getName() { return name; } int process::getTime() { return time; } class queue { private: int head; int tail; process q[MAX]; public: void initialize(); process front(); void pop(); void push(process p); bool empty(); }; void queue::initialize() { head = 0; tail = 0; } process queue::front() { return q[head]; } void queue::pop() { if (head >= MAX) { head = 0; } else { head++; } } void queue::push(process p) { q[tail] = p; if (tail >= MAX) { tail = 0; } else { tail++; } } bool queue::empty() { if (head == tail) { return true; } return false; } int main() { int n, q; cin >> n >> q; queue que; que.initialize(); string name; int time; for (int i = 0; i < n; i++) { cin >> name; cin >> time; process p; p.setName(name); p.setTime(time); que.push(p); } int total = 0; while (!que.empty()) { process p = que.front(); que.pop(); time = p.getTime(); if (time > q) { p.setTime(time - q); total += q; que.push(p); } else { total += time; // p.setTime(total); cout << p.getName() << " " << total << endl; } } return 0; }
//============================================================================ // Name : AOJCpp.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <algorithm> #include <iostream> // #include <queue> using namespace std; static const int MAX = 1000000; class process { private: string name; int time; public: void setName(string s); void setTime(int t); string getName(); int getTime(); }; void process::setName(string s) { name = s; } void process::setTime(int i) { time = i; } string process::getName() { return name; } int process::getTime() { return time; } class queue { private: int head; int tail; process q[MAX]; public: void initialize(); process front(); void pop(); void push(process p); bool empty(); }; void queue::initialize() { head = 0; tail = 0; } process queue::front() { return q[head]; } void queue::pop() { if (head >= MAX) { head = 0; } else { head++; } } void queue::push(process p) { q[tail] = p; if (tail >= MAX) { tail = 0; } else { tail++; } } bool queue::empty() { if (head == tail) { return true; } return false; } int main() { int n, q; cin >> n >> q; queue que; que.initialize(); string name; int time; for (int i = 0; i < n; i++) { cin >> name; cin >> time; process p; p.setName(name); p.setTime(time); que.push(p); } int total = 0; while (!que.empty()) { process p = que.front(); que.pop(); time = p.getTime(); if (time > q) { p.setTime(time - q); total += q; que.push(p); } else { total += time; // p.setTime(total); cout << p.getName() << " " << total << endl; } } return 0; }
replace
12
13
12
13
0