ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

PAT (Advanced Level) Practice 1147 Heaps (30 分) 凌宸1642

2021-08-20 02:31:40  阅读:218  来源: 互联网

标签:Heaps layer PAT Heap int 30 tree heap each


PAT (Advanced Level) Practice 1147 Heaps (30 分) 凌宸1642

题目描述:

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.

译:在计算机科学中,堆是一种特殊的基于树的数据结构,它满足堆属性:如果 P 是 C 的父节点,则 P 的键(值)要么大于或等于(在最大堆中) ) 或小于或等于(在最小堆中)C 的键。堆的常见实现是二叉堆,其中树是完全二叉树。 (引自维基百科 https://en.wikipedia.org/wiki/Heap_(data_structure))

你的工作是判断一个给定的完整二叉树是否是一个堆。


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.

译:每个输入文件包含一个测试用例。 对于每种情况,第一行给出两个正整数:M(≤ 100),要测试的树的数量; 和 N (1 < N ≤ 1,000),分别是每棵树中的键数。 然后是 M 行,每行包含 N 个不同的整数键(都在 int 范围内),这给出了完整二叉树的层序遍历序列。


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.

译:对于每个给定的树,如果它是最大堆,则在一行中打印 Max Heap,如果是最小堆,则打印为Min Heap,如果根本不是堆,则打印为 Not Heap。 然后在下一行打印树的后序遍历序列。 所有数字之间用空格隔开,行首或行尾不得有多余空格。


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

The Idea:

  • 直接建树的话,最后两个测试点会超时。

  • 利用堆的性质,可以不见树即可判断是否是堆,然后由于需后序遍历,最后一个结点一定是根节点,所以可以直接对数组使用 dfs 。

The Codes:

#include<bits/stdc++.h>
using namespace std ;
int layer[1010] , m , n ;
void dfs(int root){
	if(root > n ) return ;
	dfs(root * 2) ;
	dfs(root * 2 + 1) ;
	cout << layer[root] << ((root == 1)?'\n':' ') ;
 }
int main(){
	cin >> m >> n ;
	for(int i = 0 ; i < m ; i ++){
		for(int j = 1 ; j <= n ; j ++) cin >> layer[j] ;
		bool minHeap = true , maxHeap = true ;
		for(int j = 1 ; j <= n ; j ++){
			if(j > 1 && layer[j/2] > layer[j]) minHeap = false ;
			if(j > 1 && layer[j/2] < layer[j]) maxHeap = false ;
		}
		if(maxHeap) cout << "Max Heap" << endl ;
		else if(minHeap) cout << "Min Heap" << endl ;
		else cout << "Not Heap" << endl ;
		dfs(1) ;
	}
	return 0 ;
}

标签:Heaps,layer,PAT,Heap,int,30,tree,heap,each
来源: https://www.cnblogs.com/lingchen1642/p/15164749.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有