PAT005 Path in a Heap
题目:
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的更多相关文章
- 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 ...
- pat04-树9. Path in a Heap (25)
04-树9. Path in a Heap (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Insert ...
- PAT 05-树6 Path in a Heap
这次的作业完全是依葫芦画瓢,参照云课堂<数据结构>(http://mooc.study.163.com/learn/ZJU-1000033001#/learn/content)中何钦铭老师 ...
- 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 ...
- PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由
03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top do ...
- 日志分析 第六章 安装elasticsearch
在这里,以两台es集群为例. es集群健康状况有三种状态,这里我们搭建的es集群,只要两台不同时挂掉,数据不会丢失. green 所有主要分片和复制分片都可用 yellow 所有主要分片可用,但不是所 ...
- golang gc 优化思路以及实例分析
一个即将上线的go 写的高频服务,压测的时候发现 gc 特别高,高到10%-15% 左右了,本文记录下优化 gc 的过程和和思路.线上环境1.10. 首先,查看gc 是否有异常,我们可以使用 gctr ...
- Java性能调优:利用JFR生成性能日志
Java性能调优作为大型分布式系统提供高性能服务的必修课,其重要性不言而喻. 好的分析工具能起到事半功倍的效果,利用分析利器JMC.JFR,可以实现性能问题的准确定位. 本文主要阐述如何利用JFR生成 ...
- Android内存管理(10)MAT: 基本教程
原文: http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.mat.ui.help%2Fgettingstarted%2Fbasic ...
随机推荐
- 更新node版本
步骤如下:1.查看当前的node版本 node -v 2.清除npm当前缓存信息 npm cache clean -f 3.执行下载node npm install -g n 4.下载成功后执行安装 ...
- c++11 Using Callable Objects, std::thread, std::bind, std::async, std::call_once
- 【ajax+php】动态展示4级单位(省、市、县、镇)
1.本篇教程以ajax+php动态展示[省.市.县.镇]四级地区单位 2.效果图: 3.不废话,贴代码! HTML: <div class="form-group"&g ...
- Mysql报错......\xE6\x80\xBB\xE7\x9B\x91' for column...
Mysql添加表中字符报错:Incorrect string value: '\xE6\x80\xBB\xE7\x9B\x91' for column 'postName' at row 1 原因:字 ...
- bootstrap学习笔记 多媒体对象
本文将介绍Bootstrap中的多媒体对象(Media Object).这些抽象的对象样式用于创建各种类型的组件(比如博客评论),我们可以在组件中使用图文混排,图像可以左对齐或者右对齐.媒体对象可以用 ...
- 数据库表syscolumns 各个字段含义 select * from syscolumns where name='textA'
每个数据库创建后都会有一些系统表用来存储该数据库的一些基本信息 每个表和视图中的每列在表中占一行,存储过程中的每个参数在表中也占一行.该表位于每个数据库中. 列名 数据类型 描述 name sysna ...
- java 重载和多态的区别
虚拟函数表是在编译期就建立了,各个虚拟函数这时被组织成了一个虚拟函数的入口地址的数组.而对象的隐藏成员--虚拟函数表指针是在运行期--也就是构造函数被调用时进行初始化的,这是实现多态的关键. http ...
- Python -面向对象(一 基本概念)
一 Python简单介绍 Python是一个可移植的面向对象的脚本语言. Python尽管是一个脚本语言,但也是一个全然面向对象的语言.由于它设计之初把易用性做为很重要的一个考量标准,所以用起来很简洁 ...
- openstack neutron L3 HA
作者:Liping Mao 发表于:2014-08-20 版权声明:能够随意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 近期Assaf Muller写了一篇关于Neutro ...
- winform最小化及关闭提示
public PrintService() { InitializeComponent(); this.WindowState = FormWindowState.Minimized; } priva ...