当前位置:网站首页>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;
}
边栏推荐
- Form form
- 网络安全漫山遍野的高大上名词之后的攻防策略本质
- Original root and NTT 5000 word explanation
- "Could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32"
- 【ARKit、RealityKit】把图片转为3D模型
- HBuilderX 运行微信开发者工具 “Fail to open IDE“报错解决
- Database operation topic 2
- 2022流动式起重机司机考试题模拟考试题库模拟考试平台操作
- Study notes of canal
- JDBC database connection pool (Druid Technology)
猜你喜欢

十大蓝筹NFT近半年数据横向对比

Introduction to excellent verilog/fpga open source project (30) - brute force MD5

Study notes of automatic control principle -- correction and synthesis of automatic control system

数据库操作 技能6

NTT (fast number theory transformation) polynomial inverse 1500 word analysis

Review notes of Microcomputer Principles -- zoufengxing

at、crontab

2022年上海市安全员C证考试试题及模拟考试

李沐d2l(四)---Softmax回归

Pop up window in Win 11 opens with a new tab ---firefox
随机推荐
网络安全漫山遍野的高大上名词之后的攻防策略本质
[eslint] Failed to load parser ‘@typescript-eslint/parser‘ declared in ‘package. json » eslint-confi
at、crontab
NPM add source and switch source
2022茶艺师(中级)特种作业证考试题库模拟考试平台操作
Go intelligent robot alpha dog, alpha dog robot go
Qt | 关于如何使用事件过滤器 eventFilter
Nuxt - Project packaging deployment and online to server process (SSR server rendering)
Learning notes of automatic control principle --- linear discrete system
巴比特 | 元宇宙每日必读:元宇宙的未来是属于大型科技公司,还是属于分散的Web3世界?...
深度学习常用激活函数总结
NTT(快速数论变换)多项式求逆 一千五百字解析
Error: cannot find module 'UMI' problem handling
Innovus卡住,提示X Error:
Pop up window in Win 11 opens with a new tab ---firefox
Qtcreator reports an error: you need to set an executable in the custom run configuration
Study notes of dataX
ext4文件系统打开了DIR_NLINK特性后,link_count超过65000的后使用link_count=1来表示数量不可知
2022流动式起重机司机考试题模拟考试题库模拟考试平台操作
围棋智能机器人阿法狗,阿尔法狗机器人围棋