AcWing 1640. 堆
原题链接
简单
作者:
Value
,
2020-06-01 21:10:06
,
所有人可见
,
阅读 479
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 1010;
int heap[N];
int n, m;
void print(int t){
if(t * 2 <= m) print(t * 2);
if(t * 2 + 1 <= m) print(t * 2 + 1);
if(t <= m) cout << heap[t];
if(t == 1) cout << endl; // 控制一下格式输出
else cout << " ";
}
bool isMin_heap(){
for(int i = m / 2; i; i -- ){
if(i * 2 <= m && heap[i] > heap[i * 2]) return false;
if(i * 2 + 1 <= m && heap[i] > heap[i * 2 + 1]) return false;
}
return true;
}
bool isMax_heap(){
for(int i = m / 2; i; i -- ){
if(i * 2 <= m && heap[i] < heap[i * 2]) return false;
if(i * 2 + 1 <= m && heap[i] < heap[i * 2 + 1]) return false;
}
return true;
}
int main(){
cin >> n >> m;
while(n -- ){
for(int i = 1; i <= m; i ++ ) scanf("%d", &heap[i]);
if(isMin_heap()) cout << "Min Heap" << endl;
else if(isMax_heap()) cout << "Max Heap" << endl;
else cout << "Not Heap" << endl;
print(1);
}
return 0;
}