1147 Heaps (30 分)给出序列判断最大/最小堆
作者:互联网
题意:给出序列判断是否为最大堆/最小堆,并输出后序遍历序列
#include<bits/stdc++.h>
using namespace std;
int n,m;
int a[10005];
int k[10005];
int sum1,sum2,sum;
vector<int> v;
void dfs(int index,int cnt)
{
if(index>n)
{
int p=k[0],f=k[0],p1=0,f1=0;
for(int i=1;i<cnt;i++)
{
if(p<=k[i])
{
p1++;
p=k[i];
}
if(f>=k[i])
{
f1++;
f=k[i];
}
}
if(p1==cnt-1)
sum1++;
if(f1==cnt-1)
sum2++;
sum++;
return ;
}
k[cnt]=a[index];
dfs(index*2,cnt+1); //左
dfs(index*2+1,cnt+1); //右
v.push_back(a[index]); //根
}
int main()
{
cin>>m>>n;
while(m--)
{
v.clear();
sum1=0;
sum2=0;
sum=0;
for(int i=1;i<=n;i++)
cin>>a[i];
dfs(1,0);
if(sum1==sum) //若所有从根出发向下的序列均是递增的
cout<<"Min Heap"<<endl;
if(sum2==sum) //若所有从根出发向下的序列均是递减的
cout<<"Max Heap"<<endl;
if(sum1!=sum && sum2!=sum)
cout<<"Not Heap"<<endl;
for(int i=0;i<v.size();i++)
{
if(i==0)
cout<<v[i];
else
cout<<" "<<v[i];
}
cout<<endl;
}
}
标签:Heaps,index,cnt,1147,int,sum,30,sum1,++ 来源: https://blog.csdn.net/henulmh/article/details/114166288