当前位置:网站首页>Heap acwing 838 Heap sort
Heap acwing 838 Heap sort
2022-07-02 12:42:00 【T_ Y_ F666】
Pile up AcWing 838. Heap sort
Original link
Algorithm tags
Pile up
Ideas

The nature of the heap
Perfect binary tree
Common heap
Heap The root node stores the minimum value , The storage value of the left and right nodes is greater than that of the root node
Big root pile The maximum storage value of the root node , The storage value of the left and right nodes is smaller than that of the root node 
storage
Use one-dimensional array storage . Be careful : Here the array subscript is from 1 Start
If from 0 Start , Then the root node will be overwritten by the left son of the root node 
Basic operation of the heap
down operation Compare the current node with the left and right sons , If the current node > The smaller of the left and right sons , Exchange the current node with the position of the smaller value in the left and right sons , Recursively perform the above operations after exchange , Until you find the right place
up operation Compare the current node with the root node , If the current node < The root node , Exchange the current node with the root node , Recursively perform the above operations after exchange , Until you find the right place
To ensure the stability of the reactor structure , For insert and delete operations , Choose to operate at the end of the array
The basic operation pseudo code of heap

The construction of the heap
If it is inserted from the tail in turn , Time complexity of each insertion log(n), Need to insert n Elements , Time complexity n*log(n)
If from N/2 Insert , It can be guaranteed in O(n) The time complexity is inserted n Elements
Proof process


Code
#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 Storage node ancestor node s Store the number of connected block nodes
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);
}
// Select the smaller value of the left and right nodes
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;
}
Originality is not easy.
Reprint please indicate the source
If it helps you Don't forget to praise and support 
边栏推荐
- The differences and relationships among port, targetport, nodeport and containerport in kubenetes
- bellman-ford AcWing 853. 有边数限制的最短路
- Error in kubeadm join: [error port-10250]: port 10250 is in use [error fileavailable--etc kubernetes PKI
- LeetCode—剑指 Offer 37、38
- Fastdateformat why thread safe
- Redis transaction mechanism implementation process and principle, and use transaction mechanism to prevent inventory oversold
- What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
- 趣味 面试题
- js 迭代器 生成器 异步代码处理 promise+生成器 -> await/async
- 堆 AcWing 838. 堆排序
猜你喜欢

应用LNK306GN-TL 转换器、非隔离电源

Docker-compose配置Mysql,Redis,MongoDB

线性DP AcWing 902. 最短编辑距离
![JDBC 预防sql注入问题与解决方法[PreparedStatement]](/img/32/f71f5a31cdf710704267ff100b85d7.png)
JDBC 预防sql注入问题与解决方法[PreparedStatement]

This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
![[ybtoj advanced training guidance] cross the river [BFS]](/img/4e/83f14452ea6410768cdd01e725af2e.jpg)
[ybtoj advanced training guidance] cross the river [BFS]

防抖 节流

Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime

通过反射执行任意类的任意方法

Initial JDBC programming
随机推荐
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
线性DP AcWing 897. 最长公共子序列
通过反射执行任意类的任意方法
CV2 in OpenCV VideoWriter_ Fourcc() function and cv2 Combined use of videowriter() function
async/await 异步函数
Redis bloom filter
C#修饰符
spfa AcWing 851. spfa求最短路
BOM DOM
spfa AcWing 852. spfa判断负环
bellman-ford AcWing 853. 有边数限制的最短路
考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
Wechat official account payment prompt MCH_ ID parameter format error
Mui WebView down refresh pull-up load implementation
C#运算符
AI中台技术调研
Fluent fluent library encapsulation
Floyd AcWing 854. Floyd求最短路
Simple understanding of ThreadLocal
Drools terminates the execution of other rules after executing one rule