1.手写小根堆:
#include <cstdio>
#include <algorithm>
#define maxn 1000003
using namespace std;
int heap[maxn];
int tot=0;
inline void pushup(int x)
{
for(;x>0;x>>=1)
{
if(heap[x]>heap[x>>1]) break;
swap(heap[x],heap[x>>1]);
}
}
inline void pushdown(int x)
{
for(int y;x<=tot;)
{
y=x<<1;
if(y>tot) break;
if(heap[y|1]<heap[y]&&y<tot) y|=1;
if(heap[x]<heap[y]) break;
swap(heap[x],heap[y]);
x=y;
}
}
inline void push(int x)
{
heap[++tot]=x;
pushup(tot);
}
inline int top()
{
return heap[1];
}
inline void pop()
{
swap(heap[1],heap[tot--]);
pushdown(1);
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int opt,x;
scanf("%d",&opt);
switch(opt)
{
case 1: {scanf("%d",&x);push(x);break;}
case 2: {printf("%d\n",top());break;}
case 3: {pop();break;}
default:break;
}
}
return 0;
}
2.STL的priority_queue
#include <queue>
using namespace std;
struct node{
int a,b,c;
bool operator < (const node &x) const{
return a.x<x;
}
};
priority_queue<int>q1;
priority_queue<int,vector<int>,greater<int> >q2
priority_queue<node>q3;