1147. Heaps (30)
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)的更多相关文章
- PAT 甲级 1147 Heaps (30 分) (层序遍历,如何建树,后序输出,还有更简单的方法~)
1147 Heaps (30 分) In computer science, a heap is a specialized tree-based data structure that sati ...
- PAT Advanced 1147 Heaps (30) [堆,树的遍历]
题目 In computer science, a heap is a specialized tree-based data structure that satisfies the heap pr ...
- [PAT] 1147 Heaps(30 分)
1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...
- PAT 1147 Heaps[难]
1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...
- 1147 Heaps
1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...
- PAT甲级——1147 Heaps【30】
In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...
- PAT 1147 Heaps
https://pintia.cn/problem-sets/994805342720868352/problems/994805342821531648 In computer science, a ...
- 天梯赛L2-006. 树的遍历L3-010. 是否完全二叉搜索树
L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...
- PAT甲级 堆 相关题_C++题解
堆 目录 <算法笔记>重点摘要 1147 Heaps (30) 1155 Heap Paths (30) <算法笔记> 9.7 堆 重点摘要 1. 定义 堆是完全二叉树,树中每 ...
随机推荐
- window下php5安装redis扩展 设置自启动服务
最近想在5.6版本的开发环境装一下redis的扩展,结果找了半天都是失效链接,特此做下备份 5.3-5.6 https://pecl.php.net/package/redis/2.2.7/windo ...
- git介绍和常用指令
Git介绍和常用指令 介绍:Git和SVN一样都是版本控制工具.不同的是Git是分布式的,SVN是集中式的.Git开始用可能感觉难点,等你用习惯了你就会觉得svn是有点恐怖.(如果一个项目有好多人一起 ...
- [转]springmvc常用注解标签详解
1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ...
- [Sdoi2013]随机数生成器(BSGS)
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #inclu ...
- Count(二维树状数组)
[bzoj1452][JSOI2009]Count Description Input Output Sample Input Sample Output 12 HINT 题解:对于每一个颜色建一 ...
- 低秩近似 low-rank approximation
- GPL 与 LGPL 扫盲
本文部分摘自评论:从射手QQ之争看开源许可证的选择 首先,开源并不代表放弃自身的权力,相反,开源软件之所以存在,正是它非常注重这种权力,并且把这种权力赋予了软件的所有使用者.小心的选择许可证是开发开 ...
- Delphi 对话框实现源码分析
Delphi 对话框实现源码分析 简介 在这篇文章中,我将大概的从Delphi XE2 的Dialogs单元入手,分析ShowMessage,MessageBox等对话框运行原理,希望能帮助你理解 ...
- [note]高精度模板
高精度模板 先定义一个struct struct gj{ int l,s[N]; bool fh; void Print(){ if(fh)putchar('-'); for(int i=l;i> ...
- Python中为什么要使用线程池?如何使用线程池?
系统处理任务时,需要为每个请求创建和销毁对象.当有大量并发任务需要处理时,再使用传统的多线程就会造成大量的资源创建销毁导致服务器效率的下降.这时候,线程池就派上用场了.线程池技术为线程创建.销毁的开销 ...