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
#include<iostream>
#include<cstdio>
using namespace std;
int N, M, tree[];
int isMaxheap(){
for(int k = N / ; k >= ; k--){
int max;
if(*k + <= N && tree[*k+] > tree[*k]){
max = tree[*k + ];
}else{
max = tree[*k];
}
if(tree[k] < max)
return ;
}
return ;
}
int isMinheap(){
for(int k = N / ; k >= ; k--){
int min;
if(*k+ <= N && tree[*k+] < tree[*k]){
min = tree[*k+];
}else min = tree[*k];
if(tree[k] > min)
return ;
}
return ;
}
int cnt = ;
void postOrder(int root){
if(root > N)
return;
postOrder(root*);
postOrder(root* + );
cnt++;
if(cnt == N)
printf("%d\n", tree[root]);
else printf("%d ", tree[root]);
}
int main(){
scanf("%d%d", &M, &N);
for(int i = ; i < M; i++){
for(int i = ; i <= N; i++){
scanf("%d", &tree[i]);
}
if(isMaxheap()){
printf("Max Heap\n");
}else if(isMinheap()){
printf("Min Heap\n");
}else{
printf("Not Heap\n");
}
cnt = ;
postOrder();
}
cin >> N;
return ;
}

总结:

1、题意:检查每个给出的序列是否是一个大根堆或者小根堆。

A1147. Heaps的更多相关文章

  1. PAT A1147 Heaps (30 分)——完全二叉树,层序遍历,后序遍历

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

  2. PAT_A1147#Heaps

    Source: PAT A1147 Heaps (30 分) Description: In computer science, a heap is a specialized tree-based ...

  3. PAT (Advanced Level) Practice(更新中)

    Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...

  4. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

  5. CodeForces 353B Two Heaps

    B. Two Heaps   Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily cho ...

  6. 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 ...

  7. CSU 1616: Heaps(区间DP)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1616 1616: Heaps Time Limit: 2 Sec  Memory Lim ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 虚拟机的ip地址为什么会发生变化

    因为虚拟机在NAT模式下由Vmware8虚拟网卡提供虚拟机的IP分配,网桥模式下由Vmware1来提供IP分配.它们都相当于 一个小型的DHCP服务器,除非改动虚拟机的网络连接方式,或动了虚拟网卡服务 ...

  2. SpringBoot 4.SpringBoot 整合 devtools 实现热部署

    一.添加 devtools 依赖 <!-- Spring boot 热部署 : 此热部署会遇到 java.lang.ClassCastException 异常 --> <!-- op ...

  3. zsh & tree & macOS

    zsh & tree & macOS https://unix.stackexchange.com/questions/22803/counting-files-in-leaves-o ...

  4. 转 利用java反射实现两个具有相同属性bean赋值

    package com.dobn.bdgcgl.utils; import java.lang.reflect.Field; import java.lang.reflect.Method; publ ...

  5. Hack You CTF 2014: NotEasyTask

    exe文件,运行后闪退,peid查一下,发现是是c#写的 Reflector打开 找到Main: private static void Main(string[] args) { string ho ...

  6. hdu 5652(并查集)

    题意:很久之前,在中国和印度之间有通路,通路可以简化为一个n*m的字符串,0表示能通过,1表示障碍,每过一年就有一个坐标变成1,问你什么时候,通路彻底无法通过: 解题思路:无向图的连通性,一般直接搜索 ...

  7. 【Python】一份非常好的Matplotlib教程

    Matplotlib 教程 本文为译文,原文载于此,译文原载于此.本文欢迎转载,但请保留本段文字,尊重作者和译者的权益.谢谢.: ) 介绍 Matplotlib 可能是 Python 2D-绘图领域使 ...

  8. 晨读笔记:CSS3选择器之属性选择器

    一.属性选择器 1.E[foo^="bar"]:该属性选择器描述的是选择属性以bar开头的元素,如: //所有以名称g_开头的div的字体颜色为红色div[name^=" ...

  9. AHOI2013 差异 【后缀数组】

    题目分析: 求出height以后很明显跨越最小height的一定贡献是最小height,所以对于区间找出最小height再将区间对半分. 代码: #include<bits/stdc++.h&g ...

  10. centos6.8下安装破解quartus prime16.0以及modelsim ae安装

    前言 装逼使用 流程 安装modelsim: 1.modelsim ae在linux下是32位的,对于64位系统需要安装32位库:yum install xulrunner.i686 2.给予权限: ...