当前位置:网站首页>堆 AcWing 838. 堆排序
堆 AcWing 838. 堆排序
2022-07-02 09:43:00 【T_Y_F666】
堆 AcWing 838. 堆排序
原题链接
算法标签
堆
思路

堆的本质
完全二叉树
常见的堆
小根堆 根节点存储最小值, 左右节点存储值大于根节点
大根堆 根节点存储最大值, 左右节点存储值小于根节点
存储方式
采用一维数组存储 。注意: 此处数组下标从1开始
若从0开始,则根节点会被根节点左儿子覆盖
堆的基本操作
down操作 将当前节点与左右儿子比较, 若当前节点 > 左右儿子中较小值, 将当前节点与左右儿子中较小值位置交换,交换后递归进行上述操作,直至找到适合的位置
up操作 将当前节点与根节点比较, 若当前节点 < 根节点, 将当前节点与根节点位置交换,交换后递归进行上述操作,直至找到适合的位置
为了保证堆结构的稳定性,对于插入与删除操作, 选择在数组尾部进行操作
堆的基本操作伪代码

堆的构建
若依次从尾部插入,每次插入时间复杂度log(n), 需要插入n个元素, 时间复杂度n*log(n)
若依次从N/2插入,可以保证在O(n)的时间复杂度内插入n个元素
证明过程


代码
#include<bits/stdc++.h>
#define int long long
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>b;--i)
using namespace std;
const int N = 100005;
// p存储节点祖宗节点 s存储所属连通块节点数
int a[N];
int s;
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
// 选取左右节点较小值
void down(int x){
int u=x;
if(x*2<=s&&a[x*2]<a[u]){
u=x*2;
}
if(x*2+1<=s&&a[x*2+1]<a[u]){
u=x*2+1;
}
if(x!=u){
swap(a[x], a[u]);
down(u);
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n=read(), m=read();
s=n;
rep(i, 1, n+1){
a[i]=read();
}
Rep(i, n/2, 0){
down(i);
}
while(m--){
printf("%lld ", a[1]);
a[1]=a[s--];
down(1);
}
return 0;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- LeetCode—剑指 Offer 59 - I、59 - II
- Post request body content cannot be retrieved repeatedly
- Initial JDBC programming
- Intel internal instructions - AVX and avx2 learning notes
- Use sqoop to export ads layer data to MySQL
- Deep understanding of P-R curve, ROC and AUC
- AI中台技术调研
- 防抖 节流
- Leetcode209 subarray with the smallest length
- CDA data analysis -- common knowledge points induction of Excel data processing
猜你喜欢

Deep Copy Event bus

Docker compose configuration mysql, redis, mongodb

Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution

CDA data analysis -- Introduction and use of aarrr growth model

Docker-compose配置Mysql,Redis,MongoDB

This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry

深拷贝 事件总线

There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes

CDA数据分析——AARRR增长模型的介绍、使用

MySQL indexes and transactions
随机推荐
[ybtoj advanced training guidance] judgment overflow [error]
Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students
Enhance network security of kubernetes with cilium
arcgis js 4. Add pictures to x map
Brush questions --- binary tree --2
js 迭代器 生成器 异步代码处理 promise+生成器 -> await/async
OpenCV中cv2.VideoWriter_fourcc()函数和cv2.VideoWriter()函数的结合使用
CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
线性DP AcWing 897. 最长公共子序列
Introduction to CPU instruction set
Drools dynamically add, modify, and delete rules
Intel 内部指令 --- AVX和AVX2学习笔记
The programmer and the female nurse went on a blind date and spent 360. He packed leftovers and was stunned when he received wechat at night
Redis avalanche, penetration, breakdown
Deep copy event bus
drools中then部分的写法
[I'm a mound pytorch tutorial] learning notes
中国交通标志检测数据集
Lekao: 22 year first-class fire engineer "technical practice" knowledge points
LeetCode—剑指 Offer 59 - I、59 - II