zoukankan      html  css  js  c++  java
  • [模板]优先队列(堆)

    洛谷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
    ",q.top());       
    31         }else if(p1==3)
    32         {
    33             q.pop();         //删除优先级最高的
    34         }
    35     }
    36     return 0;
    37 }
  • 相关阅读:
    js加密
    sharepoint更新左侧列表的名字
    HTML转换JS
    Html空格字符代码:
    docker 与host互传文件
    Ubuntu里node命令出错,找不到
    docker查看运行容器详细信息
    docker保存容器的修改
    Docker容器中安装新的程序
    运行docker容器镜像
  • 原文地址:https://www.cnblogs.com/Slager-Z/p/7769314.html
Copyright © 2011-2022 走看看