[模板]优先队列(堆)
洛谷P3378
注意:优先队列的定义不能打错了!
1 #include<cstdio> 2 #include<queue> 3 using namespace std; 4 struct cmp{ //注意它的格式! 一些编译器在格式错误的情况下(比如我的)仍然可以编译并运行且得到正确结果,提交时当然是CE了 5 bool operator () (const int x,const int y) const{ 6 return x>y; //小根堆 7 } 8 }; 9 priority_queue <int,vector<int>,cmp> q; 10 //当然你可以用这种 priority_queue <int,vector<int>,greater<int> > q; 11 inline int read() //读入优化 12 { 13 char c=getchar(); int res=0; 14 while(c<'0'||c>'9') c=getchar(); 15 while(c>='0'&&c<='9') res=res*10+c-'0',c=getchar(); 16 return res; 17 } 18 int main() 19 { 20 int n,i,p1,p2; 21 n=read(); 22 for(i=1;i<=n;i++) 23 { 24 p1=read(); 25 if(p1==1) //插入 26 { 27 p2=read(); q.push(p2); 28 }else if(p1==2) //读出最小的数 29 { 30 printf("%d\n",q.top()); 31 }else if(p1==3) 32 { 33 q.pop(); //删除优先级最高的 34 } 35 } 36 return 0; 37 }
欢迎大家吐槽或评论,如要转载请注明地址,谢谢~(虽然不一定有人能看到。。。)