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

思路:先按字符串排下序,建笛卡尔树,递归输出。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; struct S{
int id,val;
int parent,l,r;
}node[50005]; int stk[50005],top;
char s[50005][20]; bool cmp(struct S a,struct S b)
{
if(strcmp(s[a.id],s[b.id])<0) return 1; return 0;
} int build(int n)
{
int i; top=0; stk[top]=0; for(i=1;i<n;i++)
{
while(top>=0 && node[stk[top]].val<node[i].val) top--;//注意。这里让小于当前值的出栈 if(top>-1)
{
node[i].parent=stk[top];
node[node[stk[top]].r].parent=i;
node[i].l=node[stk[top]].r;
node[stk[top]].r=i;
}
else
{
node[stk[0]].parent=i;
node[i].l=stk[0];
} stk[++top]=i;
} return stk[0];//返回根节点
} void dfs(int x)
{
printf("(");//先输出左括号 if(node[x].l!=-1) dfs(node[x].l);//假设有左子树。就先输出左子树 printf("%s/%d",s[node[x].id],node[x].val);//输出自己 if(node[x].r!=-1) dfs(node[x].r);//再输出右子树 printf(")");//右括号
} int main()
{
int n,i,root; while(~scanf("%d",&n) && n)
{
for(i=0;i<n;i++)
{
scanf("%*[ ]%[a-z]/%d",s[i],&node[i].val); node[i].id=i; node[i].parent=node[i].l=node[i].r=-1;
} sort(node,node+n,cmp);//按字符串字典序排序 root=build(n);//建树 dfs(root);//递归输出 printf("\n");
}
}

POJ-1785-Binary Search Heap Construction(笛卡尔树)的更多相关文章

  1. 笛卡尔树 POJ ——1785 Binary Search Heap Construction

    相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS   Memory Limit: 30000K Total Subm ...

  2. POJ 1785 Binary Search Heap Construction(裸笛卡尔树的构造)

    笛卡尔树: 每个节点有2个关键字key.value.从key的角度看,这是一颗二叉搜索树,每个节点的左子树的key都比它小,右子树都比它大:从value的角度看,这是一个堆. 题意:以字符串为关键字k ...

  3. POJ 1785 Binary Search Heap Construction (线段树)

    题目大意: 给出的东西要求建立一个堆,使得后面的数字满足堆的性质.并且字符串满足搜索序 思路分析: 用线段树的最大询问建树.在建树之前先排序,然后用中序遍历递归输出. 注意输入的时候的技巧. .. # ...

  4. ZOJ - 2243 - Binary Search Heap Construction

    先上题目: Binary Search Heap Construction Time Limit: 5 Seconds      Memory Limit: 32768 KB Read the sta ...

  5. [POJ1785]Binary Search Heap Construction(笛卡尔树)

    Code #include <cstdio> #include <algorithm> #include <cstring> #define N 500010 us ...

  6. poj1785 Binary Search Heap Construction

    此题可以先排序再用rmq递归解决. 当然可以用treap. http://poj.org/problem?id=1785 #include <cstdio> #include <cs ...

  7. POJ 2559 Largest Rectangle in a Histogram ——笛卡尔树

    [题目分析] 本来是单调栈的题目,用笛卡尔树可以快速的水过去. 把每一个矩阵看成一个二元组(出现的顺序,高度). 然后建造笛卡尔树. 神奇的发现,每一个节点的高度*该子树的大小,就是这一块最大的子矩阵 ...

  8. POJ 2201 Cartesian Tree ——笛卡尔树

    [题目分析] 构造一颗笛卡尔树,然后输出这棵树即可. 首先进行排序,然后用一个栈维护最右的树的节点信息,插入的时候按照第二关键字去找,找到之后插入,下面的树成为它的左子树即可. 然后插入分三种情况讨论 ...

  9. sdut2355Binary Search Heap Construction

    链接 捣鼓了一下午..按堆建树 写完交 返回TLE..数据不大 感觉不会超了 无奈拿了数据来看什么奇葩数据会超 发现数据跟我输出不一样 看了好久才明白理解错题意了 给出的字符串有两个标签 按前一个来建 ...

随机推荐

  1. 看云-git类的书籍写作

    看云-git类的书籍写作 https://www.kancloud.cn/explore 测试一本:https://www.kancloud.cn/stono/b001/501901

  2. myeclipse集成svn

    svn安装 这个我在博客中的代码管理里面有些,也是一直next.svn代码管理版本号管理器安装好之后. myeclipse的svn插件 方法一: 然后配置MyEclipse的SVN插件,将插件下载下来 ...

  3. 【解决方法】UITableView 性能优化笔记

    1.网络图片异步载入,SDWebImage. 2.文字直接 drawInRect/drawAtPoint 绘制,參考 ABTableViewCell.AdvancedTableViewCells. 3 ...

  4. Cocos2d-x 2.2.3 使用NDK配置安卓编译环境问题之 Cannot find module with tag &#39;CocosDenshion/android&#39; in import path

    1.当做安卓移植的时候遇到例如以下问题: Android NDK: jni/Android.mk: Cannot find module with tag 'CocosDenshion/android ...

  5. UVA - 10043 Chainsaw Massacre

    Description  Problem E: Chainsaw Massacre  Background As every year the Canadian Lumberjack Society ...

  6. 0xC0000005;Access Violation(栈区空间很宝贵, linux上栈区空间默认为8M,vc6下默认栈空间大小为1M)

    写C/C++程序最怕出现这样的提示了,还好是在调试环境下显示出来的,在非调试状态就直接崩溃退出. 从上述汇编代码发现在取内存地址 eax+38h 的值时出错, 那说明这个地址非法呗, 不能访问, 一般 ...

  7. 机器学习 数据量不足问题----1 做好特征工程 2 不要用太多的特征 3 做好交叉验证 使用线性svm

    来自:https://www.zhihu.com/question/35649122 其实这里所说的数据量不足,可以换一种方式去理解:在维度高的情况下,数据相对少.举一个特例,比如只有一维,和1万个数 ...

  8. 2.Matlab数值数组及其运算

    2.1引导 2.2一维数组的创建与寻访 2.3二维数组的创建 2.4二维数组元素的标识 2.5二维数组的子数组寻访和赋值 2.6执行数组运算的常用函数 2.7数组运算和矩阵运算 2.8多项式的表达和创 ...

  9. 观光奶牛Sightseeing Cows (二分+spfa(dfs))

    观光奶牛 农夫约翰已决定通过带他们参观大城市来奖励他们的辛苦工作!奶牛必须决定如何最好地度过他们的空闲时间. 幸运的是,他们有一个详细的城市地图,显示L(2≤L≤1000)主要地标(方便编号为1 .. ...

  10. Python—使用xm.dom解析xml文件

    什么是DOM? 文件对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可扩展置标语言的标准编程接口. 一个 DOM 的解析器在解析一个 XML 文档时,一次性读 ...