PAT 甲级 1155 Heap Paths
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的更多相关文章
- pat甲级 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 (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 Advanced 1155 Heap Paths (30) [DFS, 深搜回溯,堆]
		
题目 In computer science, a heap is a specialized tree-based data structure that satisfies the heap pr ...
 - PTA 1155 Heap Paths (DFS)
		
题目链接:1155 Heap Paths (30 分) In computer science, a heap is a specialized tree-based data structure t ...
 - 1155 Heap Paths (30 分)(堆+dfs遍历)
		
比较简单的一题 遍历左右的时候注意一下 #include<bits/stdc++.h> using namespace std; ; ]; ; vector<int>t; ve ...
 - 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 ...
 - PAT甲级 堆 相关题_C++题解
		
堆 目录 <算法笔记>重点摘要 1147 Heaps (30) 1155 Heap Paths (30) <算法笔记> 9.7 堆 重点摘要 1. 定义 堆是完全二叉树,树中每 ...
 - PAT甲级1098. Insertion or Heap Sort
		
PAT甲级1098. Insertion or Heap Sort 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.在每次迭代中,插入排序从输入数据中删除一个元素 ...
 
随机推荐
- 洛谷 P3795 钟氏映射
			
洛谷 P3795 钟氏映射 题目背景 2233年,CSSYZ学校的数学老师兼数学竞赛顾问钟JG已经2200+岁啦! 为了庆生,他或她给广大人民群众出了道题. 题目描述 设集合N=M={x∣x∈N+, ...
 - Ubuntu14.04 64位机上安装cuda8.0+cudnn5.0操作步骤
			
查看Ubuntu14.04 64位上显卡信息,执行: lspci | grep -i vga lspci -v -s 01:00.0 nvidia-smi 第一条此命令可以显示一些显卡的相关信息:如果 ...
 - NOIp2018 pj 滚粗记
			
NOIp2018 pj 滚粗记 考前 一个午觉睡完就到了考场 考中 \(T1\)水题切了 \(T2\)水题切了 \(T3\)好像是\(dp\),不会,先跳 \(T4\)像树上莫队一样,然后再欧拉序上面 ...
 - python json.dumps中文乱码
			
json.dumps在默认情况下,对于非ascii字符生成的是相对应的字符编码,而非原始字符,例如: >>> import json>>> js = json.lo ...
 - 第一次玩github,第一个开源小项目——xxoo
			
引言 由于最近的工作写代码比较少,这让LZ产生了一丝危机感.于是便想找一个办法可以没事自己写写代码,自然而然就想到了github.接下来便是一阵捣鼓的过程,其实整个过程很快,主要过程就是注册一个账号, ...
 - Gitlab 403 forbidden 并发引起IP被封
			
问题 在工作中自搭建的Gitlab.但今天打开页面的时候显示的是空白页面,上面还有一次文本Forbidden. 原因 Gitlab使用rack_attack做了并发访问的限制. 解决方案 将Gitla ...
 - Dede织梦验证码不显示,织梦后台登陆验证码不显示解决方法
			
关于"织梦验证码不显示"的解决方法 "织梦验证码无法显示出来"的问题分析? 1.之前显示正常,但是换了服务器后就不能够正常显示:(这种通常是网站程序经过迁移后所 ...
 - 如何防范和应对Redis勒索,腾讯云教你出招
			
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯云数据库 TencentDB发表于云+社区专栏 9月10日下午,又一起规模化利用Redis未授权访问漏洞攻击数据库的事件发生,此次 ...
 - 使用NNI的scikit-learn以及Tensorflow分析
			
一.NNI简介 NNI (Neural Network Intelligence) 是自动机器学习(AutoML)的工具包. 它通过多种调优的算法来搜索最好的神经网络结构和(或)超参,并支持单机.本地 ...
 - Excel VBA宏 链接服务器 上传和下载数据
			
首先说明以下. 第一: 下面的 “ _" 也就是 空格下划线 在VBA中表示换行的意思:& 表示链接连个字符串的操作,注意 & 的前后是否需要空格 第二: 如果链接服务器,服 ...