AcWing 1649. 优化来优化去,还是y总的代码最简单啊
原题链接
中等
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1002;
int q[N];
int n;
bool max_heap,min_heap;
vector<vector<int>> res;
vector<int> temp;
void dfs(int root)
{
temp.push_back(q[root]);
if(root*2>n)
{
res.push_back(temp);
temp.pop_back();
return ;
}
if(root*2+1<=n)
dfs(root*2+1);
if(root*2<=n)
dfs(root*2);
temp.pop_back();
}
void check(vector<int> a)
{
int size=a.size();
if(size==1) return ;
for(int i=1;i<a.size();i++)
if(a[i]<a[i-1]) max_heap=true;
else min_heap=true;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++) cin>>q[i];
dfs(1);
for(auto i:res)
{
for(auto j:i)
cout<<j<<' ';
cout<<endl;
}
for(auto i:res)
check(i);
if(max_heap&&min_heap) cout<<"Not Heap";
else if(!max_heap) cout<<"Min Heap";
else cout<<"Max Heap";
return 0;
}