码迷,mamicode.com
首页 > 其他好文 > 详细

【模板】小根堆

时间:2017-10-27 22:28:09      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:表示   top   reg   lib   gif   using   nbsp   algorithm   手写   

因为根的实现方法(优先队列)默认为大根堆,即从大到小排列,所以在需要的时候需要手写小根堆。

题目描述

如题,初始小根堆为空,我们需要支持以下3种操作:

操作1: 1 x 表示将x插入到堆中

操作2: 2 输出该小根堆内的最小数

操作3: 3 删除该小根堆内的最小数

输入输出格式

输入格式:

 

第一行包含一个整数N,表示操作的个数

接下来N行,每行包含1个或2个正整数,表示三种操作,格式如下:

操作1: 1 x

操作2: 2

操作3: 3

 

输出格式:

 

包含若干行正整数,每行依次对应一个操作2的结果。

 

输入输出样例

输入样例#1: 复制
5
1 2
1 5
2
3
2
输出样例#1: 复制
2
5

说明

时空限制:1000ms,128M

数据规模:

对于30%的数据:N<=15

对于70%的数据:N<=10000

对于100%的数据:N<=1000000(注意是6个0。。。不过不要害怕,经过编者实测,堆是可以AC的)

样例说明:

技术分享

故输出为2、5

技术分享
 1 #include <iostream>
 2 #include <cmath>
 3 #include <cstdio>
 4 #include <cstdlib>
 5 #include <algorithm>
 6 #include <queue>
 7 using namespace std;
 8 struct data
 9 {
10     int x,y,z;
11     bool operator <(const data&o)const
12     {
13         return o.z<z;  /**/
14     }
15 };
16 
17 int main()
18 {
19     int n;
20     scanf("%d",&n);
21     priority_queue<int,vector<int>,greater<int> /*这两个 >之间一定要加一个空格*/> q;
22     for(int i=1;i<=n;i++)
23     {
24         int c;
25         scanf("%d",&c);
26         if(c==1)
27         {
28             int x;
29             scanf("%d",&x);
30             q.push(x);
31         }
32         else if(c==2) printf("%d\n",q.top());/**/
33         else q.pop();
34     }
35     system("pause");
36     return 0;
37 }
priority_queue

 

【模板】小根堆

标签:表示   top   reg   lib   gif   using   nbsp   algorithm   手写   

原文地址:http://www.cnblogs.com/lulala/p/7745411.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!