HEAPULM  Binary Search Heap Construction
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 labelprioritypairs, with unique labels and unique priorities, to construct a treap containing this data.
Input Specification
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 l_{1}/p_{1},...,l_{n}/p_{n}
denoting the label and priority of each node.
The strings are nonempty and composed of lowercase letters, and the numbers are nonnegative integers.
The last test case is followed by a zero.
Output Specification
For each test case output on a single line a treap that contains the specified nodes.
A treap is printed as (<left subtreap><label>/<priority><right subtreap>)
.
The subtreaps 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)))
hide comments
harry_shit:
20191017 07:41:37
anyone solved this using segment trees??


chandan kumar:
20171128 06:33:57
@dungnd42 AC means ACCEPTED 

dungnd42:
20171127 18:19:39
what mean AC


roadblock:
20170425 13:30:52
has anyone solved using java


doluongk56:
20160728 11:07:01
AC after long time and many many go!


dovantiep:
20160723 15:24:36
AC in many go :))


Hussain Kara Fallah:
20140203 23:00:53
assume that no string is longer than 50 characters 
Added by:  Wanderley GuimarÄƒes 
Date:  20070919 
Time limit:  1s 
Source limit:  50000B 
Memory limit:  1536MB 
Cluster:  Cube (Intel G860) 
Languages:  All except: ERL JSRHINO 
Resource:  University of Ulm Local Contest 2004 