当前位置:网站首页>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 
边栏推荐
- Sweetheart leader: Wang Xinling
- Distributed machine learning framework and high-dimensional real-time recommendation system
- Drools executes string rules or executes a rule file
- kubenetes中port、targetPort、nodePort、containerPort的区别与联系
- js 迭代器 生成器 异步代码处理 promise+生成器 -> await/async
- PR 2021 quick start tutorial, learn about the and functions of the timeline panel
- Intel 内部指令 --- AVX和AVX2学习笔记
- [ybtoj advanced training guidance] cross the river [BFS]
- About the loading of layer web spring layer components, the position of the layer is centered
- Find the common ancestor of any two numbers in a binary tree
猜你喜欢

Enhance network security of kubernetes with cilium

Docker compose configuration mysql, redis, mongodb

线性DP AcWing 902. 最短编辑距离

Deep copy event bus
![2.6 using recursion and stack - [tower of Hanoi problem]](/img/fc/45038170dafd104691c93716b103cf.jpg)
2.6 using recursion and stack - [tower of Hanoi problem]

线性DP AcWing 896. 最长上升子序列 II

High performance erasure code coding

Direct control PTZ PTZ PTZ PTZ camera debugging (c)

BOM DOM

Bom Dom
随机推荐
Find the common ancestor of any two numbers in a binary tree
2.7 binary tree, post order traversal - [FBI tree]
Drools terminates the execution of other rules after executing one rule
Sub thread get request
深拷貝 事件總線
AI中台技术调研
Shuttle encapsulated AppBar
Simple use of drools decision table
spfa AcWing 852. spfa判断负环
堆 AcWing 839. 模拟堆
Leetcode - Sword finger offer 59 - I, 59 - II
Direct control PTZ PTZ PTZ PTZ camera debugging (c)
Adding database driver to sqoop of cdh6
Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution
AAAI 2022 | Peking University & Ali Dharma Institute: pruning and compression of pre training language model based on comparative learning
Redis avalanche, penetration, breakdown
bellman-ford AcWing 853. 有边数限制的最短路
LeetCode—<动态规划专项>剑指 Offer 19、49、60
Performance tuning project case
C#运算符