2977 二叉堆练习1 codevs

题目描述 Description

已知一个二叉树,判断它是否为二叉堆(小根堆)

输入描述 Input Description

二叉树的节点数N和N个节点(按层输入)

输出描述 Output Description

YES或NO

样例输入 Sample Input

样例输入1

3

1 4 9

样例输入2

3

6 4 9

样例输出 Sample Output

样例输出1

YES

样例输出2

NO

数据范围及提示 Data Size & Hint

对于20%的数据  N≤20

对于50%的数据  N≤1000

对于100%的数据 N≤50000,每个节点≤10000

#include <cstdio>
#include <iostream>
using namespace std;
int n,tree[60000];
bool pq;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    scanf("%d",&tree[i]);
    for(int i=1;i<=n;i++)
    for(int j=i;j<=n;j=j*2)
    if(tree[i]>tree[j])
    {
        pq=1;
        break;
    }
    if(pq==true)
    printf("NO");
    else
    printf("YES");
    return 0;
}
原文地址:https://www.cnblogs.com/sssy/p/6664992.html