• 7-5 堆中的路径


    https://pintia.cn/problem-sets/15/problems/713

    将一系列给定数字插入一个初始为空的小顶堆H[]。随后对任意给定的下标i,打印从H[i]到根结点的路径。

    输入格式:

    每组测试第1行包含2个正整数N和M(≤),分别是插入元素的个数、以及需要打印的路径条数。下一行给出区间[-10000, 10000]内的N个要被插入一个初始为空的小顶堆的整数。最后一行给出M个下标。

    输出格式:

    对输入中给出的每个下标i,在一行中输出从H[i]到根结点的路径上的数据。数字间以1个空格分隔,行末不得有多余空格。

    输入样例:

    5 3
    46 23 26 24 10
    5 4 3
    

    输出样例:

    24 23 10
    46 23 10
    26 10

    提交代码
    #include <stdio.h>
    #include <stdlib.h>
    
    //min heap save from 1 to n
    
    void PushHeap(int *heap, int &len, int data)
    {
        int i = ++len;
        while(i!=1 && data < heap[i>>1])
        {
            heap[i] = heap[i>>1];
            i>>=1;
        }
        heap[i] = data;
    }
    
    int PopHeap(int *heap, int &len)
    {
        if(len==0)
            return -1;
        int ans = heap[1];
        int num = heap[len--];
        int father = 1, child = 2;
        while(child <=len)
        {
            if(child<len && heap[child+1] < heap[child])
                ++child;
            if(num <= heap[child])
                break;
            heap[father] = heap[child];
            father = child;
            child <<= 1;
        }
        heap[father] = num;
        return ans;
    }
    
    void MakeHeap(int *heap, int len)
    {
        for(int i=len/2;i>=1;i--)
        {
            int num = heap[i];
            int father = i, child = i*2;
            while(child<=len)
            {
                if(child<len && heap[child+1]<heap[child])
                    child++;
                if(num<=heap[child])
                    break;
                father = child;
                child *= 2;
            }
            heap[father] = num;
        }
    }
    
    void SortHeap(int *heap, int len)
    {
        MakeHeap(heap,len);
        for(int i=len;i>1;i--)
            heap[i] = PopHeap(heap,len);
    }
    
    int main()
    {
        int insertNum,printNum,tmp;
        //int *heap = (int *)malloc(sizeof(int)*(insertNum+1));
        int heap[1024] = {0};
        int len = 0;
        scanf("%d %d",&insertNum,&printNum);
        for(int i=0;i<insertNum;i++)
        {
            scanf("%d",&tmp);
            PushHeap(heap,len,tmp);
        }
        for(int i=0;i<printNum;i++)
        {
            scanf("%d",&tmp);
            tmp = tmp < len ? tmp : len;
            bool isFirst = true;
            while(tmp>=1)
            {
                if(isFirst)
                {
                    isFirst = false;
                    printf("%d",heap[tmp]);
                }
                else
                {
                    printf(" %d",heap[tmp]);
                }
                tmp>>=1;
            }
            printf("
    ");
        }
        //free(heap);
        return 0;
    }
  • 相关阅读:
    Office 转 PDF & PDF 转 SWF Windows版
    Office 转 PDF & PDF 转 SWF Linux版
    MP4Box 编译 和相应命令
    CentOS VNC 安装与配置,方便进行运程桌面连接
    系统时钟&&硬件时钟
    IPtables中SNAT、DNAT和MASQUERADE的含义
    配置SNAT实现共享上网
    DNAT & SNAT
    linux应急操作
    linux-清理linux空间
  • 原文地址:https://www.cnblogs.com/cbattle/p/10768216.html
Copyright © 2020-2023  润新知