• 优先队列


     1 #include<cstdio>
     2 #include<queue>
     3 using namespace std;
     4 
     5 struct cmp1   //小到大
     6 {
     7     bool operator () (int &a, int &b)
     8     {
     9         return a>b;
    10     }
    11 };
    12 
    13 struct cmp2   //大到小
    14 {
    15     bool operator () (int &a, int &b)
    16     {
    17         return a<b;
    18     }
    19 };
    20 
    21 struct num1
    22 {
    23     int x;
    24     bool operator < (const num1 a) const
    25     {
    26         return x>a.x;
    27     }
    28 };
    29 
    30 struct num2
    31 {
    32     int x;
    33     bool operator < (const num2 a) const
    34     {
    35         return x<a.x;
    36     }
    37 };
    38 
    39 int main()
    40 {
    41     priority_queue<int,vector<int>, cmp1> q1;
    42     priority_queue<int,vector<int>, cmp2> q2;
    43     priority_queue<num1> q3;
    44     priority_queue<num2> q4;
    45 
    46     int n;
    47     while(scanf("%d", &n)==1)
    48     {
    49         int x;
    50         num1 x1;
    51         num2 x2;
    52         for(int i=0; i<n; i++)
    53         {
    54             scanf("%d", &x);
    55             x1.x=x;
    56             x2.x=x;
    57             q1.push(x);
    58             q2.push(x);
    59             q3.push(x1);
    60             q4.push(x2);
    61         }
    62 
    63         while(!q1.empty())
    64         {
    65             printf("%d ", q1.top());
    66             q1.pop();
    67         }
    68         puts("");
    69 
    70         while(!q2.empty())
    71         {
    72             printf("%d ", q2.top());
    73             q2.pop();
    74         }
    75         puts("");
    76 
    77         while(!q3.empty())
    78         {
    79             printf("%d ", q3.top().x);
    80             q3.pop();
    81         }
    82         puts("");
    83 
    84         while(!q4.empty())
    85         {
    86             printf("%d ", q4.top().x);
    87             q4.pop();
    88         }
    89         puts("");
    90     }
    91     return 0;
    92 }
  • 相关阅读:
    删除input上传的文件路径
    Atom的追踪函数插件和自定义语法
    配置虚拟域名,hosts文件起作用
    django--博客系统--后台管理
    django--个人主页建立练习
    django--博客--forms组件-用户注册
    django--之登录表单提交
    django--mysql设置
    django之中间件
    django之cookie与session
  • 原文地址:https://www.cnblogs.com/qyy-goodluck/p/4370898.html
Copyright © 2020-2023  润新知