Source:

PAT A1147 Heaps (30 分)

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, or Min Heap for a min heap, or Not 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的更多相关文章

  1. CodeForces 353B Two Heaps

    B. Two Heaps   Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily cho ...

  2. 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 ...

  3. CSU 1616: Heaps(区间DP)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1616 1616: Heaps Time Limit: 2 Sec  Memory Lim ...

  4. 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 ...

  5. 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 ...

  6. Fibonacci Heaps

    Mergeable heapsA mergeable heap is any data structure that supports the following five operations,in ...

  7. PAT 1147 Heaps[难]

    1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...

  8. [PAT] 1147 Heaps(30 分)

    1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...

  9. Codeforces 538 F. A Heap of Heaps

    \(>Codeforces \space 538 F. A Heap of Heaps<\) 题目大意 :给出 \(n\) 个点,编号为 \(1 - n\) ,每个点有点权,将这些点构建成 ...

随机推荐

  1. B - Oulipo

    The French author Georges Perec (1936�C1982) once wrote a book, La disparition, without the letter ' ...

  2. Loadrunner | 录制脚本时弹不出IE的解决办法

    Loadrunner在录制脚本的时候有时候会遇到弹不出IE的问题,那怎么解决呢?别急,按照以下几个步骤操作,一般就可以解决这个问题. 1. IE浏览器取消勾选[启用第三方浏览器扩展] 启动IE,从[工 ...

  3. [Node.js] Add Logging to a Node.js Application using Winston

    Winston is a popular logging library for NodeJS which allows you to customise the output, as well as ...

  4. SegmentFault 巨献 1024 程序猿游戏「红岸的呼唤」第一天任务攻略

    今天一不小心在微博上看到了SegmentFault的一条微博: 眼看今天就要过去了,那在这里说一下我的解题过程(事实上大家都知道了吧-=). 高速传送门:http://segmentfault.com ...

  5. ctags的基本操作总结

    ctags用法 说明: a.       ctags能够分析程序生成tags文件: b.         生成的tags文件,能够用 vi -t 查找结构体,数据类型,函数名所在位置.非常方便: ct ...

  6. Ural 1353 Milliard Vasya&#39;s Function(DP)

    题目地址:Ural 1353 定义dp[i][j].表示当前位数为i位时,各位数和为j的个数. 对于第i位数来说.总能够看成在前i-1位后面加上一个0~9.所以状态转移方程就非常easy出来了: dp ...

  7. Cocos2d-x飞机大战教程笔记

    咳咳~跟着大神的教程学做Cocos2d-x的飞机大战...鉴于我是那种跟着教程都会出非常多错的人,所以还是一路跟着做些笔记比較好.并且因为是用课余时间,所以仅仅能断断续续地做,写下来也好让自己别忘记~ ...

  8. Linux vs Window

    目前国内Linux更多的是应用于服务器上,而桌面操作系统更多使用的是Window.主要区别如下: 比较 Windows Linux 界面 界面统一,外壳程序固定所有Windows程序菜单几乎一致,快捷 ...

  9. poj--1753--Flip Game(dfs好题)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37201   Accepted: 16201 Descr ...

  10. PCB MS SQL 排序应用---相邻数据且相同合并处理

    这是一个很有趣SQL数据处理应用,具体需求如下 ERP需要工程将物料编码相邻的编码合并求和BOM用量,巧妙的用到了已有排序号与分组排序号之间的差值求解 示例: 原数据: 要求转换: 实际转换后数据: ...