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 ...
随机推荐
- 数据可视化——matplotlib(2)
导入相关模块 import matplotlib.pyplot as plt import numpy as np import pandas as pd 图表设置 添加X.Y轴标签以及图标标题 a ...
- mac下cordova的ios-deploy安装问题
mac下进行cordova项目编译部署到ios设备,这个时候需要安装ios-deploy,会失败: npm WARN lifecycle ios-deploy@1.8.6~preinstall: ca ...
- 初识HTML和CSS
HTML 1.一套规则,浏览器认识的规则. 2.开发者: 学习Html规则 开发后台程序: - 写Html文件(充当模板的作用) ****** - 数据库获取数据,然后替换到html文件的指定位置(W ...
- Kali之aircrack-ng
本机装好设备及驱动 电脑本机装好Realtek RTL8187 Wireless驱动连接好USB无线驱动 把设备转接给虚拟机 win+R,启动VMware USB Arbitration Servic ...
- js判断回车,判断焦点控件
document.onkeydown=function(event){ e = event ? event :(window.event ? window.event : null); ...
- Date类型
1.创建日期对象 var now = new Date(); var someDate = new Date(Date.parse("May 25, 2004")); var so ...
- es6 nodejs compose
const compose = (...fns) => { let len = fns.length; let fn_index = len - 1; let fn_result; functi ...
- C# 设计模式巩固笔记 - 建造者模式
前言 写给自己-贵在坚持.建造者模式不复杂,但是想个形象的例子好难. 介绍-建造者模式 定义:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 实现 建造者模式主要是应对复杂 ...
- rsync的服务端和客户端搭建
首先要看看有没有rsync,没有就按装一个rsync 1配置文件 然后创建rsyncd.conf文件,并添加如下内容(文件默认不存在) [root@chensiqi2 backup]# cat /et ...
- 判断Git是否有新的提交
公司要搭建CI,有这样一个需求:判断Git是否有新的提交,如果有的话拉取代码构建,如果没有不构建,Jenkins的搭建这里就不赘述了,主要讲一下判断这里. Jenkins需要安装插件Condition ...