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. 定义 堆是完全二叉树,树中每 ...
随机推荐
- SpringMVC基于代码的配置方式(零配置,无web.xml)
基于配置文件的web项目维护起来可能会更方便,可是有时候我们会有一些特殊的需求,比方防止客户胡乱更改配置,这时候我们须要给配置隐藏到代码中. 1.创建一个动态web项目(无需web.xml) 2.右键 ...
- 统计输入的单词中有几个长度大于n的,n是自己指定的,用函数对象实现
#ifndef COUNT_WORD_H #define COUNT_WORD_H #include <string.h> #include <iostream> #inclu ...
- kubernetes高级之集群中使用sysctls
系列目录 在linux系统里,sysctls 接口允许管理员在运行时修改内核参数.参数存在于/proc/sys/虚拟进程文件系统里.参数涉及到很多子模块,例如: 内核(kernel)(常见前缀kern ...
- 3354 [IOI2005]河流
题目描述 几乎整个Byteland王国都被森林和河流所覆盖.小点的河汇聚到一起,形成了稍大点的河.就这样,所有的河水都汇聚并流进了一条大河,最后这条大河流进了大海.这条大河的入海口处有一个村庄——名叫 ...
- EasyDSS流媒体服务器灵活地帮助用户实现摄像机RTSP转RTMP直播功能
简要描述 今天突然接到国内某上市公司同事打来的技术咨询电话,经过简单的沟通,大概所描述的需求是: 1.目前现场有非常多的摄像机资源需要接入: 2.需要将摄像机的RTSP流转成RTMP流接入到微信小程序 ...
- Java 迭代器
1.快速报错 public class FailFast { public static void main(String[] args) { List<String> c = new A ...
- 程序运行之ELF文件结构
ELF目标文件格式的最前部是ELF文件头.包含了整个文件的基本属性.比如ELF文件版本,目标机器型号,程序入口地址等.然后是ELF的各个段,其中ELF文件中与段有关的重要结构就是段表.段表描述了ELF ...
- java 类中定义接口的调用方法
public class Human { public interface MyAction { public void getPower(); } } public class Test{ publ ...
- Python list,tuple,dict and set
list 有序可变的集合 查找和插入的时间随着元素的增加而增加 占用空间小,浪费内存很少 tuple 有序只读不可变.因为tuple不可变,所以代码更安全.如果可能,能用tuple代替list就尽量用 ...
- mac manpages 汉化
默认在终端进行man命令,如:man ls,会显示英文的帮助文档.本文教你如何查看中文文档. 资源:1.manpages-zh-1.5.2.tar.bz22.groff-1.21.tar.gz - ...