zoukankan      html  css  js  c++  java
  • 笛卡尔树 POJ ——1785 Binary Search Heap Construction

    相应POJ 题目:点击打开链接

    Binary Search Heap Construction
    Time Limit: 2000MS   Memory Limit: 30000K
    Total Submissions: 9075   Accepted: 2566

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

    题意:

    每次有n个输入,每一个输入格式为(字符串/数字)。字符串(长度未知,反正我开100也能过)和数字都不会反复。要求建立一棵树。使得中序遍历按字符串字典序排序,并且数字符合大根堆。

    输出格式为((左子树)根节点(右子树))。

     

    思路:

    赤裸裸的Treap树,可惜会TLE。可用笛卡尔树顺利AC。建树时在右链从下往上找适合位置插入。读入的时候有点技巧,%*[ ]表示忽略[]里面的字符,%[^/]表示读入字符串时遇到'/'就结束,没有读入'/'且会在字符串后面加入结束符。

     

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <time.h>
    using namespace std;
    #define N 50010
    #define inf 0x7fffffff
    #define nil 0
    
    struct Node
    {
    	int pri, l, r, fa;
    	char str[100];
    };
    
    bool cmp(Node n1, Node n2)
    {
    	return strcmp(n1.str, n2.str) < 0;
    }
    
    class CartesianTree
    {
    public:
    	void Init(int n)
    	{
    		a[0].pri = inf;
    		a[0].l = a[0].r = a[0].fa = nil;
    		int i;
    		for(i = 1; i <= n; i++){
    			scanf("%*[ ]%[^/]/%d", a[i].str, &a[i].pri);
    			a[i].l = a[i].r = a[i].fa = nil;
    		}
    		sort(a + 1, a + n + 1, cmp);
    		for(i = 1; i <= n; i++)
    			Insert(i);
    	}
    	void Insert(int p)
    	{
    		int t = p - 1; //从下往上找
    		while(a[t].pri < a[p].pri) t = a[t].fa;
    		a[p].l = a[t].r;
    		a[t].r = p;
    		a[p].fa = t;
    	}
    	void Show()
    	{
    		InOrder(a[0].r);
    		printf("
    ");
    	}
    	void InOrder(int t)
    	{
    		if(nil == t) return;
    		printf("(");
    		InOrder(a[t].l);
    		printf("%s/%d", a[t].str, a[t].pri);
    		InOrder(a[t].r);
    		printf(")");
    	}
    private:
    	Node a[N];
    };
    
    CartesianTree ct;
    
    int main()
    {
    	//freopen("in.txt","r",stdin);
    	int n;
    	while(scanf("%d", &n), n)
    	{
    		ct.Init(n);
    		ct.Show();
    	}
    	return 0;
    }
    
    


     

     

     

     

     

     

  • 相关阅读:
    CentOS 7.3 CDH 5.10.0 Druid0.12.4安装记录
    cloudera manager卸载流程
    CDH5.10.0 离线安装(共3节点) 转
    CentOS 7 安装Httpd(转)
    CentOS下MySQL的彻底卸载
    CentOS7 修改主机名
    sendEvent()
    QSignalMapper Class
    ubuntu12.04开启虚拟机的unity模式
    BCM_I2C函数更改
  • 原文地址:https://www.cnblogs.com/mthoutai/p/6785712.html
Copyright © 2011-2022 走看看