POJ 1785 Binary Search Heap Construction 笛卡尔树

Binary Search Heap Construction
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 9091 Accepted: 2570

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

Source

ACcode:
#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define ll long long int
#define maxn 100005
#define mod 1000000007
#define INF 0x3f3f3f3f //int×î´óÖµ
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI  acos(-1.0)
#define E  exp(1)
using namespace std;
struct Tree{
    char str[100];
    int value;
    int left,right,pre;
    void cher(){
        left=right=pre=0;
    }
    bool operator<(Tree t)const{
        return strcmp(str,t.str)<0;
    }
}treap[maxn];
void creat(int n){
    FOR(i,1,n)
        treap[i].cher();
    treap[0].value=INF;
}
void join(int n){
    FOR(i,1,n){
        int j=i-1;
        while(treap[j].value<treap[i].value)
            j=treap[j].pre;
        treap[i].left=treap[j].right;
        treap[j].right=i;
        treap[i].pre=j;
    }
}
void dfs(int i){
    if(i){
        putchar('(');
        dfs(treap[i].left);
        printf("%s/%d",treap[i].str,treap[i].value);
        dfs(treap[i].right);
        putchar(')');
    }
}
int main(){
    int  n;
    while(rd(n)&&n){
        MT(treap,0);
        FOR(i,1,n)
            scanf("%*[ ]%[^/]/%d",treap[i].str,&treap[i].value);
        sort(treap+1,treap+1+n);
        creat(n);
        join(n);
        dfs(treap[0].right);
        putchar('\n');
    }
    return 0;
}
/**
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
**/


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值