#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int check_heap(int a[], int n);
void insert(int val, int a[], int *n);
int deletemin(int a[],int *n);
int main(void) {
int a[12] = {1,13,71,14,15,80,91,24,60,63};
int n;
int i;
int b,c;
b = random() % 100 + 1;
n = 10;
insert(b, a, &n);
n = 11;
c = random() % 100 + 1;
insert(c, a, &n);
printf("%d\n", check_heap
(a
, 12));
deletemin(a, &n);
return 0;
}
int check_heap(int a[], int n) {
int i;
int m;
m = (n - 1) / 2;
for(i = 0; i < m; i++)
{
if(a[i] >= a[i * 2 + 1])
{
return 0;
}
if(a[i] >= a[i * 2 + 2]){
return 0;
}
}
if(n % 2 == 0){
if(a[(n - 1)/2] > a[n - 1]){
return 0;
}
}
return 1;
}
void insert(int val, int a[], int *n) {
int temp;
int i;
a[*n] = val;
i = *n;
while( 0 < i){
if(a[(i-1)/2] <= a[i])
{
break;
}
temp = a[(i-1) / 2];
a[(i-1) / 2] = a[i];
a[i] = temp;
i = (i - 1)/2;
}
}
int deletemin(int a[],int *n){
int b;
int i,j,k;
b = a[0];
return b;
}
I2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGU8c3RkbGliLmg+CiNpbmNsdWRlPHRpbWUuaD4KaW50IGNoZWNrX2hlYXAoaW50IGFbXSwgaW50IG4pOwp2b2lkIGluc2VydChpbnQgdmFsLCBpbnQgYVtdLCBpbnQgKm4pOwppbnQgZGVsZXRlbWluKGludCBhW10saW50ICpuKTsKIAppbnQgbWFpbih2b2lkKSB7CiAgaW50IGFbMTJdID0gezEsMTMsNzEsMTQsMTUsODAsOTEsMjQsNjAsNjN9OwogIGludCBuOwogIGludCBpOwogIGludCBiLGM7CgogIHNyYW5kb20odGltZShOVUxMKSk7CiAgCiAgYiA9IHJhbmRvbSgpICUgMTAwICsgMTsKICBuID0gMTA7CiAgaW5zZXJ0KGIsIGEsICZuKTsKICBuID0gMTE7CiAgYyA9IHJhbmRvbSgpICUgMTAwICsgMTsKICBpbnNlcnQoYywgYS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