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

2020-09-28 10:53:39 浏览数 (2)

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:

代码语言:javascript复制
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:

代码语言:javascript复制
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分题吧~

代码语言:javascript复制
// 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;
}

0 人点赞