UESTC 483 Data Structure Problem
来源:互联网 发布:国泰君安网络金融部 编辑:程序博客网 时间:2024/06/07 10:10
Description
Data structure is a fundamental course of Computer Science, so that each contestant is highly likely to solve this data structure problem.
A Heap data structure is a binary tree with the following properties:
- It is a complete binary tree; that is, each level of the tree is completely filled, except possibly the bottom level. At this level, it is filled from left to right.
- It satisfies the heap-order property: The key stored in each node is greater than or equal to the keys stored in its children.
So such a heap is sometimes called a max-heap. (Alternatively, if the comparison is reversed, the smallest element is always in the root node, which results in a min-heap.)
A binary search tree (BST), which may sometimes also be called an ordered or sorted binary tree, is a node-based binary tree data structure which has the following properties:
- The left subtree of a node contains only nodes with keys less than (greater than) the node's key.
- The right subtree of a node contains only nodes with keys greater than (less than) the node's key.
- Both the left and right subtrees must also be binary search trees.
Given a complete binary tree with $N$ keys, your task is to determine the type of it.
Note that either a max-heap or a min-heap is acceptable, and it is also acceptable for both increasing ordered BST and decreasing ordered BST.
Input
The first line of the input is $T$ (no more than $100$), which stands for the number of test cases you need to solve.
For each test case, the first line contains an integer $N$ ($1 \leq N \leq 1000$), indicating the number of keys in the binary tree. On the second line, a permutation of $1$ to $N$ is given. The key stored in root node is given by the first integer, and the $2i_{th}$ and $2i+1_{th}$ integers are keys in the left child and right child of the $i_{th}$ integer respectively.
Output
For every test case, you should output Case #k:
first, where $k$ indicates the case number and counts from $1$. Then output the type of the binary tree:
Neither
— It is neither a Heap nor a BST.Both
— It is both a Heap and a BST.Heap
— It is only a Heap.BST
— It is only a BST.
Sample Input
4
1
1
3
1 2 3
3
2 1 3
4
2 1 3 4
Sample Output
Case #1: Both
Case #2: Heap
Case #3: BST
Case #4: Neither
判断一下是二叉搜索树还是堆
#include<cstdio>#include<cstring>#include<cmath>#include<iostream>#include<map>#include<queue>#include<algorithm>using namespace std;const int maxn=1e3+10;int T,t=0,ch[maxn][2],a[maxn],minx[maxn],maxx[maxn],n;bool checkheap(int x,int kind){ if (!x) return true; if (ch[x][0]&&(kind==0&&a[x]>a[ch[x][0]]||kind==1&&a[x]<a[ch[x][0]])) return false; if (ch[x][1]&&(kind==0&&a[x]>a[ch[x][1]]||kind==1&&a[x]<a[ch[x][1]])) return false; return checkheap(ch[x][0],kind)&&checkheap(ch[x][1],kind);}bool checkbst(int x,int kind){ maxx[x]=minx[x]=a[x]; if (ch[x][0]) { if (!checkbst(ch[x][0],kind)) return false; minx[x]=min(minx[x],minx[ch[x][0]]); maxx[x]=max(maxx[x],maxx[ch[x][0]]); if (kind==0&&maxx[ch[x][0]]>=a[x]) return false; if (kind==1&&minx[ch[x][0]]<=a[x]) return false; } if (ch[x][1]) { if (!checkbst(ch[x][1],kind)) return false; minx[x]=min(minx[x],minx[ch[x][1]]); maxx[x]=max(maxx[x],maxx[ch[x][1]]); if (kind==0&&minx[ch[x][1]]<=a[x]) return false; if (kind==1&&maxx[ch[x][1]]>=a[x]) return false; } return true;}int main(){ scanf("%d",&T); while (T--) { scanf("%d",&n); for (int i=1;i<=n;i++) { scanf("%d",&a[i]); if (i&1) ch[i>>1][1]=i; else ch[i>>1][0]=i; ch[i][0]=ch[i][1]=0; } int flag1=checkheap(1,0)||checkheap(1,1); int flag2=checkbst(1,0)||checkbst(1,1); printf("Case #%d: ",++t); if (flag1&&flag2) printf("Both\n"); else if (flag1) printf("Heap\n"); else if (flag2) printf("BST\n"); else printf("Neither\n"); } return 0;}
- UESTC 483 Data Structure Problem
- UESTC 483 Data Structure Problem
- cdoj 483 Data Structure Problem DFS
- uestc data structure K - Sliding Window
- uestc data structure n 秋实大哥搞算数
- uestc data structure n 秋实大哥与快餐店
- uestc data structure D - 秋实大哥与战争
- uva 11991 Easy Problem from Rujia Liu? Data Structure
- uestc data structure training f 秋实大哥与妹纸
- Data Structure
- Data structure
- Data Structure
- Data structure
- Data structure
- data structure
- Data Structure
- Data structure
- Data Structure
- x264代码剖析(十三):核心算法之帧间预测函数x264_mb_analyse_inter_*()
- 2597: [Wc2007]剪刀石头布 费用流
- HDU 1059.Dividing【多重背包+筛选(DP)】【3月17】
- Paper Reading 2:Human-level control through deep reinforcement learning
- ubuntu14.04+opencv 3.0安装及测试
- UESTC 483 Data Structure Problem
- golang入门-- 一个2D的图形库学习
- 史上最详细的Android Studio系列教程四--Gradle基础
- java多线程之中断(interrupt)问题
- 第八章:JavaScript事件驱动编程和访问CSS技术
- Oracle SQL语句执行顺序
- 易语言 线程消息队列
- HDU1042&1753--大数运算--JAVA
- 程序员面试十大算法