PAT_A1147#Heaps
Source:
Description:
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))
Your job is to tell if a given complete binary tree is a heap.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers: M (≤100), the number of trees to be tested; and N (1 < N ≤ 1,000), the number of keys in each tree, respectively. Then M lines follow, each 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, print in a line
Max Heap
if it is a max heap, orMin Heap
for a min heap, orNot Heap
if it is not a heap at all. Then in the next line print the tree's postorder traversal sequence. All the numbers are separated by a space, and there must no extra space at the beginning or the end of the line.
Sample Input:
3 8
98 72 86 60 65 12 23 50
8 38 25 58 52 82 70 60
10 28 15 12 34 9 8 56
Sample Output:
Max Heap
50 60 65 72 12 23 86 98
Min Heap
60 58 52 38 82 70 25 8
Not Heap
56 12 34 28 9 8 15 10
Keys:
Attention:
- 有点水了哈0,0;
Code:
/*
Data: 2019-06-29 16:15:43
Problem: PAT_A1147#Heaps
AC: 19:20 题目大意:
判断给定的完全二叉树是否是堆
输入:
第一行给出测试数M(<=100)和结点数N[1,1e3]
接下来N行,逐层给出完全二叉树的各个键值
输出:
大根堆,小根堆,非堆;
接下来输出二叉树的后序遍历 基本思路:
静态树存储BST,遍历判断是否为堆
*/
#include<cstdio>
#include<vector>
using namespace std;
const int M=1e3+;
int bst[M],Min,Max,n,m;
vector<int> path; void Travel(int root)
{
if(root > n)
return;
if(root!=)
{
if(bst[root/] > bst[root])
Min=;
if(bst[root/] < bst[root])
Max=;
}
Travel(root*);
Travel(root*+);
path.push_back(bst[root]);
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE scanf("%d%d", &m,&n);
while(m--)
{
for(int i=; i<=n; i++)
scanf("%d", &bst[i]);
Min=;
Max=;
path.clear();
Travel();
if(Max) printf("Max Heap\n");
else if(Min) printf("Min Heap\n");
else printf("Not Heap\n");
for(int i=; i<n; i++)
printf("%d%c", path[i],i==n-?'\n':' ');
} return ;
}
PAT_A1147#Heaps的更多相关文章
- CodeForces 353B Two Heaps
B. Two Heaps Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily cho ...
- DSP\BIOS调试Heaps are enabled,but not set correctly
转自:http://blog.sina.com.cn/s/blog_735f291001015t9i.html Heaps are enabled, but the segment for DSP/B ...
- CSU 1616: Heaps(区间DP)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1616 1616: Heaps Time Limit: 2 Sec Memory Lim ...
- Codeforces Round #300 F - A Heap of Heaps (树状数组 OR 差分)
F. A Heap of Heaps time limit per test 3 seconds memory limit per test 512 megabytes input standard ...
- Heaps(Contest2080 - 湖南多校对抗赛(2015.05.10)(国防科大学校赛决赛-Semilive)+scu1616)
Problem H: Heaps Time Limit: 2 Sec Memory Limit: 128 MBSubmit: 48 Solved: 9[Submit][Status][Web Bo ...
- Fibonacci Heaps
Mergeable heapsA mergeable heap is any data structure that supports the following five operations,in ...
- PAT 1147 Heaps[难]
1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...
- [PAT] 1147 Heaps(30 分)
1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...
- Codeforces 538 F. A Heap of Heaps
\(>Codeforces \space 538 F. A Heap of Heaps<\) 题目大意 :给出 \(n\) 个点,编号为 \(1 - n\) ,每个点有点权,将这些点构建成 ...
随机推荐
- ZooKeeper的原理(转)
一.ZooKeeper的角色 领导者(Leader),负责进行投票的发起和决议,更新系统状态. 学习者(Learner),包括跟随者(Follower)和观察者(Observer),Follower用 ...
- N天学习一个Linux命令之ln
前言有时候同一个文件想创建多个别名,这个时候可以使用链接文件代替 用途对文件或者目录创建链接,默认创建的是硬链接 硬链接Linux底层文件系统由超级数据块,目录树对象,inode索引节点对象,文件对象 ...
- Java修改文件夹名称
Java修改文件夹名称 学习了:http://blog.csdn.net/yongh701/article/details/45063833 进行文件夹名字批量修改,注意,要写全路径: package ...
- [Tools] Using colours in a NodeJS terminal - make your output pop
Use can use colour and styles to make it easy to spot errors and group common functionality into blo ...
- Android之——AsyncTask和Handler对照
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46952835 AsyncTask和Handler对照 1 ) AsyncTask实 ...
- 使用Hive的正则解析器RegexSerDe分析nginx日志
1.环境: hadoop-2.6.0 + apache-hive-1.2.0-bin 2.使用Hive分析nginx日志,站点的訪问日志部分内容为: cat /home/hadoop/hivetest ...
- 【Hibernate学习】 ——ORM(四)再次认识实体继承
在信用办时.做失信.守信.黑名单这一块的时候.先把原来的需求看了看.紧接着就開始设计实体,这一块大部分都是同样的信息,所以就设计了一个实体,而且用一个状态标识出来是失信.守信还是黑名单. 在之后的改动 ...
- linux 搭建https server (apache)
一. 安装准备 1. 安装Openssl 要使Apache支持SSL,须要首先安装Openssl支持.这里使用的是openssl-0.9.8k.tar.gz 下载Openssl:htt ...
- oracle加入联合主键
alter table tablename add constraint unionkeyname primary key (column1,column2); 上面语句中: tablename为要加 ...
- golang import all 类似python import * 效果
import "io/ioutil" func main() { content, err = iotuil.ReadFile("somefile.txt") ...