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 ...
随机推荐
- Implicit Object in JSP
Implicit Object Description request The HttpServletRequest object associated with the request. respo ...
- linux中无 conio.h的解决办法
conio.h不是C标准库中的头文件,在ISO和POSIX标准中均没有定义.conio是Console Input/Output(控制台输入输出)的简写,其中定义了通过控制台进行数据输入和数据输出的函 ...
- 5.6 WebDriver API实例讲解(16-30)
16.操作单选框 被测试的网页为Demo1. Java语言版本的API实例代码: public static void operateRadio(){ driver.get("file:// ...
- spring来了-05-JDBC
概述 Spring对C3P0连接池的支持很完善 Spring对jdbc提供了JdbcTemplate,来简化jdbc操作, JdbcTemplate模板工具类,类似于DbUtils组件 JDBC: p ...
- centos7配置mono和jexus5.6.2
一.通过集成包安装mono: 1.添加Mono的 包库源: 把Mono Project public Jenkins GPG signing 导入系统 wget http://jenkins.mon ...
- ARM 汇编的一些规范
A.5.1 文件格式 ARM 源程序文件(即源文件)为文件格式,可以使用任一文本编辑器编写程序代码. 在一个项目中,至少要有一个汇编源文件或C 程序文件,可以有多个汇编 ...
- android:ems
<EditText android:id="@+id/qq_number" android:layout_width="wrap_content" and ...
- plot a critical difference diagram , MATLAB code
plot a critical difference diagram , MATLAB code 建立criticaldifference函数 function cd = criticaldiffer ...
- Objective-C:Foundation框架-常用类-NSDate
直接上代码吧: #import <Foundation/Foundation.h> #pragma mark 日期创建 void dateCreate() { // date方法返回的就是 ...
- JButton计数
1.引言 在Swing窗口中,我们时常会点击按钮进行计数,例如点击按钮A,第一次弹出窗口1,第二次弹出窗口2....以及按钮的快捷键设置. import java.awt.event.ActionEv ...