zoukankan      html  css  js  c++  java
  • codevs 3110 二叉堆练习3

    时间限制: 3 s
     空间限制: 128000 KB
     题目等级 : 黄金 Gold
    题目描述 Description

    给定N(N≤500,000)和N个整数(较有序),将其排序后输出。

    输入描述 Input Description

    N和N个整数

    输出描述 Output Description

    N个整数(升序)

    样例输入 Sample Input

    5

    12 11 10 8 9

    样例输出 Sample Output

    8 9 10 11 12

    数据范围及提示 Data Size & Hint

    对于33%的数据 N≤10000

    对于另外33%的数据 N≤100,000  0≤每个数≤1000

    对于100%的数据 N≤500,000  0≤每个数≤2*10^9

    自己打的代码 

    #include <algorithm>
    #include <cstdio>
    
    using namespace std;
    
    int num,heap[500000*4+1],i,j,n,size;
    inline void push()
    {
        scanf("%d",&num);
        heap[++size]=num;
        int pos=size;
        while(pos>1)
        {
            int next=pos/2;
            if(heap[next]<=heap[pos])
            break;
            swap(heap[next],heap[pos]);
            pos=next;
        }
    }
    inline int pop()
    {
        int t=heap[1];
        heap[1]=heap[size--];
        int pos=1;
        while(pos*2<=size)
        {
            int next=pos*2;
            if(heap[next+1]<heap[next])
            next++;
            if(heap[next]>=heap[pos]) break;
            swap(heap[next],heap[pos]);
            pos=next;
        }
        return t;
    }
    int main()
    {
        scanf("%d",&n);
        for(i=0;i<n;++i)
        push();
        for(i=0;i<n;++i)
        printf("%d ",pop());
        return 0;
    }

      包含多种操作的代码

      
    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    #include <cstring>
    #include <string>
    
    using namespace std;
    
    int heap[500010],n;
    void shiftdown(int i,int m)  // 堆向下调整 
    {
        int k,t;
        t=heap[i];
        k=2*i;
        while(k<=m)
        {
            if(k<m&&heap[k]>heap[k+1]) k++;
            if(t>heap[k])
            {
                heap[i]=heap[k];
                i=k;
                k=2*i;
            }
            else break;
        }
    }
    void shiftup(int x)  //堆向上调整 
    {
        int temp;
        temp=heap[x];
        for(int i=x/2;i>0&&temp<heap[i];i=i/2)
        {
            heap[x]=heap[i];
            x=i;
        }
        heap[x]=temp;
    }
    void del() //删除堆顶元素 
    {
       heap[1]=heap[n];
       n--;
       if(n>0)
       {
       shiftdown(1,n);
       }
    }
    void insert(int x)
    {
        n++;
        heap[n]=x;
        shiftup(n);
        }
        void shiftda(int i,int len)
        {
        int t=heap[i],k=2*i;
        while(k<=len)
        {
        if(k<len&&(heap[k]<heap[k+1])) k++;
        if(t<heap[k])
        {
        heap[i]=heap[k];
        i=k;
        k=2*i;
        }
        else break;
        }
        heap[i]=t;
    }
    void shiftxiao(int i,int len)
    {
        int t=heap[i],k;
        k=2*i;
        while(k<=len)
        {
            if(k<len&&heap[k]>heap[k+1]) k++;
            if(t>heap[k])
            {
                heap[i]=heap[k];
                i=k;
                k=2*i;
            }
            else break;
        }
        heap[i]=t;
    }
    int main()
    {
           int t;
           cin>>n;
           for(int i=1;i<=n;i++)
           {
            scanf("%d",&heap[i]);
           }
           for(int i=n/2;i>=1;i--) shiftxiao(i,n); //建堆 
           for(int j=n;j>=2;j--) // 排序 
           {
               t=heap[1];
               heap[1]=heap[j];
               heap[j]=t;
               shiftxiao(1,j-1);
        }
        for(int i=n;i>=1;i--)
        printf("%d ",heap[i]); 
        return 0;
    }
    View Code

          传送门 

       恶补知识1 http://www.cppblog.com/guogangj/archive/2009/10/29/99729.html

       恶补知识2 http://www.cnblogs.com/QG-whz/p/5173112.html

    我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。
  • 相关阅读:
    关于StringBuilder拼成的json格式数据,在转化成string数据类型后,转义符失效
    jQuery 引用地址{包括jquery和google提供的地址}, 节省你不必要的流量
    jQuery的.bind()、.live()和.delegate()之间区别
    使用Jquery Easy UI要导入的js顺序
    babygin
    card
    DecimalFormat用法
    DATEDIFF 函数 [日期和时间]
    IsDirectory( )的用法
    Arrays.asList()函数
  • 原文地址:https://www.cnblogs.com/ruojisun/p/6238860.html
Copyright © 2011-2022 走看看