zoukankan      html  css  js  c++  java
  • Codevs

     时间限制: 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;
    }

    堆排序~~~~

  • 相关阅读:
    flash 3d基础学习
    3d中的镜头
    [转]Android Canvas 切割 clipRect
    绘制球形
    绘制圆筒
    stage3d学习笔记1
    (转)Region.Op效果解析
    游戏中的镜头
    无向网的最小生成树——Prim算法(转)
    最短路径之——Dijkstra算法(转)
  • 原文地址:https://www.cnblogs.com/suishiguang/p/5879266.html
Copyright © 2011-2022 走看看