1098: 堆的判断
题目链接-1098: 堆的判断
解题思路
小根堆:小根堆是一个特殊的二叉树,他的父亲节点都不大于两个儿子节点,且他的左右子树也是小根堆
- 如果有一个关键字的集合 K = k 1 , k 2 , . . . , k n K={k_1,k_2, ..., k_n} K=k1,k2,...,kn, 把所有元素按完全二叉树的顺序存储方式存放在一个一维数组中,并且满足 k i < = k 2 i + 1 k_i<=k_{2i+1} ki<=k2i+1且 k i < = k 2 i k_i<=k_{2i} ki<=k2i,则称这个集合为小根堆
- 依次判断该点的左右孩子是否大于该点,如果该点的左右孩子大于该点 ,则不是小根堆
- 具体操作见代码
附上代码
#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include<bits/stdc++.h>
#define int long long
#define lowbit(x) (x &(-x))
#define endl '\n'
using namespace std;
const int INF=0x3f3f3f3f;
const int dir[4][2]={-1,0,1,0,0,-1,0,1};
const double PI=acos(-1.0);
const double e=exp(1.0);
const double eps=1e-10;
const int M=1e9+7;
const int N=2e5+10;
typedef long long ll;
typedef pair<int,int> PII;
typedef unsigned long long ull;
int a[N];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
bool ass=0;
for(int i=1;i<=n/2;i++){
if((a[i]>a[i<<1]||a[i]>a[i<<1|1])&&(i<<1)<=n&&(i<<1|1)<=n){
ass=1;
break;
}
}
if(ass) cout<<"No";
else cout<<"Yes";
return 0;
}