堆的实现andprioity-queue函数

来源:互联网 发布:培罗成西服淘宝店 编辑:程序博客网 时间:2024/06/09 18:32

int heap[maxn],sz=0;

void push(int x)

{

int I=sz++;

while(I>0)

{

int p=(I-1)/2;

if(head[p]<=x)

break;

head[I]=head[p];

I=p;

}

head[I]=x;

}

int pop()

{

int ret=heap[0];

int x=head[--sz];

int I=0;

while(I*2+1<sz)

{

int a=I*2+1,b=I*2+2;

if(b<sz&&head[b]<head[a]) a=b;

if(head[a]>=x) break;

head[I]=head[a];

I=a;

}

head[I]=x;

return ret;

}


priorty_queue函数

#include<queue>

#include<cstdio>

using namespace std;

int main()

{

priorty_queue<int>pque;

pque.push(3);

pque.push(5);

pque.push(1);

while(!pque.empty())

{

printf("%d\n",pque.top());

pque.pop();

}

return 0;

}

0 0