-
Notifications
You must be signed in to change notification settings - Fork 0
/
1147.cpp
56 lines (50 loc) · 1.36 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
52
53
54
55
56
#include<iostream>
#include<algorithm>
#define MMAX 100000
using namespace std;
int left(int root) {
return root * 2 + 1;
}
int right(int root) {
return root * 2 + 2;
}
bool compare(int a, int b, int cmp) {
if (cmp) return a <= b;
else return a >= b;
}
bool judge(int * data, int root, int hNum, bool cmp) {
bool flag = true;
if (left(root) < hNum) {
if (!compare(data[root], data[left(root)], cmp)) return false;
flag = flag && judge(data, left(root), hNum, cmp);
}
if (right(root) < hNum) {
if (!compare(data[root], data[right(root)], cmp)) return false;
flag = flag && judge(data, right(root), hNum, cmp);
}
return flag;
}
bool s;
void print(int * data, int root, int hNum) {
if (left(root) < hNum) print(data, left(root), hNum);
if (right(root) < hNum) print(data, right(root), hNum);
if (s) s = false;
else cout << " ";
cout << data[root];
}
int main() {
int n, hNum;
cin >> n >> hNum;
for (int i = 0; i < n; i++) {
int data[MMAX];
for (int h = 0; h < hNum; h++) {
cin >> data[h];
}
if (judge(data, 0, hNum, false)) cout << "Max Heap" << endl;
else if (judge(data, 0, hNum, true)) cout << "Min Heap" << endl;
else cout << "Not Heap" << endl;
s = true;
print(data, 0, hNum);
cout << endl;
}
}