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 <= 1000), 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 判断最堆只需要判断从头到尾是否是一致的双亲大于(小于)左右儿子。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
#define Max 1005
using namespace std;
int n,m,h[Max],flag;
bool check(bool tag)
{
for(int i = ;i * <= n;i ++)
{
if(h[i] != h[i * ] && h[i] > h[i * ] == tag)return ;
if(i * + <= n && h[i] != h[i * + ] && h[i] > h[i * + ] == tag)return ;
}
return ;
}
void post_order(int t)
{
if(t * <= n)post_order(t * );
if(t * + <= n)post_order(t * + );
if(flag ++)printf(" %d",h[t]);
else printf("%d",h[t]);
}
int main()
{
scanf("%d%d",&m,&n);
while(m --)
{
for(int i = ;i <= n;i ++)
{
scanf("%d",&h[i]);
}
if(check(false))puts("Max Heap");
else if(check(true))puts("Min Heap");
else puts("Not Heap");
flag = ;
post_order();
puts("");
}
}

1147. Heaps (30)的更多相关文章

  1. PAT 甲级 1147 Heaps (30 分) (层序遍历,如何建树,后序输出,还有更简单的方法~)

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

  2. PAT Advanced 1147 Heaps (30) [堆,树的遍历]

    题目 In computer science, a heap is a specialized tree-based data structure that satisfies the heap pr ...

  3. [PAT] 1147 Heaps(30 分)

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

  4. PAT 1147 Heaps[难]

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

  5. 1147 Heaps

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

  6. PAT甲级——1147 Heaps【30】

    In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...

  7. PAT 1147 Heaps

    https://pintia.cn/problem-sets/994805342720868352/problems/994805342821531648 In computer science, a ...

  8. 天梯赛L2-006. 树的遍历L3-010. 是否完全二叉搜索树

    L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...

  9. PAT甲级 堆 相关题_C++题解

    堆 目录 <算法笔记>重点摘要 1147 Heaps (30) 1155 Heap Paths (30) <算法笔记> 9.7 堆 重点摘要 1. 定义 堆是完全二叉树,树中每 ...

随机推荐

  1. 在Linux中显示日历(cal)

    cal 2013    显示2013年整年日历 cal 7 2013  显示2013年 7 月 日历

  2. 已备份数据库的磁盘结构版本号为611,server支持版本号为539,无法还原或升级数据库

    提供的是bak文件是2005备份的,还原到本地的sqlserver2000,提示:已备份数据库的磁盘上结构版本号为611.服务器支持版本号539,无法还原或升级数据库. 网上找了下,原因是611是sq ...

  3. Centos 安装libreoffice 生成office 报错信息见内容

    个人博客:https://blog.sharedata.info/ 错误信息:/opt/libreoffice5.2/program/soffice.bin: error while loading ...

  4. Solr6.5查询参数说明

    q - 查询字符串,这个是必须的.如果查询所有*:* ,根据指定字段查询(Name:张三 AND Address:北京) fq - (filter query)过虑查询,作用:在q查询符合结果中同时是 ...

  5. iOS 3DES加密

    本文转载至 http://www.cocoachina.com/bbs/read.php?tid=177167 -(NSString *)TripleDES:(NSString *)plainText ...

  6. spring 事务传播行为类型

    事务传播行为种类 Spring在TransactionDefinition接口中规定了7种类型的事务传播行为, 它们规定了事务方法和事务方法发生嵌套调用时事务如何进行传播: 事务传播行为类型 说明 P ...

  7. CSS3 线性渐变(linear-gradient)

    CSS3 Gradient 分为 linear-gradient(线性渐变)和 radial-gradient(径向渐变).而我们今天主要是针对线性渐变来剖析其具体的用法.为了更好的应用 CSS3 G ...

  8. javascript中apply和call的区别

    请补充   136页  pdf  高级javascript设计

  9. 我的Android进阶之旅------>Android SDK支持的配置标识符(有用的参考文件)

    Android SDK支持的配置标致符 配置标识符 标识符值 描      述 MCC   MNC 例子: mcc310: MCC310-MNC004: MCC208-MNC00 MCC(移动国家代码 ...

  10. 【模式识别】CART和GML AdaBoost MATLAB TOOLBOX

    GML AdaBoost Matlab Toolbox是一款很优秀的AdaBoost工具箱,内部实现了Real AdaBoost, Gentle AdaBoost和Modest AdaBoost三种方 ...