zoj 2243 Binary Search Heap Construction(笛卡爾樹)

Binary Search Heap Construction

Time Limit: 5 Seconds      Memory Limit: 32768 KB

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

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

 

 

題意:給出n個結點,有兩個值key和value,要求構造一棵笛卡爾樹。光看key的話,笛卡爾樹是一棵二叉搜索樹,每個節點的左子樹的key都比它小,右子樹都比它大;光看value的話,笛卡爾樹有點類似堆,根節點的value是最小(或者最大)的,每個節點的value都比它的子樹要小(或者大)。

思路:先按key值排序,然後一個個插入構造笛卡爾樹,這裏用了O(n)的算法求出了每個結點的父親結點,然後對於每個結點,若該節點key值比父親結點小,則爲左子樹,否則爲右子樹。

 

AC代碼:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 50005;

int n;
struct node{
    char s[105];
    int v;
}p[maxn];
int l[maxn], r[maxn], T[maxn], st[maxn];
bool cmp(node a, node b){
    return strcmp(a.s, b.s) < 0;
}
void solve(){
    int k, top = -1;
    memset(l, -1, sizeof(l));
    memset(r, -1, sizeof(r));
    for(int i = 0; i < n; i++)
    {
        k = top;
        while(k >= 0 && p[st[k]].v < p[i].v) k--;
        if(k != -1) T[i] = st[k];
        if(k < top) T[st[k + 1]] = i;
        st[++k] = i;
        top = k;
    }
    T[st[0]] = -1;
    for(int i = 0; i < n; i++)
    {
        if(T[i] == -1) continue;
        if(strcmp(p[i].s, p[T[i]].s) < 0) l[T[i]] = i;
        else r[T[i]] = i;
    }
}
void dfs(int u){
    if(u == -1) return;
    printf("(");
    dfs(l[u]);
    printf("%s/%d", p[u].s, p[u].v);
    dfs(r[u]);
    printf(")");
}
int main()
{
    while(scanf("%d", &n), n)
    {
        getchar();
        for(int i = 0; i < n; i++)
        {
            scanf("%[^/]s", p[i].s);
            scanf("/%d", &p[i].v);
            getchar();
        }
        sort(p, p + n, cmp);
        solve();
        dfs(st[0]);
        puts("");
    }
    return 0;
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章