https://pintia.cn/problem-sets/994805342720868352/problems/994805342821531648

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 <bits/stdc++.h>
using namespace std; int N, M;
vector<int> level; void postorder(int st) {
if(st >= N) return ;
postorder(st * 2 + 1);
postorder(st * 2 + 2);
printf("%d%s", level[st], st == 0 ? "\n" : " ");
} int main() {
scanf("%d%d", &M, &N);
level.resize(N);
while(M --) {
for(int i = 0; i < N; i ++)
scanf("%d", &level[i]); int flag;
if(level[0] > level[1]) flag = 1; // max
else if(level[0] < level[1]) flag = -1; // min for(int i = 0; i <= (N - 1) / 2; i ++) {
int l = i * 2 + 1, r = i * 2 + 2;
if(flag == 1 && (level[i] < level[l] || r < N && level[r] > level[i])) flag = 0;
if(flag == -1 && (level[i] > level[l] || r < N && level[r] < level[i])) flag = 0;
} if(flag == 0) printf("Not Heap\n");
else if(flag == 1) printf("Max Heap\n");
else if(flag == -1) printf("Min Heap\n"); postorder(0);
}
return 0;
}

  堆最后一层不满的话是都靠左排的 头是 level[0] 先判断 level[0] 和 level[1] 的大小关系初步假设是最大堆还是最小堆 然后进行判断 0 到 (N - 1) / 2 的点都是有孩子节点的 如果父亲节点是 index 那么左儿子是 index * 2 + 1 右儿子是 index * 2 + 2

FHFHFH

PAT 1147 Heaps的更多相关文章

  1. PAT 1147 Heaps[难]

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

  2. [PAT] 1147 Heaps(30 分)

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

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

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

  4. 1147 Heaps

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

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

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

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

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

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

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

  8. 1147. Heaps (30)

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

  9. PAT (Advanced Level) 1144~1147:1145Hash二次探查 1146拓扑排序 1147堆

    1144 The Missing Number(20 分) 题意:给定N个数的序列,输出不在序列中的最小的正整数. 分析: 1.给定的N个数可能为正,可能为负,可能重复. 2.由于N≤10​5​​,所 ...

随机推荐

  1. opencv-Drawing Functions in OpenCV

    1.opencv简单画图形 # coding = utf-8 # 画线.长方形.圆等 import numpy as np import cv2 # 返回一个数组 img = np.zeros((51 ...

  2. JNDI是什么,怎么理解

    JNDI 是什么 JNDI是 Java 命名与目录接口(Java Naming and Directory Interface),在J2EE规范中是重要的规范之一,不少专家认为,没有透彻理解JNDI的 ...

  3. 【JUC源码解析】ForkJoinPool

    简介 ForkJoin 框架,另一种风格的线程池(相比于ThreadPoolExecutor),采用分治算法,工作密取策略,极大地提高了并行性.对于那种大任务分割小任务的场景(分治)尤其有用. 框架图 ...

  4. Maven 依赖节点总结

    首先是log4j: <!--哎,神特么的log4j,版本不对就Spring AOP前置增强 new不出来--><dependency> <groupId>log4j ...

  5. Selenium2+python自动化-八种元素定位(Firebug和Firepath)

    前言    自动化只要掌握四步操作:获取元素,操作元素,获取返回结果,断言(返回结果与期望结果是否一致),最后自动出测试报告.本篇主要讲如何用firefox辅助工具进行元素定位.元素定位在这四个环节中 ...

  6. Git积累

    1.使用git config命令进行配置(此配置为全局配置,这些是在提交commit时的签名): $ git config --global user.name "填写github的用户名& ...

  7. 与(&)、或(|)等运算符理解及其特殊用途

    1.按位与运算符(&) 在与运算中两个开关是串联的,如果我们要开灯,需要两个开关都打开灯才会打开.理解为A与B都打开,则开灯,所以是1&1=1任意一个开关没打开,都不开灯,所以其他运算 ...

  8. [Unity Shader] 逐顶点光照和逐片元漫反射光照

    书中的6.4节讲的是漫反射的逐顶点光照和逐片元光照. 前一种算法是根据漫反射公式计算顶点颜色(顶点着色器),对颜色插值(光栅化过程)返回每个像素的颜色值(片元着色器). 第二种算法是获得顶点的法线(顶 ...

  9. 洛谷P1585 魔法阵

    题目传送门 这题就是一个有技巧的DFS+一大堆乱七八糟的剪枝 进行DFS时注意一下以下点 根据题意,我们可以把DFS分成两块,即1--n*m/2与n*m/2--n*m,第一块边找边记录,第二块就开始计 ...

  10. Spring Boot之发送HTTP请求(RestTemplate详解)

    原文作者:微笑面对生活 https://www.javazhiyin.com/19714.html#comment-345 RestTemplate是Spring提供的用于访问Rest服务的客户端,R ...