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.
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/17 a/1 b/2 c/3 d/4 e/5 f/6 g/77 a/3 b/6 c/4 d/7 e/2 f/5 g/10
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 #include3 #include 4 #include 5 #include 6 #include 7 #include 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