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 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.在每次迭代中,插入排序从输入数据中删除一个元素 ...
随机推荐
- # 20155229 2016-2017-2 《Java程序设计》第七周学习总结
20155229 2016-2017-2 <Java程序设计>第七周学习总结 教材学习内容总结 第十二章 lambda的主体是一个表达式,而不是一个代码块.仅仅能在lambda表达式中封装 ...
- Windows:任务调度器
Windows 服务器系列: Windows:查看IP地址,IP地址对应的机器名,占用的端口,以及占用该端口的应用程 Windows:使用Dos命令管理服务(Services) Windows:任务调 ...
- 【轮子狂魔】抛弃IIS,向天借个HttpListener - 基础篇(附带源码)
这一次我们要玩什么? 先声明一下,由于这篇是基础篇主要是通过这篇文章让大家对使用HttpListener响应Http请求有个大概了解,所以正式的花样轮子在下一篇推出,敬请期待 ^_^ 嗯哼,还有,我标 ...
- linux 修改文件最大数
ulimit -a 查看所有 open files (-n) 1024 是linux操作系统对一个进程打开的文件句柄数量的限制(也包含打开的套接字数量) ulimit -SHn 10000 ##临时修 ...
- stl源码分析之hash table
本文主要分析g++ stl中哈希表的实现方法.stl中,除了以红黑树为底层存储结构的map和set,还有用哈希表实现的hash_map和hash_set.map和set的查询时间是对数级的,而hash ...
- APP性能测试--功耗测试
一.功耗测试基础 移动设备的电池电量是非常有限的,保持持久的续航能力尤为重要.另外,android的很多特性都比较耗电(如屏幕,GPS,sensor传感器,唤醒机制,CPU,连网等的使用),我们必须要 ...
- Linux——CentOS7添加/删除用户和用户组(学习笔记)
1.新建用户 adduser testuser //新建testuser 用户 passwd testuser //给testuser 用户设置密码 2.建工作组 groupadd testgroup ...
- aircrack-ng无线破解实验
查看无线网卡 airmon-ng 开启网卡监听模式 airmon-ng start wlan0 扫描附近的wifi airodump-ng wlan0mon 停止扫描: ctrl c 使用airodu ...
- Centos7 zabbix 自动发现与注册
自动发现与自动注册 自动发现: zabbix Server主动发现所有客户端,然后将客户端登记自己的小本上,缺点zabbix server压力山大(网段大,客户端多),时间消耗多. 自动注册: zab ...
- Python基础_可迭代的/迭代器/生成器
介绍 可迭代的:内部实现了__iter__方法 迭代器:内部实现了__iter__,__next__方法 生成器:yield,yield from 使用 __iter__() __next__() _ ...