AcWing 1640. 堆
原题链接
简单
作者:
heiyou
,
2020-04-20 10:19:22
,
所有人可见
,
阅读 550
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 110, M = 2020;
int m, n;
int Heap[N][M];
bool t;
void dfs(int i, int j){
if(Heap[i][j] != 0x3f3f3f3f){
dfs(i, 2 * j + 1);
dfs(i, 2 * j + 2);
if(t) cout << " " << Heap[i][j];
else { cout << Heap[i][j], t = true; }
}
}
int main()
{
scanf("%d%d", &m, &n);
memset(Heap, 0x3f, sizeof Heap);
for(int i = 0; i < m; i ++)
{
for(int j = 0; j < n; j ++)
scanf("%d", &Heap[i][j]);
}
for(int i = 0; i < m; i ++)
{
//首先假定是大顶堆
bool flag = true;
t = false;
for(int j = 0; j < n; j ++){
if(Heap[i][j * 2 + 1] > Heap[i][j] && Heap[i][j * 2 + 1] != 0x3f3f3f3f) {flag = false; break;}
if(Heap[i][j * 2 + 2] > Heap[i][j] && Heap[i][j * 2 + 2] != 0x3f3f3f3f) {flag = false; break;}
}
if(flag) puts("Max Heap"); //大顶堆,后序遍历
else //flag == false, 测试小顶堆
{
for(int j = 0; j < n; j ++){
if(Heap[i][j * 2 + 1] < Heap[i][j] && Heap[i][j * 2 + 1] != 0x3f3f3f3f) {flag = true; break;}
if(Heap[i][j * 2 + 2] < Heap[i][j] && Heap[i][j * 2 + 2] != 0x3f3f3f3f) {flag = true; break;}
}
if(!flag) puts("Min Heap");
else puts("Not Heap");
}
dfs(i, 0);
printf("\n");
}
return 0;
}
pat真蛋疼,每次输出都要考虑空格