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. 定义 堆是完全二叉树,树中每 ...
随机推荐
- Android JNI开发之NDK环境搭建
参考:http://www.cnblogs.com/yejiurui/p/3476565.html 谷歌改良了ndk的开发流程,对于Windows环境下NDK的开发,如果使用的NDK是r7之前的版本, ...
- iOS ARC也会有内存泄露
本文转载至 http://blog.csdn.net/allison162004/article/details/38753219 iOS提供了ARC功能,很大程度上简化了内存管理的代码. 但使用A ...
- elipse开发环境搭建(Java+C++)
目的:搭建可用于java和C++开发的eclipse开发环境. 步骤: 1.安装JDK(Java Development Kit):JDK包含了java掩饰程序和样例.Java公共API类的源代码.J ...
- lua(仿类)
Account = { balance = } function Account:deposit(v) self.balance = self.balance + v end function Acc ...
- Unity 插件收集(持续更新)
MGS Machinery Unity绑定机械关节,铰链,机构插件包. MGS Mechanical Drive 用于绑定场景中的机械驱动器的Unity插件 Unity Wave Propa ...
- COGS 1507. [IOI2000]邮局
1507. [IOI2000]邮局 ★☆ 输入文件:postoffice.in 输出文件:postoffice.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] ...
- 九度OJ 1351:数组中只出现一次的数字 (位运算)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3098 解决:906 题目描述: 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 输入: 每个 ...
- tomcat日志按天切分
1. 下载工具cronolog wget http://cronolog.org/download/cronolog-1.6.2.tar.gz 这是网上流传的下载地址,好像没用,所以需要自己去网上找. ...
- "ORA-01012: not logged on"以及"Connected to an idle instance."解决思路
今天测试用的ORACLE服务器出现卡顿情况,于是准备重启一下,在运行shutdown指令关闭数据库的时候意外断开连接,后面想再次进入ORACLE服务器启动时便遇见如下报错: 使用sqlplus /no ...
- dig指定服务器查询域名解析时间
time=$(dig @8.8.8.8 baidu.com | grep Query | awk '{print $4}') echo $time 一 nslookup指定服务器查询域名解析时间 ro ...