POJ1785 Binary Search Heap Construction
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 10309 | Accepted: 2838 |
Description
Read the statement of problem G for the definitions concerning trees. In the following we define the basic terminology of heaps. A heap is a tree whose internal nodes have each assigned a priority (a number) such that the priority of each internal node is less than the priority of its parent. As a consequence, the root has the greatest priority in the tree, which is one of the reasons why heaps can be used for the implementation of priority queues and for sorting.
A binary tree in which each internal node has both a label and a priority, and which is both a binary search tree with respect to the labels and a heap with respect to the priorities, is called a treap. Your task is, given a set of label-priority-pairs, with unique labels and unique priorities, to construct a treap containing this data.
A binary tree in which each internal node has both a label and a priority, and which is both a binary search tree with respect to the labels and a heap with respect to the priorities, is called a treap. Your task is, given a set of label-priority-pairs, with unique labels and unique priorities, to construct a treap containing this data.
Input
The input contains several test cases. Every test case starts with an integer n. You may assume that 1<=n<=50000. Then follow n pairs of strings and numbers l1/p1,...,ln/pn denoting the label and priority of each node. The strings are non-empty and composed of lower-case letters, and the numbers are non-negative integers. The last test case is followed by a zero.
Output
For each test case output on a single line a treap that contains the specified nodes. A treap is printed as (< left sub-treap >< label >/< priority >< right sub-treap >). The sub-treaps are printed recursively, and omitted if leafs.
Sample Input
7 a/7 b/6 c/5 d/4 e/3 f/2 g/1 7 a/1 b/2 c/3 d/4 e/5 f/6 g/7 7 a/3 b/6 c/4 d/7 e/2 f/5 g/1 0
Sample Output
(a/7(b/6(c/5(d/4(e/3(f/2(g/1))))))) (((((((a/1)b/2)c/3)d/4)e/5)f/6)g/7) (((a/3)b/6(c/4))d/7((e/2)f/5(g/1)))
Source
树 笛卡尔树模板题
笛卡尔树满足两个性质:中序遍历就是原数组(二叉搜索树性质),以及数值满足堆的性质(上小下大或上大下小)。
基本上就是没加随机旋转的treap
这题把读入数据建成笛卡尔树即可,要求字符串满足二叉树性质,数值满足大根堆性质
strcmp跑得真够慢
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #include<vector> 8 using namespace std; 9 const int mxn=100010; 10 struct node{ 11 char s[30]; 12 int val; 13 int fa,l,r; 14 bool operator < (node b)const{ 15 return strcmp(s,b.s)<0; 16 } 17 }t[mxn]; 18 void init(int n){ 19 for(int i=0;i<=n;i++){ 20 t[i].l=t[i].r=t[i].fa=0; 21 } 22 t[0].val=0x3f3f3f3f; 23 } 24 int n; 25 //int st[mxn],top=0; 26 void Build(){ 27 for(int i=1;i<=n;i++){ 28 int j=i-1; 29 while(t[j].val<t[i].val) 30 j=t[j].fa; 31 t[i].l=t[j].r;t[j].r=i; 32 t[i].fa=j; 33 } 34 return; 35 } 36 void Print(int u){ 37 if(!u)return; 38 printf("("); 39 Print(t[u].l); 40 printf("%s/%d",t[u].s+1,t[u].val); 41 Print(t[u].r); 42 printf(")"); 43 return; 44 } 45 int main(){ 46 int i,j; 47 while(scanf("%d",&n) && n){ 48 init(n); 49 for(i=1;i<=n;i++) 50 scanf("%[^/]/%d",t[i].s,&t[i].val); 51 sort(t+1,t+n+1); 52 Build(); 53 Print(t[0].r); 54 printf("\n"); 55 } 56 return 0; 57 }
本文为博主原创文章,转载请注明出处。