7-4 堆构建 堆中的路径 (20 分)
将一系列给定数字插入一个初始为空的小顶堆H[]
。随后对任意给定的下标i
,打印从H[i]
到根结点的路径。
输入格式:
每组测试第1行包含2个正整数N和M(≤1000),分别是插入元素的个数、以及需要打印的路径条数。下一行给出区间[-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 <iostream>
#include <algorithm>
using namespace std;
const int MAX_SIZE = 1010;
int mar[MAX_SIZE];
int n, m;
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
int id;
cin >> id;
mar[i] = id;
int current = i;
while (current != 1 && mar[current / 2] > mar[current])
{
swap(mar[current / 2], mar[current]);
current = current / 2;
}
}
while (m--)
{
int index;
cin >> index;
cout << mar[index];
index = index / 2;
while (index >= 1)
{
cout << " " << mar[index];
index = index / 2;
}
if (m != 0)
cout << endl;
}
return 0;
}