PAT (Advanced Level) Practice 1147 Heaps (30 分)
1147 Heaps (30 分)
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
题目大意:验证性题目,给定一组数据,判断是否为最大堆、最小堆、不是堆,然后再将其后序遍历输出
思路方法:首先要熟悉堆的性质,其父节点肯定比子节点大或小(但不保证左儿子和右儿子哪个大哪个小),然后是完全二叉树,而且父节点和子节点有对应的关系,所以可以很容易地输出后序遍历(递归)。用数组存储,某结点左儿子编号=该结点编号*2,某结点右儿子编号=该结点右儿子编号+1,算是比较水的30分题吧~
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#include<unordered_set>
#define rg register ll
#define inf 2147483647
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define ll long long
#define maxn 100005
#define lb(x) (x&(-x))
const double eps = 1e-6;
using namespace std;
inline ll read()
{
char ch = getchar(); ll s = 0, w = 1;
while (ch < 48 || ch>57) { if (ch == '-')w = -1; ch = getchar(); }
while (ch >= 48 && ch <= 57) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
return s * w;
}
inline void write(ll x)
{
if (x < 0)putchar('-'), x = -x;
if (x > 9)write(x / 10);
putchar(x % 10 + 48);
}
int n,m;
vector<ll>v;
ll a[1005];
inline void check1()
{
ll sum=0;
for(rg i=2;i<=m;i++)
{
if(a[i]<=a[i/2])sum++;
}
sum==m-1?cout<<"Max Heap"<<endl:cout<<"Not Heap"<<endl;
}
inline void check2()
{
ll sum=0;
for(rg i=2;i<=m;i++)
{
//cout<<a[i]<<" "<<a[i/2]<<endl;
if(a[i]>=a[i/2])sum++;
}
sum==m-1?cout<<"Min Heap"<<endl:cout<<"Not Heap"<<endl;
}
inline void post(ll x)
{
if(x>m)return ;
post(x*2);
post(x*2+1);
//cout<<v.size()<<" "<<a[x]<<endl;
v.push_back(a[x]);
}
int main()
{
cin>>n>>m;
for(rg i=1;i<=n;i++)
{
fill(a,a+1005,0),v.clear();
for(rg j=1;j<=m;j++)cin>>a[j];
//cout<<*min_element(a+1,a+1+m)<<endl;
if(a[1]==*max_element(a+1,a+m+1))check1();
else if(a[1]==*min_element(a+1,a+1+m))check2();
else cout<<"Not Heap"<<endl;
post(1);
for(rg i=0;i<v.size();i++)
{
i==v.size()-1?cout<<v[i]<<endl:cout<<v[i]<<" ";
}
}
//while(1)getchar();
return 0;
}
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- laravel实现一个上传图片的接口,并建立软链接,访问图片的方法
- Laravel中validation验证 返回中文提示 全局设置的方法
- laravel5表单唯一验证的实例代码
- 实现laravel 插入操作日志到数据库的方法
- laravel validate 设置为中文的例子(验证提示为中文)
- Laravel 使用查询构造器配合原生sql语句查询的例子
- php面试实现反射注入的详细方法
- laravel框架 api自定义全局异常处理方法
- laravel实现于语言包的完美切换方法
- PHP校验15位和18位身份证号的类封装
- 用Echarts打造一个轮播图!
- Laravel5.5 实现后台管理登录的方法(自定义用户表登录)
- PHP 获取客户端 IP 地址的办法实例代码
- laravel http 自定义公共验证和响应的方法
- Windows服务器中PHP如何安装redis扩展