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. 开源GIS软件 4

    空间数据操作框架 Apache SIS Apache SIS 是一个空间的框架,可以更好地搜索,数据聚类,归档,或任何其他相关的空间坐标表示的需要. kvwmap kvwmap是一个采用PHP开发的W ...

  2. Eclipse ADT 导入别的电脑开发的项目

    用Eclipse开发的时候常常要导入别的电脑开发的项目,常常会出错,甚至导入不了. 方法一: 把你正在使用的Eclipse开发的随便一个项目.打开,把下图这三个文件复制过去你要导入的项目.覆盖.然后再 ...

  3. HDU 2563 统计问题(递推)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=2563 将向上移的步数设为a[n],将向左右移的步数设为b[n],有a[n]=a[n-1]+b[n-1 ...

  4. shell EOF注意点

    当sqlplus与shell交互的时候我们这么用 su - oracle -c "sqlplus / as sysdba<<EOF select * from gv($insta ...

  5. jenkins+jmeter+ant+jmeter在Jenkins上报告

    1.jmeter+ant 参考 http://www.cnblogs.com/dieyaxianju/p/8268802.html 2.在jenkins上配置 3.执行成功 4.配置报告  参考 下载 ...

  6. 使用百度地图API进行Android地图应用开发(Eclipse)

    随着基于位置的服务的兴起,地图类App呈现爆发趋势.随着而来的是地图供应商开放大量的API.供开发人员开发基于PC或者移动端的应用程序. 如今我们研究使用百度地图SDK进行Android项目的开发. ...

  7. Mybatis 框架文档 超具体笔记

    1      Mybatis入门 1.1    单独使用jdbc编程问题总结 1.1.1  jdbc程序 Public static void main(String[] args) { Connec ...

  8. Android系统Recovery工作原理之使用update.zip升级过程分析(七)---Recovery服务的核心install_package函数【转】

    本文转载自:http://blog.csdn.net/mu0206mu/article/details/7465514 一.       Recovery服务的核心install_package(升级 ...

  9. 2749: [HAOI2012]外星人

    首先像我一样把柿子画出来或者看下hint 你就会发现其实是多了个p-1这样的东东 然后除非是2他们都是偶数,而2就直接到0了 算一下2出现的次数就好 #include<cstdio> #i ...

  10. 生活的 tricks

    1. 远距离传递 传真(需要附近有传真机):发 QQ.微信拍照,自己打印: 2. 超市的设计 如果是两层的话,入口一定在第一楼,出口在第二楼,也即当你需要出去的时候,需要贯穿整个超市: 用的在第一楼: ...