Source:

PAT A1147 Heaps (30 分)

Description:

In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to (in a min heap) the key of C. A common implementation of a heap is the binary heap, in which the tree is a complete binary tree. (Quoted from Wikipedia at https://en.wikipedia.org/wiki/Heap_(data_structure))

Your job is to tell if a given complete binary tree is a heap.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers: M (≤100), the number of trees to be tested; and N (1 < N ≤ 1,000), the number of keys in each tree, respectively. Then M lines follow, each contains N distinct integer keys (all in the range of int), which gives the level order traversal sequence of a complete binary tree.

Output Specification:

For each given tree, print in a line Max Heap if it is a max heap, or Min Heap for a min heap, or Not Heap if it is not a heap at all. Then in the next line print the tree's postorder traversal sequence. All the numbers are separated by a space, and there must no extra space at the beginning or the end of the line.

Sample Input:

3 8
98 72 86 60 65 12 23 50
8 38 25 58 52 82 70 60
10 28 15 12 34 9 8 56

Sample Output:

Max Heap
50 60 65 72 12 23 86 98
Min Heap
60 58 52 38 82 70 25 8
Not Heap
56 12 34 28 9 8 15 10

Keys:

Attention:

  • 有点水了哈0,0;

Code:

 /*
Data: 2019-06-29 16:15:43
Problem: PAT_A1147#Heaps
AC: 19:20 题目大意:
判断给定的完全二叉树是否是堆
输入:
第一行给出测试数M(<=100)和结点数N[1,1e3]
接下来N行,逐层给出完全二叉树的各个键值
输出:
大根堆,小根堆,非堆;
接下来输出二叉树的后序遍历 基本思路:
静态树存储BST,遍历判断是否为堆
*/
#include<cstdio>
#include<vector>
using namespace std;
const int M=1e3+;
int bst[M],Min,Max,n,m;
vector<int> path; void Travel(int root)
{
if(root > n)
return;
if(root!=)
{
if(bst[root/] > bst[root])
Min=;
if(bst[root/] < bst[root])
Max=;
}
Travel(root*);
Travel(root*+);
path.push_back(bst[root]);
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE scanf("%d%d", &m,&n);
while(m--)
{
for(int i=; i<=n; i++)
scanf("%d", &bst[i]);
Min=;
Max=;
path.clear();
Travel();
if(Max) printf("Max Heap\n");
else if(Min) printf("Min Heap\n");
else printf("Not Heap\n");
for(int i=; i<n; i++)
printf("%d%c", path[i],i==n-?'\n':' ');
} return ;
}

PAT_A1147#Heaps的更多相关文章

  1. CodeForces 353B Two Heaps

    B. Two Heaps   Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily cho ...

  2. DSP\BIOS调试Heaps are enabled,but not set correctly

    转自:http://blog.sina.com.cn/s/blog_735f291001015t9i.html Heaps are enabled, but the segment for DSP/B ...

  3. CSU 1616: Heaps(区间DP)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1616 1616: Heaps Time Limit: 2 Sec  Memory Lim ...

  4. Codeforces Round #300 F - A Heap of Heaps (树状数组 OR 差分)

    F. A Heap of Heaps time limit per test 3 seconds memory limit per test 512 megabytes input standard ...

  5. Heaps(Contest2080 - 湖南多校对抗赛(2015.05.10)(国防科大学校赛决赛-Semilive)+scu1616)

    Problem H: Heaps Time Limit: 2 Sec  Memory Limit: 128 MBSubmit: 48  Solved: 9[Submit][Status][Web Bo ...

  6. Fibonacci Heaps

    Mergeable heapsA mergeable heap is any data structure that supports the following five operations,in ...

  7. PAT 1147 Heaps[难]

    1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...

  8. [PAT] 1147 Heaps(30 分)

    1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...

  9. Codeforces 538 F. A Heap of Heaps

    \(>Codeforces \space 538 F. A Heap of Heaps<\) 题目大意 :给出 \(n\) 个点,编号为 \(1 - n\) ,每个点有点权,将这些点构建成 ...

随机推荐

  1. MyBatis 3模糊查询(like)写法(转)

    说明:以下写法可以同时支持XML和注解的形式. 1.SQL中字符串拼接 SELECT * FROM tableName WHERE name LIKE CONCAT(CONCAT('%', #{tex ...

  2. Oracle-统计数据库表数据总数量

    create or replace procedure prc_table_count(p_flag out varchar2) AS TCOUNT number; SCOUNT number; CO ...

  3. mysql 源码 王恒 [mysql数据结构+mysql 执行计划]

    http://blog.chinaunix.net/uid/26896862/cid-156733-list-1.html http://www.it168.com/redian/wangheng/

  4. MySQL:解决MySQL无法启动的问题

    MySQL无法启动的原因有多种,这里是我遇到的一种情况和解决方法. 起因: 最近项目需要使用MySQL,于是想在MAC上安装一个本地的数据库,但是其实忘了已经安装过一个版本了,结果发现新的服务器怎么也 ...

  5. Androd自己定义控件(三)飞翔的小火箭

    在前面的自己定义控件概述中已经跟大家分享了Android开发其中自己定义控件的种类. 今天跟大家分享一个非主流的组合控件. 我们在开发其中,难免须要在不同的场合中反复使用一些控件的组合.而Java的最 ...

  6. HDU 5245 上海大都会 J题 (概率期望)

    这道题的概率可以单独考虑每个格子对期望的贡献值.因为其实每个格子是否被选都可以认为是独立的,单独一个格子贡献的期望为1*(该格子K次被选的概率),所以答案其实就是每个格子K次被选中的概率之和. #in ...

  7. Java学习笔记之 IO包 字节流

    IO包最重要的五个类和一个接口 File/OutputStream/InputStream(字节流)/Writer/Reader(字符流) 一个接口:Serializable   File类: 字节流 ...

  8. Android Gallery2源代码分析

    打开图库中图片为什么从模糊变清晰 1. 有一点要明白,图片要进行显示,首先要先将图片进行decode,然后才干显示 2. 图片decode须要时间,越大的图片,细节越多的图片,那么它decode时间就 ...

  9. 加州理工学院公开课:机器学习与数据挖掘_Regularization(第十二课)

    课程简单介绍: 接上一节课,这一节课的主题是怎样利用 Regularization 避免 Overfitting.通过给如果集设定一些限制条件从而避免  Overfitting,可是如果限制条件设置的 ...

  10. 一个jeecg整合activiti的学习样例,源代码下载

    社区成员:刘京华採用技术:jeecg+ activiti源代码下载地址:http://pan.baidu.com/s/1dDxOHrV 截图演示:  2.jpg (71.81 KB, 下载次数: 0) ...