您的位置:首页 > 其它

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

#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;
}


堆排序~~~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: