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.1
2
3
4
5
6
7
8Sample Input:
5 3
46 23 26 24 10
5 4 3
Sample Output:
24 23 10
46 23 10
26 10
代码如下:
1 | #include<iostream> |
多余的Delete操作没有删,源代码
其实只要将最小堆的插入删除创建写一遍就好了,这道题就迎刃而解了,我第一次提交没过,觉得可能是我数组定义小了,所以换成1000,还是没过,然后再换成1001,过了,所以最后一个测试点的数据大小应该是1000,因为我的IsFull()函数是判断m_h->size == m_h->capacity - 1,因为我们设置了哨兵,他也占了一个位置,哨兵的作用是为了再插入或者删除的循环体内让其达到边界就退出,不需要额外加一个i>0.