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 ...
随机推荐
- 使用ar命令删除iOS静态库重复编译的.o文件
关于 xcode引入第三方静态类库 duplicate symbol _OBJC_XXX 重复编译错误 看这里 http://www.cnblogs.com/cocoajin/p/3917709.ht ...
- python 暴力破解密码脚本
python 暴力破解密码脚本 以下,仅为个人测试代码,环境也是测试环境,暴力破解原理都是一样的, 假设要暴力破解登陆网站www.a.com 用户 testUser的密码, 首先,该网站登陆的验证要支 ...
- python 练习 2
#!/usr/bin/python # -*- coding: utf-8 -*- from random import shuffle class caigame: win=False flag=F ...
- [myeclipse] 官方中文网站
myeclipse官方中文网站:http://www.myeclipsecn.com/
- 通过chrome 获取网站的cookie信息
通过chrome 打开网页 审核元素 登录网站后 network列表中查看 home?开头列表项的 cookie信息
- Unique Paths II [LeetCode]
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 输出有序数组的中两个元素差值为指定值diff的两个元素
题目: 输出有序数组的中两个元素差值为指定值diff的两个元素. 思路: 这与输出两个元素的和的值为一定值类似,需要两个指针,不同的是:指针不是一左一右,而是一前一后. 如果差值等于diff,则返回: ...
- jdbc URL中的各个参数详解
常用的有两个,一个是gjt(Giant JavaTree)组织提供的mysql驱动,其JDBC Driver名称(Java类名)为:org.gjt.mm.mysql.Driver 详情请参见网站:ht ...
- Objective-C:Category
Category可以在不需要创建子类或是修改原始类的基础上,动态为已经存在的类添加新的行为(方法),,这样可以保证类的原始设计规模较小,功能增加时再逐步扩展:实现了类的相关方法的模块化,把不同的类方法 ...
- 如何为Kafka集群选择合适的Partitions数量
转载:http://blog.csdn.net/odailidong/article/details/52571901 这是许多kafka使用者经常会问到的一个问题.本文的目的是介绍与本问题相关的一些 ...