04-树9. Path in a Heap (25)
来源:互联网 发布:山东广电网络集团吧 编辑:程序博客网 时间:2024/06/06 01:22
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 areseparated by a space, and there must be no extra space at the end of the line.
Sample Input:5 346 23 26 24 105 4 3Sample Output:
24 23 1046 23 1026 10
提交代码
————————————————
这个题目和云课堂上面讲的一样的。
主要涉及的知识点为。
1,堆的创建——最小堆,利用数组来表示。和上面一道题很类似。下标为i 的节点的左右儿子节点的小标为2*i 和2*i+1,反过来,父亲下标为i/2.
2,插入一个节点。先放在最后面,然后和父亲比较,如果不符合的话,换。
3,索引值的理解。从1开始,在0处放上一个哨兵。最小堆上面的哨兵的值为最小值。
————————————————
代码如下。
#include <iostream>#include <stdlib.h>using namespace std;/* run this program using the console pauser or add your own getch, system("pause") or input loop */typedef struct MinHeap{int *data;int size;int cap;}* pMinHeap;pMinHeap Creat(int Size){pMinHeap h =(pMinHeap)malloc(sizeof(struct MinHeap));h->data=(int *)malloc((Size+1)*sizeof(int));h->cap=Size;h->size=0;h->data[0]=-10001;//The Max datareturn h;}void Insert(pMinHeap h, int d){if(h->size == h->cap)return;int i=++h->size;for(;h->data[i/2]>d; i/=2){h->data[i]=h->data[i/2];}h->data[i]=d;}int main(int argc, char** argv) {int N=0, M=0;pMinHeap H;cin>>N>>M;H=Creat(N);for(int i=0; i<N; i++){int tmp=0;cin>>tmp;Insert(H,tmp);}for(int i=0; i<M; i++){int index=0;cin>>index;for(int j=index; j>0; j/=2){cout<<H->data[j];j==1?cout<<endl:cout<<" ";}}return 0;}
- 04-树9. Path in a Heap (25)
- PAT 数据结构 04-树9. Path in a Heap (25)
- 04-树9. Path in a Heap (25)
- 04-树9. Path in a Heap (25) -- 小堆树-利用其性质
- 05-树6. Path in a Heap (25)
- 05-树6. Path in a Heap (25)
- 05-树6. Path in a Heap
- Dijkstra+Heap-HDU-4725-The Shortest Path in Nya Graph
- SPOJ 1437 Longest path in a tree
- leetcode Longest Increasing Path in a Matrix
- 329. Longest Increasing Path in a Matrix
- [LeetCode] Longest Increasing Path in a Matrix
- Longest Increasing Path in a Matrix
- leetcode: Longest Increasing Path in a Matrix
- Leetcode: Longest Increasing Path in a Matrix
- Longest Increasing Path in a Matrix
- leetcode:Longest Increasing Path in a Matrix
- [LeetCode]Longest Increasing Path in a Matrix
- 连菜鸟都算不上的代码小萌妹的学习之路开始了
- json-lib中toBean方式解析JSON串大小写问题
- 【转】投资中最难的事情
- sql server的20个系统变量
- 利用WebView API来做一个自己的浏览器
- 04-树9. Path in a Heap (25)
- SpringMVC返回XML或者JSON格式的数据
- 陈怡暖:(午评)非农打击金银,多头弱势反攻
- [学习记录]NSString与int和float的相互转换
- 如何在mac os中安装gdb及为gdb进行代码签名
- Android 装置CTS与GMS认证差异
- Hadoop自带字数统计程序wordcount异常
- 一个四十岁的人给你提个醒
- C++ Primer 学习笔记_4_变量和基本类型(续1)