二叉堆练习3&【模板】堆

题目描述  Description

给定N(N≤500,000)和N个整数(较有序),将其排序后输出。

输入描述  Input Description

N和N个整数

输出描述  Output Description

N个整数(升序)

样例输入  Sample Input

5

12 11 10 8 9

样例输出  Sample Output

8 9 10 11 12

数据范围及提示  Data Size & Hint

对于33%的数据 N≤10000

对于另外33%的数据 N≤100,000  0≤每个数≤1000

对于100%的数据 N≤500,000  0≤每个数≤2*10^9

 代碼實現:

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 int n,a,b,c,dp,ado,rec;
 5 int heap[600000];//數組要開的大一些。
 6 void put(int x){
 7     ++dp;
 8     heap[dp]=x;
 9     rec=dp;
10     while(rec>1){
11         if(heap[rec]<heap[rec/2]){
12             swap(heap[rec],heap[rec/2]);
13             rec/=2;
14         }
15         else break;
16     }
17 }
18 int get(){
19     int d=1,e;
20     rec=heap[d];
21     heap[d]=heap[dp];
22     dp--;
23     while(d*2<=dp){
24         if(heap[d*2]>heap[d*2+1]) c=d*2+1;
25         else c=d*2;
26         if(heap[d]>heap[c]){
27             swap(heap[d],heap[c]);
28             d=c;
29         }
30         else break;
31     }
32     return rec;
33 }
34 int main(){
35     cin>>n;
36     for(int i=1;i<=n;i++){
37         cin>>a;
38         put(a);
39     }
40     for(int i=1;i<=n;i++) printf("%d ",get());
41     return 0;
42 }

一個堆排的裸題。

题目来源:CODE[VS]

题目描述

如题,初始小根堆为空,我们需要支持以下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<cstdio>
 2 #include<iostream>
 3 using namespace std;
 4 int n,a,b,c;
 5 int heap[5000000],k;
 6 void mai(int x){
 7     int lson=x*2,rson=x*2+1;
 8     if(lson>k) return;
 9     if(heap[lson]<heap[rson]||rson>k){
10         if(heap[x]>heap[lson]){
11             swap(heap[x],heap[lson]);
12             mai(lson);
13         }
14     }
15     else{
16         if(heap[x]>heap[rson]){
17             swap(heap[x],heap[rson]);
18             mai(rson);
19         }
20     }
21 }
22 void put(int x){
23     heap[++k]=x;
24     c=k;
25     while(heap[c]<heap[c/2]&&c/2){
26         swap(heap[c],heap[c/2]);
27         c/=2;
28     }
29 }
30 void push(){
31     heap[1]=heap[k--];
32     mai(1);
33 }
34 int main(){
35     scanf("%d",&n);
36     for(int i=1;i<=n;i++){
37         scanf("%d",&a);
38         if(a==1){
39             scanf("%d",&b);
40             put(b);
41         }
42         if(a==2) printf("%d\n",heap[1]);
43         if(a==3) push();
44     }
45     return 0;
46 }

这份代码比上面的快。

题目来源:洛谷

转载于:https://www.cnblogs.com/J-william/p/6238877.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值