Codevs
2016-09-17 17:37
78 查看
时间限制: 1 s
空间限制: 128000 KB
题目等级 : 白银 Silver
题解
题目描述 Description
给出n和n个整数,希望你从小到大给他们排序
输入描述 Input Description
第一行一个正整数n
第二行n个用空格隔开的整数
输出描述 Output Description
输出仅一行,从小到大输出n个用空格隔开的整数
样例输入 Sample Input
3
3 1 2
样例输出 Sample Output
1 2 3
数据范围及提示 Data Size & Hint
1<=n<=100000
堆排序~~~~
空间限制: 128000 KB
题目等级 : 白银 Silver
题解
题目描述 Description
给出n和n个整数,希望你从小到大给他们排序
输入描述 Input Description
第一行一个正整数n
第二行n个用空格隔开的整数
输出描述 Output Description
输出仅一行,从小到大输出n个用空格隔开的整数
样例输入 Sample Input
3
3 1 2
样例输出 Sample Output
1 2 3
数据范围及提示 Data Size & Hint
1<=n<=100000
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int x,n,m,heap[1000],heap_size; void put() {// 小根堆的插入 插入到堆底 向上维护 int now,next; heap[++heap_size]=x; now=heap_size; while(now>1) { next=now/2; if(heap[next]<=heap[now]) return; swap(heap[now],heap[next]); now=next; } } int get() { int now=1,next,res; res=heap[1]; heap[1]=heap[heap_size--]; while(now*2<=heap_size) { next=now*2; if(next<heap_size&&heap[next+1]<heap[next]) next++; if(heap[now]<=heap[next]) return res; swap(heap[now],heap[next]); now=next; } return res; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&x),put(); for(int i=1;i<=n;i++) printf("%d ",get()); return 0; return 0; }
堆排序~~~~
相关文章推荐
- 【codevs1116】四色问题,深搜入门题目
- 【codevs1295 N皇后问题(不输出方案)】回溯法
- 【codevs1453 统计素数个数2】E氏筛法
- codevs 4605 LCA
- VSCode的快捷键整理
- 对Atom 1.9 及 VSCODE 1.3+不能拖拽打开文件或者文件夹问题【个人解决方案】
- 用VSCode写python的正确姿势(转载)
- 在Mac系统下使用VSCODE做unity开发
- Cpp环境【Code[VS]1084】【NOIP2003普及组】乒乓球
- codevs 1021 玛丽卡
- Xn数列 CODEVS - 1281
- [CODEVS1090][NOIP2013]加分二叉树(树形dp)
- 【CODEVS】倒水问题
- codevs1163 访问艺术馆(树型dp)
- CodeVS1690 开关灯 解题报告【数据结构】【线段树】
- 姓名与ID(codevs 1027 未结题)
- mac 下vscode 部署go ethereum 过程 注意节点
- Codevs 1299 切水果
- VSCode下配置python调试运行环境的方法
- 非常全的VsCode快捷键