UVA1264 && UVAlive4847 Binary Search Tree
Posted 小小AI,请多多提携
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA1264 && UVAlive4847 Binary Search Tree相关的知识,希望对你有一定的参考价值。
A binary search tree is a binary tree. It may be empty. If it is not empty, it satisfies the following properties:
(1) Every node has a key, and no two nodes have the same key.
(2) The keys in a nonempty left subtree must be smaller than the key in the root of the subtree.
(3) The keys in a nonempty right subtree must be larger than the key in the root of the subtree.
(4) The left and right subtrees are also binary search trees. Sample binary search trees are shown in Figure 1.
Figure 1. binary search trees
To search for a node with a key k in a binary search tree T, we begin at the root. If T is empty, T contains no keys and the search is unsuccessful. Otherwise, we compare k with the key in root. If k equals root’s key, then the search terminates successfully. If k is less than root’s key, we search the left subtree of the root. If k is larger than root’s key, we search the right subtree of the root. In the same way, we can proceed the search in the left or right subree of T.
To insert a new key k into a binary search tree T where k is different from those of existing keys in T, we first search the tree T. The search will be unsuccessful, then we insert the key at the point the search terminated. For instance, to insert a key 80 into the Figure 1(a), we first search the tree for 80. This search terminates unsuccessfully, and the last node examined has key 40. We insert a new node containing 80 as the right child of the node. The resulting search tree is shown in Figure 1(b).
In this problem, we consider binary search trees with N keys 1, 2, . . . , N. For a permutation a1 a2 . . . aN of {1, 2, . . . , N}, inserting a1 a2 . . . aN successively into an initially empty binary search tree will produce a binary search tree. For instance, the permutation 2 1 4 3 5 will produce the tree in Figure 1(c). Also, 2 4 3 1 5 will produce the same tree. Actually, 8 permutations among all possible permutations of 1, 2, 3, 4, 5 will produce the same tree to the tree in Figure 1(c).
We are interested in finding the number of permutations of {1, 2, . . . , N} such that all those permutations produce a binary search tree identical to the tree produced by a given permutation P. Given N and P, you are to write a program that calculates the number of permutations satisfying the above condition.
Input
Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line. Each test case starts with a line containing an integer N representing the number of keys, 1 ≤ N ≤ 20. In the next line, a permutation of length N is given. There is a single space between the integers representing keys in the permutation.
Output
Your program is to write to standard output. Print exactly one line for each test case as follows: Let B be the number of permutations that produce the binary search tree identical to the tree produced by the input permutation. Print B mod 9, 999, 991 for each test case. For example, if B = 20, 000, 000, the output should be 18 for that test case. The following shows sample input and output for three test cases.
Sample Input
3
5
2 1 4 3 5
4
2 4 1 3
12
1 2 3 4 5 6 7 8 9 10 11 12
Sample Output
8
3
1
题目大意:给你BST插入序列,求所有形成此BST的序列个数,答案模9999991
答案就是 每个左(或右)子树节点数 在 左右子树节点总数 的组合数的乘积
感性理解,对于一个子树而言,要保证左边和右边顺序是真确的,而左右子树互不影响
那么一个子树下插入顺序就可以是
根 左1 左2 右1 右2 右3 左3 左4
根 左1 左2 左3 右1 右2 右3 左4
...........
这样子的话就有C(左右子树结点总数,左(或)子树结点树) 种方案
那么结果用乘法原理就好了
1 /************************************************************************* 2 > Author: ndqzhang1111 3 ************************************************************************/ 4 #include<iostream> 5 #include<cstdio> 6 #include<cstdlib> 7 #include<cmath> 8 #include<cstring> 9 #include<algorithm> 10 #include<cctype> 11 #include<ctime> 12 #include<vector> 13 using namespace std; 14 typedef long double db; 15 typedef long long ll; 16 #define FOR(i, a, b) for(register int i = (a), i##_end_ = (b); i <= i##_end_; ++ i) 17 #define ROF(i, a, b) for(register int i = (a), i##_end_ = (b); i >= i##_end_; -- i) 18 #define For(i, a, b) for(i = (a);i <= i##_end_; ++ i) 19 #define Rof(i, a, b) for(i = (a);i >= i##_end_; -- i) 20 #define debug(...) fprintf(stderr, __VA_ARGS__) 21 #define faster __attribute__((optimize("-O3"))) 22 #define mem(a, b) memset((a), b, sizeof(a)) 23 namespace OIER{ 24 void IOfile(const char *name1,const char *name2){ 25 FILE *fp;char ch; 26 if((fp=fopen(name1,"r"))==NULL){ 27 printf("Error!\\n");exit(0); 28 } 29 ch=fgetc(fp); 30 if(ch==EOF)printf("File Empty!\\n"),exit(0); 31 else freopen(name1,"r",stdin),freopen(name2,"w",stdout); 32 } 33 void file(const char *name,bool flag){ 34 if(flag)freopen(name,"w",stdout); 35 else{ 36 FILE *fp;char ch; 37 if((fp=fopen(name,"r"))==NULL){ 38 printf("Error!\\n");exit(0); 39 } 40 ch=fgetc(fp); 41 if(ch==EOF)printf("File Empty!\\n"),exit(0); 42 freopen(name,"r",stdin); 43 } 44 } 45 template <typename T> T read(T simple){ 46 char c;bool f=0;T x=0; 47 for(c=getchar();!isdigit(c);c=getchar())if(c==\'-\')f=1; 48 for(;isdigit(c);c=getchar())x=(x<<3)+(x<<1)+(c^48); 49 if(f)x=-x; 50 return x; 51 } 52 template <typename T> bool READ(T &x){ 53 char c;bool f=0; 54 for(c=getchar();!isdigit(c);c=getchar())if(c==\'-\')f=1; 55 for(;isdigit(c);c=getchar())x=(x<<3)+(x<<1)+(c^48); 56 if(f)x=-x; 57 return true; 58 } 59 template <typename T> bool write(T x,bool f=false){ 60 if(x<0)f=1,x=-x; 61 if(x>9)write(x/10,f); 62 if(x<10&&f)putchar(\'-\'); 63 putchar(x%10+48); 64 } 65 template <typename T> bool chkmax(T &a,T b){return a<b?a=b,1:0;} 66 template <typename T> bool chkmin(T &a,T b){return a>b?a=b,1:0;} 67 } 68 using namespace OIER; 69 const int N=50,mod=9999991; 70 struct BST{ 71 int key; 72 int lc,rc; 73 int lsz,rsz,sz; 74 }node[N]; 75 int n,C[N][N]; 76 void init(){ 77 for(int i=0;i<=40;++i)C[i][0]=C[i][i]=1; 78 for(int i=2;i<=40;++i) 79 for(int j=1;j<i;++j) 80 C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod; 81 } 82 void insert(int loc,int id,int x){ 83 if(node[loc].key==0){node[loc].key=x;node[loc].sz=1;return;} 84 if(node[loc].key<x){ 85 if(!node[loc].lc)node[loc].lc=id; 86 insert(node[loc].lc,id,x); 87 } 88 else{ 89 if(!node[loc].rc)node[loc].rc=id; 90 insert(node[loc].rc,id,x); 91 } 92 node[loc].lsz=node[node[loc].lc].sz; 93 node[loc].rsz=node[node[loc].rc].sz; 94 node[loc].sz=node[loc].lsz+node[loc].rsz+1; 95 } 96 int main(){ 97 #ifndef ONLINE_JUDGE 98 IOfile("bst.in","bst.out"); 99 #endif 100 init(); 101 FOR(T,1,read(0)){ 102 n=read(0); 103 memset(node,0,sizeof(node)); 104 for(int i=1;i<=n;++i){ 105 int x=read(0); 106 insert(1,i,x); 107 } 108 ll ans=1; 109 for(int i=1;i<=n;++i) 110 (ans*=C[node[i].sz-1][node[i].lsz])%=mod; 111 cout<<ans<<endl; 112 } 113 return 0; 114 }
以上是关于UVA1264 && UVAlive4847 Binary Search Tree的主要内容,如果未能解决你的问题,请参考以下文章
[BZOJ1264][AHOI2006]基因匹配Match(DP + 树状数组)
UVa 11997 K Smallest Sums 优先队列&&打有序表&&归并