题目:

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. CCPlatformConfig(设置执行平台 iOS android win32等。。。)

    #ifndef __CC_PLATFORM_CONFIG_H__ #define __CC_PLATFORM_CONFIG_H__ /** Config of cocos2d-x project, p ...

  2. Game Center Achievements and Leaderboards part 1 <转>

    原地址:http://blog.sina.com.cn/s/blog_6b3661a901013xb0.html This is the first part of a step-by-step tu ...

  3. Linux程序员福利 - 追女友神奇(Linux终端运行炫酷程序)

    概述 作为IT人员,给同事的感觉呆板,不会会浪漫,不懂情趣.其实不然,我们可以用我们的技能创造出IT人员独有的浪漫.girlLove脚本就可以实现IT人员的浪漫.girlLove本质上是一个简易的问答 ...

  4. CentOS7 升级到7.4

    2 升级CentOS7.4 自己电脑上的系统还是CentOS7.2,服务器是CentOS7.3, 打算统统升级到最新版 升级前查看 > lsb_release -a LSB Version: : ...

  5. Linux命令-网络命令:traceroute

    首先需要设置VM里面的linux系统能够上网.菜单项->虚拟机->设置,选择NAT模式 菜单项->编辑->虚拟网络编辑器->VMnet8 NAT模式->NAT设置, ...

  6. 转:浅析VO、DTO、DO、PO的概念、区别和用处

    原文链接 概念: VO(View Object):视图对象,用于展示层,它的作用是把某个指定页面(或组件)的所有数据封装起来. DTO(Data Transfer Object):数据传输对象,这个概 ...

  7. express 创建node服务器

    var express = require('express'); var app = new express(); app.listen(3000); app.get('/',function(re ...

  8. 每天进步一点点——Ganglia的Python扩展模块开发

    转载请说明出处:http://blog.csdn.net/cywosp/article/details/39701245 注:本文涉及到的代码都在centos 6.5 64bit系统上通过验证,Gan ...

  9. macbook使用“终端”远程登录linux主机

    登录mac系统后,依次打开顶部菜单,“前往” -> “应用程序” -> “实用工具” -> “终端”,如下图:   在打开的终端页面,输入如下代码: ssh user@hostnam ...

  10. mysql产生随机数并连接字符串

    concat('a','b','c');//连接字符串 rand();//产生随机数 floor();//取整数 UPDATE user set reg_ip= concat(floor(RAND() ...