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 ...
随机推荐
- python gevent使用例子
python gevent使用例子 from gevent.pool import Pool POOL_SIZE = 100 def process(func, param1_list, param2 ...
- iOS 判断NSString是否包含某个字符串
主要是使用3个方法 rangeOfString 是否包含 hasPrefix 是否在前缀包含 hasSuffix 是否在末尾包含 如代码: //判断字符是否包含某字 ...
- python——python数据结构之栈、队列的实现
这个在官网中list支持,有实现. 补充一下栈,队列的特性: 1.栈(stacks)是一种只能通过访问其一端来实现数据存储与检索的线性数据结构,具有后进先出(last in first out,LIF ...
- Vim快捷键整理
Vim主要分为两种模式一种是Insert模式,该模式下可以像其它文本编辑器一样正常输入字符:另一种是Normal模式,该模式下Vim监听用户的按键可以对文本进行快速修改. 想要从Insert模式切换到 ...
- XAudio2学习之混音
XAudio2不仅能够进行採样率转换.还能够进行混音.所谓混音就是将多路音频混合成一路进行输出.混音主要是IXAudio2SubmixVoice进行此项功能. 数据由IXAudio2SourceVoi ...
- Visual studio之C# 重新定义Messbox的显示窗口位置
背景 当前做的APP需要新建一个设置窗口,该设置窗口会出现在靠近屏幕边缘位置,但主窗口铺满屏幕,设置窗口会弹出一些讯息,但默认情况下Messagebox窗口会居中于主窗口,这不太符合要求,正常应该居中 ...
- JDBC操作,执行数据库更新操作
目标: 使用Connection对象取得Statement实例 使用Statement进行数据增删改. Statement接口 要对数据库操作,要使用Statement完成.此接口可以使用Connec ...
- Docker背后的内核知识——cgroups资源限制(转)
时间 2015-04-20 21:10:00 InfoQ 原文 http://www.infoq.com/cn/articles/docker-kernel-knowledge-cgroups-re ...
- 基于shiro+jwt的真正rest url权限管理,前后端分离
代码地址如下:http://www.demodashi.com/demo/13277.html bootshiro & usthe bootshiro是基于springboot+shiro+j ...
- oracle将一个表中字段的值赋值到另一个表中字段(批量)
面积表中数据错误,现将面积表中的sfmj字段的值改为居民信息表中匹配字段的值 update (select s.name name1,s2.name name2 from simple s,simpl ...