前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PAT (Advanced Level) Practice 1147 Heaps (30 分)

PAT (Advanced Level) Practice 1147 Heaps (30 分)

作者头像
glm233
发布2020-09-28 10:53:39
2540
发布2020-09-28 10:53:39
举报

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;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-11-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Input Specification:
  • Output Specification:
  • Sample Input:
  • Sample Output:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档