pat甲级 1155 Heap Paths (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))
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), the number of keys in the tree. Then the next line contains Ndistinct 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 <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAX 1000
#define DMAX 10000
using namespace std;
typedef long long ll;
int n;
int big,small;
int heap[MAX + ],t[MAX + ];
void print(int h) {
for(int i = ;i <= h;i ++) {
printf("%d",t[i]);
putchar(i < h ? ' ' : '\n');
}
}
void traversal(int k,int h) {
if(k > n) {
return ;
}
t[h] = heap[k];
if(k * > n) print(h);
if(h) {
if(t[h] > t[h - ]) small = ;
else if(t[h] < t[h - ]) big = ;
}
traversal(k * + ,h + );
traversal(k * ,h + );
}
int main() {
scanf("%d",&n);
for(int i = ;i <= n;i ++) {
scanf("%d",&heap[i]);
}
traversal(,);
if(big == small) puts("Not Heap");
else if(big) puts("Max Heap");
else puts("Min Heap");
}
pat甲级 1155 Heap Paths (30 分)的更多相关文章
- PAT甲级 1155 Heap Paths (30分) 堆模拟
题意分析: 给出一个1000以内的整数N,以及N个整数,并且这N个数是按照完全二叉树的层序遍历输出的序列,输出所有的整条的先序遍历的序列(根 右 左),以及判断整棵树是否是符合堆排序的规则(判断是大顶 ...
- PAT Advanced 1155 Heap Paths (30 分)
In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...
- PAT 甲级 1155 Heap Paths
https://pintia.cn/problem-sets/994805342720868352/problems/1071785408849047552 In computer science, ...
- PAT Advanced 1155 Heap Paths (30) [DFS, 深搜回溯,堆]
题目 In computer science, a heap is a specialized tree-based data structure that satisfies the heap pr ...
- PAT 甲级 1080 Graduate Admission (30 分) (简单,结构体排序模拟)
1080 Graduate Admission (30 分) It is said that in 2011, there are about 100 graduate schools ready ...
- PAT 甲级 1072 Gas Station (30 分)(dijstra)
1072 Gas Station (30 分) A gas station has to be built at such a location that the minimum distance ...
- PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***
1049 Counting Ones (30 分) The task is simple: given any positive integer N, you are supposed to co ...
- PAT 甲级 1030 Travel Plan (30 分)(dijstra,较简单,但要注意是从0到n-1)
1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, to ...
- PAT 甲级 1026 Table Tennis (30 分)(坑点很多,逻辑较复杂,做了1天)
1026 Table Tennis (30 分) A table tennis club has N tables available to the public. The tables are ...
随机推荐
- 我的Ansible学习笔记
Ansible常见错误 http://afewbug.com/article/26官方文档 http://docs.ansible.com/ansible/安装配置 http://sofar.blog ...
- MySQL安装的N种方式
一.二进制包安装 1.)下载:在官网的下载页面下的服务器操作系统选择 Linux- Generic : 进制分发版的格式是:mysql-<版本>-<OS>-tar.gz 2. ...
- 一个很有用的树形控件----zTree
演示地址 http://www.treejs.cn/v3/demo.php#_101
- CAJ2PDF
该项目不成熟,很容易遇到转换失败的例子. https://github.com/JeziL/caj2pdf https://github.com/JeziL/caj2pdf/wiki caj2pdf ...
- 【Demo】jQuery 表格内容动态排序
实现功能: 通过点击表头某个字段,实现内容的升序或降序排序. 效果如下: 完整代码: <!DOCTYPE html> <html> <head> <meta ...
- C#/Java 程序员转GO/golang程序员笔记大全(day 01)
前言: 整理一下学习 Go 语言的笔记,作为一名老程序,学习一名新的开发语言自然不需要像小白那样从 HelloWorld 看起. 简单整理一下 Go 的一些差异处,希望对大家学习 go 有点帮助,不正 ...
- Mysql之select
SELECT {*|<字段列表>} [ FROM <表1>,<表2>,…,<表n> WHERE <表达式> GROUP BY ...
- Qt5.3中qml ApplicationWindow设置窗口无边框问题
这个版本的qt在这里有点bug.. 设置ApplicationWindow的flags属性为Qt.FramelessWindowHint的确可以使程序无边框,但是同时程序在任务栏的图标也没了. 看文档 ...
- IOS-CocoaPods制作篇
作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/20067595 转载请注明出处 如果觉得文章对你有所帮助,请通过留言或关 ...
- 无密码登陆的ssh和ssh-agent
原文地址:http://lxshopping.blog.51cto.com/4542643/1179864/ 一,不需要输密码的ssh 原理:首先服务器端把公钥传给Client端,Client端在验证 ...