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 ...
随机推荐
- 学编程,学单词.....在学习中积累自己的单词(不断更新__ing)
可以去肆意大话天下,可以去小民一般的言语,但是一定要清楚,知识的积累,至于心中,即便你说这粗俗的话,你的个性,气质依旧在那,比如北大的那啥教师(心中的典范),也只有这样,你才能低至市井,上至高阁... ...
- 20145236 冯佳 《Java程序设计》第2周学习总结
20145236 <Java程序设计>第2周学习总结 教材学习内容总结 一.Java的基本类型. 在Java中的基本类型主要可区分为整数.字节.浮点数字符与布尔. •整数: 类型 长度 范 ...
- MASS批量维护
T-CODE: MASS 批量更改MASS_CHARVAL 特征的批量维护MASS_EINE 信息记录的成批维护MASS_EKKO 采购订单的成批维护MASS_MARC 后勤/配送的成批维护MASS_ ...
- C++实现对树的创建和前中后序遍历
#include<iostream>#include<stdio.h> using namespace std; class BitNode{ public: char dat ...
- 问题解决The connection to adb is down, and a severe error has occured.
遇到问题描述: 运行android程序控制台输出 [2013-06-25 11:10:32 - MyWellnessTracker] The connection to adb is down, an ...
- css3 keyframes animation
html,body,div,span,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,address,big,cite,code,del,em,font,img ...
- Asp.Net MVC EF各版本区别
2009年發行ASP.NET MVC 1.0版 2010年發行ASP.NET MVC 2.0版,VS2010 2011年發行ASP.NET MVC 3.0版+EF4,需要.Net4.0支持,VS2 ...
- JBoss JMX登录需要用户名密码的解决办法
/opt/jboss/eap5.1.2/jboss-as/server/default/conf/props/jmx-console-users.properties 取消#admin=admin的注 ...
- NetworkComms网络通信框架V3结构图
NetworkComms网络通信框架序言 来自英国的c#网络通信框架,历时五年打造,由英国剑桥的2位工程师倾情开发,最新版本V3.x版本.
- FZU 2032 Log函数问题 模拟小数加法
题目链接:Log函数问题 2 / 49 Problem G FZU 2032 Log函数问题 不知道为什么...比赛时高精度难倒了一票人...成功搞出大新闻... 试了一下直接double相加超时,然 ...