当前位置:网站首页>838. Heap sorting
838. Heap sorting
2022-07-26 09:11:00 【Hunter_ Kevin】
subject
Enter a length of n The whole number sequence of , From small to large, before output m Small number .
Input format
The first line contains integers n and m.
The second line contains n It's an integer , Represents an integer sequence .
Output format
All in one line , contain m It's an integer , Represents the first in an integer sequence m Small number .
Data range
1≤m≤n≤105,
1≤ The elements in the sequence ≤109
sample input :
5 3
4 5 1 3 2
sample output :
1 2 3
Ideas
Ideas :
Common operation of heap :
1. Insert a numberh[++size] = num; up(size);
2. Find the minimum value in the seth[1];
3. Delete minimumh[1] = h[size];size--; down(1);
4. Delete any elementh[k] = h[size]; size--; down(k), up(k);
//down and up Do it all once , Only one , There is no need to judge and execute , Convenient code writing and memory
5. Modify any elementh[k] = num; down(k), up(k);
//down and up Do it all once , Only one , There is no need to judge and execute , Convenient code writing and memory
Code :
#include <iostream>
using namespace std;
const int N = 100010;
int h[N], cnt;
// The array stores the nodes of the heap , Subscript from 1 Start , node x The subscript of the left child node of is 2x, The subscript of the right child node is 2x+1
// If subscript from 0 Start , node x The subscript of the left child node of is 2x+1, The subscript of the right child node is 2x+2
//down operation , Subscript in the heap as u To reorder the elements of
//down The core idea of is node x, node 2x, node 2x+1 Exchange positions , Ensure that the minimum of the three values is at the parent node
void down(int u)
{
int t = u;
if(2 * u <= cnt && h[2 * u] < h[t]) t = 2*u;
if(2 * u + 1 <= cnt && h[2 * u + 1] < h[t]) t = 2 * u + 1;
if(t != u)
{
swap(h[t], h[u]);
down(t);
}
}
int main()
{
int n, m;
cin >> n >> m;
cnt = n;// Number of nodes in the heap , That is, the total number of sequence elements
for(int i = 1; i <= n; i++)
cin >> h[i];
// Generate small root heap , The time complexity is about O(n), from n/2 The node at starts to traverse the node up , And do it one by one down(i) operation , Every time down perhaps up The operation is proportional to the number of levels of the binary tree , The time complexity of each operation is logN
for(int i = n/2; i; i--)
down(i);
while(m --)
{
cout << h[1] <<' ';
h[1] = h[cnt];
cnt--;// The number of heap elements decreases automatically
down(1);// Will exchange the top elements down Reorder into small root piles
}
return 0;
}
边栏推荐
- Go intelligent robot alpha dog, alpha dog robot go
- Pytoch realizes logistic regression
- 基于序的评价指标 (特别针对推荐系统和多标签学习)
- 原根与NTT 五千字详解
- Unity topdown character movement control
- Day06 operation -- addition, deletion, modification and query
- Day06 homework - skill question 6
- Apple generated and verified tokens for PHP
- Study notes of canal
- unity TopDown角色移动控制
猜你喜欢
Polynomial open root
李沐d2l(五)---多层感知机
ES6 modular import and export) (realize page nesting)
209. Subarray with the smallest length
TCP solves the problem of short write
(1) CTS tradefed test framework environment construction
NTT (fast number theory transformation) polynomial inverse 1500 word analysis
Day06 operation -- addition, deletion, modification and query
ext4文件系统打开了DIR_NLINK特性后,link_count超过65000的后使用link_count=1来表示数量不可知
Form form
随机推荐
Day 6 summary & database operation
Pat grade a a1076 forwards on Weibo
QtCreator报错:You need to set an executable in the custom run configuration.
Original root and NTT 5000 word explanation
NTT(快速数论变换)多项式求逆 一千五百字解析
Clean the label folder
Laravel框架日志文件存放在哪里?怎么用?
ONTAP 9文件系统的限制
Simple message mechanism of unity
The idea shortcut key ALT realizes the whole column operation
Codeworks DP collection
TCP solves the problem of short write
The essence of attack and defense strategy behind the noun of network security
语音聊天app源码——钠斯直播系统源码
HBuilderX 运行微信开发者工具 “Fail to open IDE“报错解决
Form form
mysql函数
【ARKit、RealityKit】把图片转为3D模型
(1) CTS tradefed test framework environment construction
对标注文件夹进行清洗