#include <iostream>
#include <stdio.h>
#define Max 1000
using namespace std;
//大顶堆
int heap[Max];//堆的存放数组
void down_adjust(int x)//将x向下调节
{
int temp=heap[x];
int y=2*x;
while(y<=heap[0])
{
if(y+1<=heap[0]&&heap[y]<heap[y+1])//若有右儿子且右儿子比左儿子大则将y指向右儿子
y++;//y是用来指向两个儿子最大值的
if(heap[y]<=temp)
break;//没顺序颠倒时结束
heap[y/2]=heap[y];
y=y*2;//继续向下调节
}
heap[y/2]=temp;
}
void up_adjust(int x)//将x向上调节
{
int temp=heap[x];
int y=x;
while(y/2>=1)
{
if(heap[y/2]>=temp)//没有顺序颠倒是直接颠倒
break;
heap[y]=heap[y/2];
y=y/2;
}
heap[y]=temp;
}
void init()//建堆
{
for(int i=heap[0]/2; i>=1; i--)
down_adjust(i);
}
int pop()//弹堆
{
int temp=heap[1];
heap[1]=heap[heap[0]];
heap[0]--;
down_adjust(1);
return temp;
}
void push(int x)//将x压入堆内
{
heap[0]++;
heap[heap[0]]=x;
up_adjust(heap[0]);
}