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. sql server时间戳timestamp

    sql server时间戳timestamp 在SQL Server中联机丛书是这样说的: SQL Server timestamp 数据类型与时间和日期无关.SQL Server timestamp ...

  2. 未能加载文件或程序集“Interop.SQLDMO”或它的某一个依赖项

    程序运行没问题,当您配置好IIS后,打开安装向导时出现以下错误信息: 解决办法为:在IIS应用程序池中“启用32位应用程序”, 1,打开IIS应用程序池,选中相应应用程序池,点击右侧“高级设置” 2, ...

  3. 不准使用xib自定义控制器view的大小

    1.AppDelegate.m // // 文 件 名:AppDelegate.m // // 版权所有:Copyright © 2018年 leLight. All rights reserved. ...

  4. P1919 【模板】A*B Problem升级版(FFT快速傅里叶)

    题目描述 给出两个n位10进制整数x和y,你需要计算x*y. 输入输出格式 输入格式: 第一行一个正整数n. 第二行描述一个位数为n的正整数x. 第三行描述一个位数为n的正整数y. 输出格式: 输出一 ...

  5. 【Cracking the Code Interview(5th edition)】二、链表(C++)

    链表结点类型定义: class Node { public: ; Node *next = nullptr; Node(int d) { data = d; } }; 快行指针(runner)技巧: ...

  6. 在本地开发环境没问题,但是发布到服务器出现:未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\106f9ae8\cc0e1169\App_global.asax.haz99mum.dll”--“拒绝访问。 ”错误

    解决方法: 1,通常的解决方法:原因是由于系统目录下的Temp目录无相应的权限所致,具体操作如下:C:\Windows\temp-->属性-->安全-->编辑-->添加NETW ...

  7. python 字符串 操作

    去空格及特殊符号 s.strip().lstrip().rstrip(',') 复制字符串 #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sStr2 = sStr1 sS ...

  8. Go语言基础之14--Waitgroup和原子操作

    一.Waitgroup介绍 1.1 背景 package main import ( "fmt" "time" ) func main() { ch := ma ...

  9. SQL SERVER linked server Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'

    昨天创建了一个View, 这个view是一系列的表达式(CTE)组成,封装了好多的业务逻辑,简化下语句如下 ;with CTE AS( ...) SELECT a.company_id ,b.comp ...

  10. linux 6 安装 使用 XtraBackup

    帮助文档:https://www.cnblogs.com/imweihao/p/7290026.html ---Yum安装 官网地址:https://www.percona.com/doc/perco ...