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\) ,每个点有点权,将这些点构建成 ...
随机推荐
- mongodb之监控
监控方式 命令行工具 mongostat命令 用途 用于mongod或者mongos操作类型统计,网络流量统计,当前并发数等统计 用法 mongostat options <sleeptime& ...
- 实现MVC.NET 5的国际化
实现国际化有三种做法: 创建资源文件. 每种语言设置一套单独的View. 1 + 2. 通常而言,第一种方法的可维护性是最高的.因为随着项目的规模的扩大,为每种语言设置一套单独的View,前期的工作量 ...
- canvas学习相关的一点东西
<!DOCTYPE html> <html> <head> <style> canvas { border: 1px dashed black; } & ...
- [Cypress] Test Variations of a Feature in Cypress with a data-driven Test
Many applications have features that can be used with slight variations. Instead of maintaining mult ...
- 【JavaSE】day03_Date、SimpleDateFormat、Calendar、Collection
[JavaSE]day03_Date.SimpleDateFormat.Calendar.Collection 1.Date及其经常使用API 1)JAVA 中的时间 Java中的时间使用标准类库的D ...
- Extjs学习笔记——Ext.data.JsonStore使用说明
Ext.data.JsonStore继承于Ext.data.Store.使得从远程JSON数据创建stores更为方便的简单辅助类. JsonStore合成了Ext.data.HttpProxy与Ex ...
- UVA 10069 Distinct Subsequences(DP)
考虑两个字符串,我们用dp[i][j]表示字串第到i个和字符串到第j个的总数,由于字串必须连续 因此dp[i][j]能够有dp[i][j-1]和dp[i-1][j-1]递推而来,而不能由dp[i-1] ...
- POJ 1128 Frame Stacking(拓扑排序·打印字典序)
题意 给你一些矩形框堆叠后的鸟瞰图 推断这些矩形框的堆叠顺序 每一个矩形框满足每边都至少有一个点可见 输入保证至少有一个解 按字典序输出全部可行解 和上一题有点像 仅仅是这个要打印全部的可行 ...
- 最小堆min_stack
class MinStack {public: void push(int x) { ele.push(x); if(min.empty()||x<=min.top()) // in or ...
- 【BASH】bash shell的使用实例
************************************************************************ ****原文:blog.csdn.net/clark_ ...