题目:

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[];
int size;
int capacity;
} MinHeapNode; MinHeapNode *createHeap(int maxSize)
{
MinHeapNode *heap = (MinHeapNode *)malloc(sizeof(MinHeapNode));
heap->size = ;
heap->capacity = maxSize;
heap->elements[] = -; // 哨兵
return heap;
} void insertToMinHeap(MinHeapNode *heap, int item)
{
if (heap->size == heap->capacity) {
return; // 堆已满,无法插入
} int i = ++heap->size;
for (; heap->elements[i / ] > item; i /= ) {
heap->elements[i] = heap->elements[i / ];
}
heap->elements[i] = item;
} int main()
{
// 接受输入
int itemNum, indexNum;
scanf("%d %d", &itemNum, &indexNum); MinHeapNode *heap = createHeap(itemNum);
for (int i = ; i < itemNum; i++) {
int item;
scanf("%d", &item);
insertToMinHeap(heap, item);
} int a[indexNum];
for (int i = ; i < indexNum; i++) {
int index;
scanf("%d", &index);
a[i] = index;
} for (int i = ; i < indexNum; i++) {
int searchIndex = a[i];
int flag = ;
while (searchIndex > ) {
if (flag) {
printf("%d", heap->elements[searchIndex]);
flag = ;
} else {
printf(" %d", heap->elements[searchIndex]);
} searchIndex /= ;
}
printf("\n");
}
}

运行结果:

PAT005 Path in a Heap的更多相关文章

  1. 05-树6. Path in a Heap (25) 小根堆

    05-树6. Path in a Heap (25) Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://www.patest.cn/contes ...

  2. pat04-树9. Path in a Heap (25)

    04-树9. Path in a Heap (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Insert ...

  3. PAT 05-树6 Path in a Heap

    这次的作业完全是依葫芦画瓢,参照云课堂<数据结构>(http://mooc.study.163.com/learn/ZJU-1000033001#/learn/content)中何钦铭老师 ...

  4. LC 656. Coin Path 【lock, Hard】

    Given an array A (index starts at 1) consisting of N integers: A1, A2, ..., AN and an integer B. The ...

  5. PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由

    03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top do ...

  6. 日志分析 第六章 安装elasticsearch

    在这里,以两台es集群为例. es集群健康状况有三种状态,这里我们搭建的es集群,只要两台不同时挂掉,数据不会丢失. green 所有主要分片和复制分片都可用 yellow 所有主要分片可用,但不是所 ...

  7. golang gc 优化思路以及实例分析

    一个即将上线的go 写的高频服务,压测的时候发现 gc 特别高,高到10%-15% 左右了,本文记录下优化 gc 的过程和和思路.线上环境1.10. 首先,查看gc 是否有异常,我们可以使用 gctr ...

  8. Java性能调优:利用JFR生成性能日志

    Java性能调优作为大型分布式系统提供高性能服务的必修课,其重要性不言而喻. 好的分析工具能起到事半功倍的效果,利用分析利器JMC.JFR,可以实现性能问题的准确定位. 本文主要阐述如何利用JFR生成 ...

  9. Android内存管理(10)MAT: 基本教程

    原文: http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.mat.ui.help%2Fgettingstarted%2Fbasic ...

随机推荐

  1. 解决:mysql5.7 timestamp默认值0000-00-00 00:00:00 报错

    解决:mysql5.7 timestamp默认值0000-00-00 00:00:00 报错 学习了:https://www.cnblogs.com/cnhkzyy/p/9119339.html se ...

  2. 转:windows BAT 批处理脚本教程

    转自:http://www.cnblogs.com/mq0036/p/3412171.html BAT 批处理脚本教程 第一章 批处理基础第一节 常用批处理内部命令简介 批处理定义:顾名思义,批处理文 ...

  3. 转: Android Studio你不知道的调试技巧

    http://tianweishu.com/2015/12/21/android-studio-debug-tips-you-may-not-know/

  4. Python将一个大文件按段落分隔为多个小文件的简单方法

    今天帮同学处理一点语料. 语料文件有点大,而且是以连续两个换行符作为段落标志,他想把它按段落分隔成多个小文件.即每3个段落组成一个新文件.因为曾经没有遇到过类似的操作,在网上找了一些类似的方法,看起来 ...

  5. win7多重网络之默认网关问题

    如果有谁做硬件的在用MAC,跟我讲一声好用不好用...有常用软件清单最好.windows已经用的无力吐槽了. 机子上有两块网卡,一块有线一块无线,有线的连接内部网络,无线连接外部网络, 问题描述: 内 ...

  6. STL源码剖析(迭代器)

    在STL中,容器跟算法是分开设计的,算法是通过迭代器来对容器进行操作的. 在算法运用迭代器的时候,可能会用到其相应的型别,例如返回值为容器中元素的型别,又或者说根据迭代器的类型来选择更好的算法等等. ...

  7. 准备你的Adempiere开发环境(1)- 编译

    1. 安装JDK 1.6. 2. 安装Eclipse IDE for Java EE Developers. 3. 导入adempire-360lts: 4. 关闭adempiere-360lts的B ...

  8. Python 多行注释

    Python 使用" # ” 进行单行注释,本身不带多行注释. 但在编译器 PyCharm 中,可以用以下方法注释多行代码: 1.“选中一段要注释的代码——>Ctrl+ / ” 即可注 ...

  9. Servlet容器 Jetty

    http://www.oschina.net/p/jettyJetty 是一个开源的servlet容器,它为基于Java的web内容,例如JSP和servlet提供运行环境.Jetty是使用Java语 ...

  10. thinkphp 编辑器kindeditor

    首先,去官网下载最新版的kindeditor,然后把里面asp,jsp,net,example的全删除,然后改名为editor放进public(最外层目录的public)文件夹里面 在目录lib目录建 ...