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

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))

One thing for sure is that all the keys along any path from the root to a leaf in a max/min heap must be in non-increasing/non-decreasing order.

Your job is to check every path in a given complete binary tree, in order to tell if it is a heap or not.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (1<N≤1,000), the number of keys in the tree. Then the next line 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, first print all the paths from the root to the leaves. Each path occupies a line, with all the numbers separated by a space, and no extra space at the beginning or the end of the line. The paths must be printed in the following order: for each node in the tree, all the paths in its right subtree must be printed before those in its left subtree.

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

Sample Input 1:

8
98 72 86 60 65 12 23 50

Sample Output 1:

98 86 23
98 86 12
98 72 65
98 72 60 50
Max Heap

Sample Input 2:

8
8 38 25 58 52 82 70 60

Sample Output 2:

8 25 70
8 25 82
8 38 52
8 38 58 60
Min Heap

Sample Input 3:

8
10 28 15 12 34 9 8 56

Sample Output 3:

10 15 8
10 15 9
10 28 34
10 28 12 56
Not Heap
 

代码:

#include <bits/stdc++.h>
using namespace std; const int maxn = 1e5 + 10;
int N;
int a[maxn];
vector<int> v; void dfs(int st) {
if(st * 2 > N && st * 2 + 1 > N) {
if(st <= N) {
for(int i = 0; i < v.size(); i ++) {
printf("%d", v[i]);
printf("%s", i != v.size() - 1 ? " " : "\n");
}
}
} else {
v.push_back(a[st * 2 + 1]);
dfs(st * 2 + 1);
v.pop_back();
v.push_back(a[st * 2]);
dfs(st * 2);
v.pop_back();
}
} int main() {
scanf("%d", &N);
for(int i = 1; i <= N; i ++)
scanf("%d", &a[i]);
v.push_back(a[1]);
dfs(1); int MaxHeap = 1, MinHeap = 1;
for(int i = 2; i <= N; i ++) {
if(a[i / 2] > a[i]) MinHeap = 0;
if(a[i / 2] < a[i]) MaxHeap = 0;
} if(MaxHeap == 1)
printf("Max Heap\n");
else if(MinHeap == 1)
printf("Min Heap\n");
else printf("Not Heap\n");
return 0;
}

  dfs 搜索路径 然后根据最大堆最小堆的性质判断 刚刚好上午写好了堆排序

PAT 甲级 1155 Heap Paths的更多相关文章

  1. pat甲级 1155 Heap Paths (30 分)

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

  2. PAT甲级 1155 Heap Paths (30分) 堆模拟

    题意分析: 给出一个1000以内的整数N,以及N个整数,并且这N个数是按照完全二叉树的层序遍历输出的序列,输出所有的整条的先序遍历的序列(根 右 左),以及判断整棵树是否是符合堆排序的规则(判断是大顶 ...

  3. PAT Advanced 1155 Heap Paths (30 分)

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

  4. PAT Advanced 1155 Heap Paths (30) [DFS, 深搜回溯,堆]

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

  5. PTA 1155 Heap Paths (DFS)

    题目链接:1155 Heap Paths (30 分) In computer science, a heap is a specialized tree-based data structure t ...

  6. 1155 Heap Paths (30 分)(堆+dfs遍历)

    比较简单的一题 遍历左右的时候注意一下 #include<bits/stdc++.h> using namespace std; ; ]; ; vector<int>t; ve ...

  7. 1155 Heap Paths

    题干前半略. Sample Input 1: 8 98 72 86 60 65 12 23 50   Sample Output 1: 98 86 23 98 86 12 98 72 65 98 72 ...

  8. PAT甲级 堆 相关题_C++题解

    堆 目录 <算法笔记>重点摘要 1147 Heaps (30) 1155 Heap Paths (30) <算法笔记> 9.7 堆 重点摘要 1. 定义 堆是完全二叉树,树中每 ...

  9. PAT甲级1098. Insertion or Heap Sort

    PAT甲级1098. Insertion or Heap Sort 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.在每次迭代中,插入排序从输入数据中删除一个元素 ...

随机推荐

  1. Mysql linux 安装文档

    1.安装依赖包 yum -y install gcc-c++ ncurses-devel cmake make perl gcc autoconf automake zlib libxml libgc ...

  2. 【转载】COM 组件设计与应用(二)——GUID 和 接口

    原文:http://vckbase.com/index.php/wv/1203.html COM 组件设计与应用 系列文章:http://vckbase.com/index.php/piwz?& ...

  3. 【HNOI2015】落忆枫音

    题面 题解 求一个有特殊性质的有向图的生成树的个数. 首先,有向图的生成树的个数可以用矩阵树定理,能够得到\(40\)分. 但是如果它是一个\(\mathrm{DAG}\)就很好做,枚举每一个点的父亲 ...

  4. C#,清晨随手写

    关于昨晚“猜拳”的博客   大家一定要记得,C#的书写规范是很严格的   很严格很严格很严格  简单的说  下面这样就没办法取值 但是这样就可以取值 插眼,开撸

  5. 【日常训练】数据中心(CSP 201812-4)

    分析 题目实际上是在要在给定的边上构建出一个树,使得这个树的最长边尽可能小. 这实际上是最小生成树的性质(反证法).问题从而得到解决. 代码 /* * Code name => csp20181 ...

  6. WordPress函数wp_page_menu详解

    说明 该标签显示带有链接的WordPress页面列表,并且可以选择将 Home(主页)自动显示为列表中的一员.该标签是自定义侧边栏和标题栏的好帮手,同时还可以用在其它模板中. WordPress教程 ...

  7. CTF MISC-USB流量分析出题记录

    USB流量分析 USB接口是目前最为通用的外设接口之一,通过监听该接口的流量,可以得到很多有意思的东西,例如键盘击键,鼠标移动与点击,存储设备的明文传输通信.USB无线网卡网络传输内容等. 1.USB ...

  8. 利用VS2015开发python版本的caffe应用

    打开VS2015,选择“新建项目”->“其它语言”->“python”,VS会提示你安装PTVS(Python Tools for Visual Studio)插件,安装完毕后即可开始py ...

  9. 2018.4.23 linux系统

    linux: 1.代表linux的内核 2.代表linux的操作系统:linux的内核和工具软件.应用软件..办公工具.开发工具. 它的特点: 1.它是开源软件,时当今最成功的开源软件之一.所以很多的 ...

  10. SICP读书笔记 3.1

    SICP CONCLUSION 让我们举起杯,祝福那些将他们的思想镶嵌在重重括号之间的Lisp程序员 ! 祝我能够突破层层代码,找到住在里计算机的神灵! 目录 1. 构造过程抽象 2. 构造数据抽象 ...