04-树9. Path in a Heap (25)

时间限制
150 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

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

提交代码

关于堆的操作(以大顶堆为例):

1.建堆。

 MaxHeap Create( int MaxSize )
{ /* 创建容量为MaxSize的空的最大堆 */
MaxHeap H = malloc( sizeof( struct HeapStruct ) );
H->Elements = malloc( (MaxSize+) * sizeof(ElementType));
H->Size = ;
H->Capacity = MaxSize;
H->Elements[] = MaxData;
/* 定义“哨兵”为大于堆中所有可能元素的值,便于以后更快操作 */
return H;
}

2.插入。

 void Insert( MaxHeap H, ElementType item )
{ /* 将元素item 插入最大堆H, 其中H->Elements[0]已经定义为哨兵 */
int i;
if ( IsFull(H) ) {
printf("最大堆已满");
return;
}
i = ++H->Size; /* i指向插入后堆中的最后一个元素的位置 */
for ( ; H->Elements[i/] < item; i/= )
H->Elements[i] = H->Elements[i/]; /* 向下过滤结点 */
H->Elements[i] = item; /* 将item 插入 */
}

3.删除最大值。

 ElementType DeleteMax( MaxHeap H )
{ /* 从最大堆H中取出键值为最大的元素, 并删除一个结点 */
int Parent, Child;
ElementType MaxItem, temp;
if ( IsEmpty(H) ) {
printf("最大堆已为空");
return;
}
MaxItem = H->Elements[]; /* 取出根结点最大值 */
/* 用最大堆中最后一个元素从根结点开始向上过滤下层结点 */
temp = H->Elements[H->Size--];
for( Parent=; Parent*<=H->Size; Parent=Child ) {
Child = Parent * ;
if( (Child!= H->Size) &&(H->Elements[Child] < H->Elements[Child+]) )
Child++; /* Child指向左右子结点的较大者 */
if( temp >= H->Elements[Child] ) break;
else /* 移动temp元素到下一层 */
H->Elements[Parent] = H->Elements[Child];
}
H->Elements[Parent] = temp;
return MaxItem;
}

代码如下:

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<map>
#include<string>
using namespace std;
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n,m;
scanf("%d %d",&n,&m);
int i,j,temp,size=;
int *minheap=new int[n+];
for(i=;i<=n;i++){
scanf("%d",&temp);
minheap[++size]=temp;
for(j=size;j>=;j/=){
if(temp<minheap[j/]){
minheap[j]=minheap[j/];
}
else{
break;
}
}
minheap[j]=temp;
}
for(i=;i<m;i++){
scanf("%d",&temp);
while(temp){
printf("%d",minheap[temp]);
temp==?printf("\n"):printf(" ");
temp/=;
}
}
return ;
}

pat04-树9. Path in a Heap (25)的更多相关文章

  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. PAT 05-树6 Path in a Heap

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

  3. PAT005 Path in a Heap

    题目: Insert a sequence of given numbers into an initially empty min-heap H. Then for any given index ...

  4. 笛卡尔树 POJ ——1785 Binary Search Heap Construction

    相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS   Memory Limit: 30000K Total Subm ...

  5. LeetCode之“树”:Path Sum && Path Sum II

    Path Sum 题目链接 题目要求: Given a binary tree and a sum, determine if the tree has a root-to-leaf path suc ...

  6. 【树】Path Sum II(递归)

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

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

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

  8. WPF 组织机构下拉树多选,递归绑定方式现实

    使用HierarchicalDataTemplate递归绑定现实 XAML代码: <UserControl x:Class="SunCreate.CombatPlatform.Clie ...

  9. 动态树之LCT(link-cut tree)讲解

    动态树是一类要求维护森林的连通性的题的总称,这类问题要求维护某个点到根的某些数据,支持树的切分,合并,以及对子树的某些操作.其中解决这一问题的某些简化版(不包括对子树的操作)的基础数据结构就是LCT( ...

随机推荐

  1. java.io.FileNotFoundException: res/drawable/title_bar_shadow.9.png

    ERROR/AndroidRuntime(803): Caused by: java.io.FileNotFoundException: res/drawable/title_bar_shadow.9 ...

  2. 说“DPI”

    作者:马健邮箱:stronghorse_mj@hotmail.com发布:2007.03.08更新:2007.04.02 目录一.基本概念二.图像文件中的DPI三.PDG文件中的DPI四.PDF文件中 ...

  3. Python如何打包EXE可执行文件

    一.安装Pyinstaller 使用Python打包EXE可执行文件时,需要使用Pyinstaller,使用命令pip install PyInstaller进行安装,如下图: 安装成功后的效果如下图 ...

  4. How can I list colors in WPF with XAML?

    How can I get list of all colors I can pick in Visual Studio Designer (which is System.Windows.Media ...

  5. 基于Ace Admin 的菜单栏实现

    1.首先是数据库表必然包含以下几个字段Id ,ParnetId,Url,Name等 create table dbo.Module ( Id uniqueidentifier not null con ...

  6. 注解和注释以及Spring和SpringMVC常用的注解

    1.两者区别 注解 :参与代码编译,以@开头的.它是给应用程序看的,单独使用注解毫无意义,一定要跟工具一起使用,这个所谓的工具实际就是能读懂注解的应用程序 注释 :对代码没有影响.对代码起到解释.说明 ...

  7. 【bzoj1853】: [Scoi2010]幸运数字 数论-容斥原理

    [bzoj1853]: [Scoi2010]幸运数字 预处理出所有幸运数字然后容斥原理 但是幸运数字是2logn个数的 直接搞会炸 所以把成倍数的处理掉 然后发现还是会T 所以数字要从大到小处理会快很 ...

  8. C语言数据结构-栈的实现-初始化、销毁、长度、取栈顶元素、查找、入栈、出栈、显示操作

    1.数据结构-栈的实现-C语言 #define MAXSIZE 100 //栈的存储结构 typedef struct { int* base; //栈底指针 int* top; //栈顶指针 int ...

  9. U19464 山村游历(Wander) LCT维护子树大小

    \(\color{#0066ff}{ 题目描述 }\) 在一个偏远的小镇上,有一些落后的山村.山村之间通过一些道路来连接.当然有的山村可能不连通. 一年当中会发生很多大事,比如说有人提议要在山村\(i ...

  10. ArcGIS-各类问题

    arcgis 10.4破解方法*注意!Desktop,Engine,Server必须为同一版本 1.先安装License10.4 2.再安装Desktop10.4 3.再安装Engine10.4 4. ...