-
Notifications
You must be signed in to change notification settings - Fork 0
/
1098.cpp
50 lines (41 loc) · 1.1 KB
/
1098.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
49
50
#include <algorithm>
#include <vector>
#include <cstdio>
void downAdjust(std::vector<int> &b, int low, int high) {
int i = 1, j = i * 2;
while(j <= high) {
if(j + 1 <= high && b[j] < b[j + 1]) j = j + 1;
if (b[i] >= b[j]) break;
std::swap(b[i], b[j]);
i = j; j = i * 2;
}
}
int main() {
int n, p = 2;
scanf("%d", &n);
std::vector<int> a(n + 1), b(n + 1);
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for(int i = 1; i <= n; i++) {
scanf("%d", &b[i]);
}
while(p <= n && b[p - 1] <= b[p]) p++;
int index = p;
while(p <= n && a[p] == b[p]) p++;
if(p == n + 1) {
printf("Insertion Sort\n");
std::sort(b.begin() + 1, b.begin() + index + 1);
} else {
printf("Heap Sort\n");
p = n;
while(p > 2 && b[p] >= b[1]) p--;
std::swap(b[1], b[p]);
downAdjust(b, 1, p - 1);
}
for (int i = 1; i <= n; i++) {
if (i == n) printf("%d\n", b[i]);
else printf("%d ", b[i]);
}
return 0;
}