#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,j;
int b,c;
b = random() % 100 + 1;
n = 10;
insert(b, a, &n);
c = random() % 100 + 1;
n = 11;
insert(c, a, &n);
printf("%d\n", check_heap
(a
, 12));
for(i = 0; i < 12; i++){
}
deletemin(a, &n);
for(j = 0; j < 11; j++){
}
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 temp;
int i,j,k;
i = 0;
b = a[0];
while(a[i] > 0)
{
k = a[2*i+1] - a[2*i+2];
if(k < 0){
temp = a[2*i+1];
a[2*i+1] = a[i];
a[i] = temp;
i = 2*i+1;
}
if(k > 0)
{
temp = a[2*i+2];
a[2*i+2] = a[i];
a[i] = temp;
i = 2*i+2;
}
}
return b;
}
CiNpbmNsdWRlPHN0ZGlvLmg+CiNpbmNsdWRlPHN0ZGxpYi5oPgojaW5jbHVkZTx0aW1lLmg+CmludCBjaGVja19oZWFwKGludCBhW10sIGludCBuKTsKdm9pZCBpbnNlcnQoaW50IHZhbCwgaW50IGFbXSwgaW50ICpuKTsKaW50IGRlbGV0ZW1pbihpbnQgYVtdLGludCAqbik7CiAKaW50IG1haW4odm9pZCkgewogIGludCBhWzEyXSA9IHsxLDEzLDcxLDE0LDE1LDgwLDkxLDI0LDYwLDYzfTsKICBpbnQgbjsKICBpbnQgaSxqOwogIGludCBiLGM7CgogIHNyYW5kb20odGltZShOVUxMKSk7CiAgCiAgYiA9IHJhbmRvbSgpICUgMTAwICsgMTsKICBuID0gMTA7CiAgaW5zZXJ0KGIsIGEsICZuKTsKICAKCiAgYyA9IHJhbmRvbSgpICUgMTAwICsgMTsKICBuID0gMTE7Cmluc2VydChjLCBhLCAmbik7CiAKICBwcmludGYoIiVkXG4iLCBjaGVja19oZWFwKGEsIDEyKSk7CgogCiAgZm9yKGkgPSAwOyBpIDwgMTI7IGkrKyl7CiAgICBwcmludGYoIiUzZCIsYVtpXSk7CgogIH0KCiAgCiAgZGVsZXRlbWluKGEsICZuKTsKICBmb3IoaiA9IDA7IGogPCAxMTsgaisrKXsKCiAgICBwcmludGYoIiUzZCIsYVtqXSk7CgogICAgfQoKICByZXR1cm4gMDsKfQogCmludCBjaGVja19oZWFwKGludCBhW10sIGludCBuKSB7CiAgaW50IGk7CiAgaW50IG07CiAgbSA9IChuIC0gMSkgLyAyOwogIGZvcihpID0gMDsgaSA8IG07IGkrKykKICAgIHsKICAgICAgaWYoYVtpXSA+PSBhW2kgKiAyICsgMV0pCgl7CgkgIHJldHVybiAwOwoJfQogICAgICBpZihhW2ldID49IGFbaSAqIDIgKyAyXSl7CglyZXR1cm4gMDsKICAgICAgfQogICAgfQogCgogIGlmKG4gJSAyID09IDApewoKICAgIGlmKGFbKG4gLSAxKS8yXSA+IGFbbiAtIDFdKXsKCiAgICAgIHJldHVybiAwOwogICAgfQogIH0KICByZXR1cm4gMTsKCn0KIAp2b2lkIGluc2VydChpbnQgdmFsLCBpbnQgYVtdLCBpbnQgKm4pIHsKCiAgaW50IHRlbXA7CiAgaW50IGk7CgogIGFbKm5dID0gdmFsOwogIGkgPSAqbjsKCiAgd2hpbGUoIDAgPCBpKXsKICAgICAKICAgIGlmKGFbKGktMSkvMl0gPD0gYVtpXSkKICAgICAgewoJYnJlYWs7CiAgICAgIH0KCiAgICB0ZW1wID0gIGFbKGktMSkgLyAyXTsKICAgIGFbKGktMSkgLyAyXSA9IGFbaV07CiAgICBhW2ldID0gIHRlbXA7CiAgICBpID0gKGkgLSAxKS8yOwogCgogIH0KCn0KIAppbnQgZGVsZXRlbWluKGludCBhW10saW50ICpuKXsKCiAgaW50IGI7CiAgaW50IHRlbXA7ICAKaW50IGksaixrOwogIGkgPSAwOwogIGIgPSBhWzBdOwogIHdoaWxlKGFbaV0gPiAwKQogICAgewogCiAgICAgIGsgPSBhWzIqaSsxXSAtIGFbMippKzJdOwogICAgICBpZihrIDwgMCl7CiAgdGVtcCA9ICBhWzIqaSsxXTsKICAgIGFbMippKzFdID0gYVtpXTsKICAgIGFbaV0gPSAgdGVtcDsKICAgIGkgPSAyKmkrMTsKIAoKCX0KCWlmKGsgPiAwKQoJICB7CgogIHRlbXAgPSAgYVsyKmkrMl07CiAgICBhWzIqaSsyXSA9IGFbaV07CiAgICBhW2ldID0gIHRlbXA7CiAgICBpID0gMippKzI7CiAgICAKCgkgIH0KCgkKCiAgICB9CgogIAogCiAgcmV0dXJuIGI7Cn0K