题解 P3378 【【模板】堆】

Update 18.2.27----想当年我还用着C.....

看到题解里一堆用C++ STL库中的优先队列,身为C语言选手心里不是滋味

故手打一个优先队列献给坚守在C语言的选手

#include <stdio.h>
#define maxn 1000050
int que1[maxn];
int head=1,tail=0;
int n;
int get()
{
    short int neg=0;int a;char c;
    while((c=getchar())>'9'||c<'0') neg=c=='-';
    a=c-48;
    while((c=getchar())>='0'&&c<='9')a=(a<<3)+(a<<1)+c-48;
    return neg?-a:a;
}
int main()
{
    int i,j,x,y;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
           x=get();
           if(x==1)
           {
                y=get();
                j=tail;
             while(head<=j&&y<que1[j]){que1[j+1]=que1[j];j--;}//实际操作中有时可以直接tail--
             tail++;     
             que1[++j]=y;
           }
           else if(x==2) printf("%d
",que1[head]);
           else if(x==3) head++;
    }
    return 0;
}
祝大家 NOIP 2017 rp++
原文地址:https://www.cnblogs.com/Rye-Catcher/p/8478920.html