-
Notifications
You must be signed in to change notification settings - Fork 0
/
1147.cpp
51 lines (40 loc) · 1.22 KB
/
1147.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
51
#include <cstdio>
#include <vector>
const int MAXN = 1000 + 10;
int a[MAXN];
int judge(int a[], int size) {
int flag = a[1] > a[2] ? 1 : -1;
for (int i = 1; i <= size / 2; i++) {
int l = i * 2, r = i * 2 + 1;
if (flag == 1 && (a[l] > a[i] || (r <= size && a[r] > a[i]))) return 3;
else if (flag == -1 && (a[l] < a[i] ||(r <= size && a[r] < a[i]))) return 3;
}
if (flag == 1) return 1;
else if (flag == -1) return 2;
}
std::vector<int> ans;
void postOrder(int a[], int root, int size) {
if (root > size) return;
postOrder(a, root * 2, size);
postOrder(a, root * 2 + 1, size);
ans.push_back(a[root]);
}
int main() {
int m, n;
scanf("%d%d", &m, &n);
while (m--) {
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
if (judge(a, n) == 1) printf("Max Heap\n");
else if (judge(a, n) == 2) printf("Min Heap\n");
else if (judge(a, n) == 3) printf("Not Heap\n");
postOrder(a, 1, n);
for (int i = 0; i < n; i++) {
if (i == n - 1) printf("%d\n", ans[i]);
else printf("%d ", ans[i]);
}
ans.clear();
}
return 0;
}