题目:

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. 2017.12.11SimpleDateFormat的线程安全性讨论

    转载来自:http://blog.csdn.net/zxh87/article/details/19414885 1.结论 DateFormat和SimpleDateFormat都不是线程安全的.在多 ...

  2. java中调用kettle转换文件

    java中调用kettle转换文件 通过命令行也能够调用,然后java中调用命令行代码也能够.这样没有和java代码逻辑无缝集成.本文说明kettle5.1中假设通过其它API和java代码无缝集成: ...

  3. 网页制作,网站制作中put和get的区别

    Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE.URL全称是资源描述符,我们可以这样认为:一个URL地址,它用于描述一个网络上的资源,而HTTP ...

  4. 如何安装MySQL软件

    1 双击EXE进行安装,在"Developer Components(开发者部分)"上左键单击,选择"This feature, and all subfeatures, ...

  5. 建站笔记1:centos6.5下安装mysql

    近期买了个域名,想要玩玩自己建站点:接下来遇到的问题都会一次记录下来.以备自己以后复习查看: 首先建站方案选择: wordPress +centos6.5 +mysql; server买的:搬瓦工最低 ...

  6. HTML 的超链接 a 标签中如何设置其宽度和高度?

    HTML 的超链接 a 标签中如何设置其宽度和高度? 在DIV CSS布局中,html 中 a 超链接标签,直接对其设置宽度和高度不能生效,设置宽度和高度也不起作用,这里为大家分享如何实现 a 标签宽 ...

  7. iNode协议再次分析

    iNode协议再次分析 声明: 1)本报告由博客园bitpeach撰写,版权所有,免费转载,请注明出处,并请勿作商业用途. 2)若本文档内有侵权文字或图片等内容,请联系作者bitpeach删除相应部分 ...

  8. Redis使用经验

    首先,缓存的对象有三种: 1:数据库中单条的的数据(以表名跟id作为key永久保存到Redis),在有更新的地方都要更新缓存(不适用于需要经常更新的数据): 2:对于一些不分页,不需要实时(需要多表查 ...

  9. 设置SSH编码为中文

    http://www.qzz.in/?post=198下面是详细方法: 方法1: vi /etc/sysconfig/i18n 将内容改为 LANG="zh_CN.GB18030" ...

  10. bootstrap 警告

    本章将讲解警告(Alerts)以及Bootstrap所提供的用于警告的class.警告(Alerts)向用户提供了一种定义消息样式的方式.它们为典型的用户操作提供了上下文信息反馈. 您可以为警告框添加 ...