当前位置:网站首页>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
边栏推荐
- OpenCV中cv2.VideoWriter_fourcc()函数和cv2.VideoWriter()函数的结合使用
- Deep Copy Event bus
- kubenetes中port、targetPort、nodePort、containerPort的区别与联系
- 高性能纠删码编码
- 正确遍历EntryList方法
- . Net wechat message template push
- 线性DP AcWing 895. 最长上升子序列
- 区间DP AcWing 282. 石子合并
- 3 A VTT端接 稳压器 NCP51200MNTXG资料
- Openssh remote enumeration username vulnerability (cve-2018-15473)
猜你喜欢
Rust search server, rust quick service finding tutorial
Use sqoop to export ads layer data to MySQL
JS8day(滚动事件(scroll家族),offset家族,client家族,轮播图案例(待做))
通过反射执行任意类的任意方法
Lekao.com: experience sharing of junior economists and previous candidates in customs clearance
Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution
Enhance network security of kubernetes with cilium
[ybtoj advanced training guidance] cross the river [BFS]
包管理工具
线性DP AcWing 896. 最长上升子序列 II
随机推荐
String palindrome hash template question o (1) judge whether the string is palindrome
Is the neural network (pinn) with embedded physical knowledge a pit?
LeetCode—剑指 Offer 59 - I、59 - II
[FFH] little bear driver calling process (take calling LED light driver as an example)
Embedded Software Engineer career planning
深拷贝 事件总线
Redis introduction, scenario and data type
Some sudden program ideas (modular processing)
1380. Lucky numbers in the matrix [two-dimensional array, matrix]
Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
Sse/avx instruction set and API of SIMD
[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol
Sparkcontext: error initializing sparkcontext solution
Rust search server, rust quick service finding tutorial
染色法判定二分图 AcWing 860. 染色法判定二分图
Simple understanding of ThreadLocal
通过反射执行任意类的任意方法
H5 to app
Use sqoop to export ads layer data to MySQL
[ybtoj advanced training guidance] judgment overflow [error]