PAT 05-树6 Path in a Heap
这次的作业完全是依葫芦画瓢,参照云课堂《数据结构》(http://mooc.study.163.com/learn/ZJU-1000033001#/learn/content)中何钦铭老师课件中有关建堆及插入的内容,再加上自己写的一个矬函数(竟然传了4个参数),OK了!题设要求及代码实现如下
/*
Name:
Copyright:
Author:
Date: 05/04/15 19:34
Description:
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
*/ #include <stdio.h>
#include <stdlib.h> #define MinData -10001 typedef struct HeapStruct
{
int * Elements;
int Size;
int Capacity;
} * MinHeap; MinHeap Create(int MaxSize);
void Insert(MinHeap H, int item);
void Print(MinHeap H, int * a, int N, int M); int main()
{
// freopen("in.txt", "r", stdin); // for test
int N, M, i, item;
MinHeap H; scanf("%d%d", &N, &M); H = Create(N);
for(i = ; i < N; i++)
{
scanf("%d", &item);
Insert(H, item);
} int a[M]; for(i = ; i < M; i++)
scanf("%d", &a[i]); Print(H, a, N, M);
// fclose(stdin); // for test
return ;
} MinHeap Create(int MaxSize)
{
MinHeap H = (MinHeap)malloc(sizeof(struct HeapStruct));
H->Elements = (int *)malloc((MaxSize + ) * sizeof(int));
H->Size = ;
H->Capacity = MaxSize;
H->Elements[] = MinData; return H;
} void Insert(MinHeap H, int item)
{
int i; i = ++H->Size;
for(; H->Elements[i / ] > item; i /= )
H->Elements[i] = H->Elements[i / ];
H->Elements[i] = item;
} void Print(MinHeap H, int * a, int N, int M)
{
int i; for(i = ; i < M; i++)
{
if(a[i] <= N)
{
while(a[i])
{
printf("%d", H->Elements[a[i]]);
if(a[i] > )
printf(" ");
else
printf("\n");
a[i] /= ;
}
}
}
}
PAT 05-树6 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 ...
- PAT005 Path in a Heap
题目: Insert a sequence of given numbers into an initially empty min-heap H. Then for any given index ...
- 笛卡尔树 POJ ——1785 Binary Search Heap Construction
相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS Memory Limit: 30000K Total Subm ...
- PAT甲级 树 相关题_C++题解
树 目录 <算法笔记>重点摘要 1004 Counting Leaves (30) 1053 Path of Equal Weight (30) 1079 Total Sales of S ...
- PAT 03-树1 树的同构 (25分)
给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是"同构"的.例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A.B.G的左右孩子互换后 ...
- pat L2-006. 树的遍历
L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...
- PAT (Advanced Level) 1053. Path of Equal Weight (30)
简单DFS #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- 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 ...
随机推荐
- Question store (Repeated review)
题目36 - ACM在线评测系统http://acm.nyist.net/JudgeOnline/problem.php?pid=36 用户名密码INVATION 讲道理太卡 第一:要注意不同的函数 ...
- Android Studio Gradle构建脚本
Gradle是一种依赖管理工具,基于Groovy语言,面向Java应用为主,它抛弃了基于XML的各种繁琐配置,取而代之的是一种基于Groovy的内部领域特定(DSL)语言. 构建工具就是对你的项目进行 ...
- web基础之Structs(一篇)
为什么有 struts 框架 Struct 的优点之处: 1. struct的好处 2. 程序更加规范化 3. 程序的可读性提 ...
- HDU----(2157)How many ways??(快速矩阵幂)
How many ways?? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hdu----(2848)Repository(trie树变形)
Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- javasE学习笔记:关键字super的使用
/* super 的作用: 1 . super可以在子类中引用父类的成员,通过 .的方式和属性.this相对应. 2. 在子类的构造方法中可以可使用super(参数列表)语句调用父类的构造方法 3. ...
- struts过滤器和拦截器的区别
拦截器的工作原理:当接收到一个httprequest ,a) 当外部的httpservletrequest到来时 b) 初始到了servlet容器 传递给一个标准的过滤器链 c) FilterDisp ...
- java json 的生成和解析 --json-lib
类(java json的解析和生成): import java.util.HashMap; import java.util.Map; import net.sf.json.JSONArray; im ...
- BZOJ1722 [Usaco2006 Mar] Milk Team Select 产奶比赛
直接树形dp就好了恩 令$f[i][j][t]$表示以$i$为根的子树,选出来的点存在$j$对父子关系,$t$表示$i$这个点选或者没选,的最大产奶值 分类讨论自己和儿子分别有没有选,然后转移一下就好 ...
- 编程思考 PetShop读后感
标准,插拔式的设计思想建立一致的标准是通向“复用”的通道.分层,使其得到的充分的独立.一个东西如果独立了[不是孤立],这个事物就具有很强大的力量,这个和一个人的成长是相同的道理.所以呢,在写程序的过程 ...