-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy path1089. Insert or Merge.cpp
48 lines (48 loc) · 1.32 KB
/
1089. Insert or Merge.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <bits/stdc++.h>
using namespace std;
using gg = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
gg ni;
cin >> ni;
vector<gg> v1(ni), v2(ni);
for (gg& i : v1) {
cin >> i;
}
for (gg& i : v2) {
cin >> i;
}
vector<vector<gg>> inserts, merges; //存储排序的每趟结果
vector<gg> v = v1;
for (gg i = 2; i <= ni; ++i) { //插入排序
sort(v.begin(), v.begin() + i);
inserts.push_back(v);
}
v = v1;
for (gg i = 2; i <= ni; i *= 2) { //归并排序
for (gg j = 0; j < ni; j += i) {
sort(v.begin() + j, v.begin() + min(j + i, ni));
}
merges.push_back(v);
}
for (gg i = 0; i < inserts.size(); ++i) { //确认是否是插入排序
if (inserts[i] == v2) {
cout << "Insertion Sort\n";
for (gg j = 0; j < ni; ++j) {
cout << inserts[i + 1][j] << (j == ni - 1 ? "\n" : " ");
}
break;
}
}
for (gg i = 0; i < merges.size(); ++i) { //确认是否是归并排序
if (merges[i] == v2) {
cout << "Merge Sort\n";
for (gg j = 0; j < ni; ++j) {
cout << merges[i + 1][j] << (j == ni - 1 ? "\n" : " ");
}
break;
}
}
return 0;
}