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/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)))

建立一颗树,每个结点有两个关键字,要求第一个关键字满足二叉搜索树的性质,第二个结点满足堆的性质

首先,要把所有结点按照第一个关键字按非递减排序,这样之后,每个结点左边的结点都比该结点的第一个关键字小,右边的则第一个关键字都比他大。这样的话按顺序每次插入右子树,因为要满足二叉搜索树的性质, 插入之后不能满足堆的性质时就左旋。

由于每个节点两种属性v值和优先级r,先读入所有的节点信息,然后将所有节点按第一关键字排序。然后我们递归处理[1,n]区间内的信息,用RMQ先找出[1,n]内的最大r值的节点mid,并输出信息,然后递归处理左边的区间[1,mid-1]和右边的区间[mid+1,n]即可.

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<map>
#include<iomanip>
#include<math.h>
using namespace std;
typedef long long ll;
typedef double ld;
const int maxn=55555;
int dmax[maxn][20];
int mm[maxn];
int n;
struct node
{
char v[150];
int r;
bool operator<(const node&b)const
{
return strcmp(v,b.v)<0;
}
} nodes[maxn];
int maxv(int i,int j)
{
if(nodes[i].r>nodes[j].r)
return i;
return j;
}
void initMax()
{
mm[0]=-1;
for(int i=1; i<=n; i++)
{
dmax[i][0]=i;
mm[i] = (i&(i-1))==0 ?mm[i-1]+1:mm[i-1];
}
for(int j=1; j<=mm[n]; j++)
for(int i=1; i+(1<<j)-1<=n; i++)
dmax[i][j]=maxv(dmax[i][j-1],dmax[i+(1<<(j-1))][j-1]);
}
int getMax(int L,int R)
{
int k=mm[R-L+1];
return maxv(dmax[L][k],dmax[R-(1<<k)+1][k]);
}
void solve(int l,int r)
{
if(l<=r)
{
int mid=getMax(l,r);
printf("(");
solve(l,mid-1);
printf("%s/%d",nodes[mid].v,nodes[mid].r);
solve(mid+1,r);
printf(")");
}
}
int main()
{
while(scanf("%d",&n)==1&&n)
{
for(int i=1; i<=n; i++)
{
scanf("%s",nodes[i].v);
int j;
for(j=0; nodes[i].v[j]!='/'; j++);
nodes[i].r=nodes[i].v[j]=0;
for(j++; nodes[i].v[j]!=0; j++)
nodes[i].r = nodes[i].r*10+nodes[i].v[j]-'0';
}
sort(nodes+1,nodes+n+1);
initMax();
solve(1,n);
printf("\n");
}
return 0;
}