2977 binary heap exercises
2977 binary heap Exercise 1
Time Limit: 10 s space limit: 32000 KB title level: Silver Title Description
Description
Determine whether a binary tree is a binary heap (small root heap)
Input description
Input Description
Number of nodes in a binary tree N and N (input by layer)
Output description
Output Description
YES or NO
Sample Input
Sample Input
Example input 1
3
1 4 9
Sample input 2
3
6 4 9
Sample output
Sample Output
Sample output 1
YES
Sample output 2
NO
Data range and prompt
Data Size & Hint
For 20% of data, N ≤ 20
For 50% of data, N ≤ 1000
For 100% of data, N ≤ 50000, each node ≤ 10000
CATEGORY tag
Tags click here to expand
1 #include<iostream> 2 using namespace std; 3 long long int a[100001]; 4 int main() 5 { 6 int n; 7 cin>>n; 8 for(int i=1;i<=n;i++) 9 {10 cin>>a[i];11 }12 int flag=0;13 for(int i=1;i<=n;i++)14 {15 if((a[i*2]<a[i]&&a[i*2]!=0)||(a[i*2+1]<a[i]&&a[i*2+1]!=0))16 {17 flag=1;18 break;19 }20 }21 if(flag==1)22 {23 cout<<"NO";24 }25 else26 {27 cout<<"YES";28 }29 return 0;30 }