zoukankan      html  css  js  c++  java
  • PAT005 Path in a Heap

    题目:

    Insert a sequence of given numbers into an initially empty min-heap H. Then for any given index i, you are supposed to print the path from H[i] to the root.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives two positive integers N and M (<=1000) which are the size of the input sequence, and the number of indices to be checked, respectively. Given in the next line are the N integers in [-10000, 10000] which are supposed to be inserted into an initially empty min-heap. Finally in the last line, M indices are given.

    Output Specification:

    For each index i in the input, print in one line the numbers visited along the path from H[i] to the root of the heap. The numbers are separated by a space, and there must be no extra space at the end of the line.

    Sample Input:

    5 3
    46 23 26 24 10
    5 4 3
    

    Sample Output:

    24 23 10
    46 23 10
    26 10

    分析:主要是考查最小堆的建立。

    代码:

    typedef struct heapNode {
        int elements[1000];
        int size;
        int capacity;
    } MinHeapNode;
    
    MinHeapNode *createHeap(int maxSize)
    {
        MinHeapNode *heap = (MinHeapNode *)malloc(sizeof(MinHeapNode));
        heap->size = 0;
        heap->capacity = maxSize;
        heap->elements[0] = -10001; // 哨兵
        return heap;
    }
    
    void insertToMinHeap(MinHeapNode *heap, int item)
    {
        if (heap->size == heap->capacity) {
            return; // 堆已满,无法插入
        }
        
        int i = ++heap->size;
        for (; heap->elements[i / 2] > item; i /= 2) {
            heap->elements[i] = heap->elements[i / 2];
        }
        heap->elements[i] = item;
    }
    
    int main()
    {
        // 接受输入
        int itemNum, indexNum;
        scanf("%d %d", &itemNum, &indexNum);
        
        MinHeapNode *heap = createHeap(itemNum);
        for (int i = 0; i < itemNum; i++) {
            int item;
            scanf("%d", &item);
            insertToMinHeap(heap, item);
        }
        
        int a[indexNum];
        for (int i = 0; i < indexNum; i++) {
            int index;
            scanf("%d", &index);
            a[i] = index;
        }
        
        for (int i = 0; i < indexNum; i++) {
            int searchIndex = a[i];
            int flag = 1;
            while (searchIndex > 0) {
                if (flag) {
                    printf("%d", heap->elements[searchIndex]);
                    flag = 0;
                } else {
                    printf(" %d", heap->elements[searchIndex]);
                }
                
                searchIndex /= 2;
            }
            printf("
    ");
        }
    }

    运行结果:

  • 相关阅读:
    中台入门系列1
    微服务 2.0 技术栈选型手册
    mysql计划任务每天定时执行
    更高效地提高redis client多线程操作的并发吞吐设计
    azure之MSSQL服务性能测试
    .NET Socket服务编程之-高效连接接入编
    轻易实现基于linux或win运行的聊天服务端程序
    零配置Socket TCP消息通讯服务容器EC
    azure存储压测的问题(农码主观意识太强被坑了)
    业务逻辑层缓存应该设计
  • 原文地址:https://www.cnblogs.com/liufeng24/p/4395780.html
Copyright © 2011-2022 走看看