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

  1. 7 a/7 b/6 c/5 d/4 e/3 f/2 g/1
  2. 7 a/1 b/2 c/3 d/4 e/5 f/6 g/7
  3. 7 a/3 b/6 c/4 d/7 e/2 f/5 g/1
  4. 0

Sample Output

  1. (a/7(b/6(c/5(d/4(e/3(f/2(g/1)))))))
  2. (((((((a/1)b/2)c/3)d/4)e/5)f/6)g/7)
  3. (((a/3)b/6(c/4))d/7((e/2)f/5(g/1)))

Source

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

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. struct S{
  7. int id,val;
  8. int parent,l,r;
  9. }node[50005];
  10.  
  11. int stk[50005],top;
  12. char s[50005][20];
  13.  
  14. bool cmp(struct S a,struct S b)
  15. {
  16. if(strcmp(s[a.id],s[b.id])<0) return 1;
  17.  
  18. return 0;
  19. }
  20.  
  21. int build(int n)
  22. {
  23. int i;
  24.  
  25. top=0;
  26.  
  27. stk[top]=0;
  28.  
  29. for(i=1;i<n;i++)
  30. {
  31. while(top>=0 && node[stk[top]].val<node[i].val) top--;//注意。这里让小于当前值的出栈
  32.  
  33. if(top>-1)
  34. {
  35. node[i].parent=stk[top];
  36. node[node[stk[top]].r].parent=i;
  37. node[i].l=node[stk[top]].r;
  38. node[stk[top]].r=i;
  39. }
  40. else
  41. {
  42. node[stk[0]].parent=i;
  43. node[i].l=stk[0];
  44. }
  45.  
  46. stk[++top]=i;
  47. }
  48.  
  49. return stk[0];//返回根节点
  50. }
  51.  
  52. void dfs(int x)
  53. {
  54. printf("(");//先输出左括号
  55.  
  56. if(node[x].l!=-1) dfs(node[x].l);//假设有左子树。就先输出左子树
  57.  
  58. printf("%s/%d",s[node[x].id],node[x].val);//输出自己
  59.  
  60. if(node[x].r!=-1) dfs(node[x].r);//再输出右子树
  61.  
  62. printf(")");//右括号
  63. }
  64.  
  65. int main()
  66. {
  67. int n,i,root;
  68.  
  69. while(~scanf("%d",&n) && n)
  70. {
  71. for(i=0;i<n;i++)
  72. {
  73. scanf("%*[ ]%[a-z]/%d",s[i],&node[i].val);
  74.  
  75. node[i].id=i;
  76.  
  77. node[i].parent=node[i].l=node[i].r=-1;
  78. }
  79.  
  80. sort(node,node+n,cmp);//按字符串字典序排序
  81.  
  82. root=build(n);//建树
  83.  
  84. dfs(root);//递归输出
  85.  
  86. printf("\n");
  87. }
  88. }

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. [Tailwind] Get started with Tailwindcss

    In this lesson, we learn how to generate CSS utility classes from Tailwind's JavaScript config file. ...

  2. Tween动画TranslateAnimation细节介绍

    Tween动画有下面这几种: Animation   动画 AlphaAnimation 渐变透明度 RotateAnimation 画面旋转 ScaleAnimation 渐变尺寸缩放 Transl ...

  3. 初次使用Android Studio时的配置

    一.第一次安装: Android Studio安装完毕后,第一次启动AS前.为了避免又一次下载新版本号的SDK.操作例如以下: AS启动前.请先将bin文件夹的idea.properties文件里添加 ...

  4. DNS隧道工具汇总——补充,还有IP over DNS的工具NSTX、Iodine、DNSCat

    github上有一堆的工具:https://github.com/search?utf8=%E2%9C%93&q=DNS+tunnel+&type= DNS隧道大检阅 研究了一天的DN ...

  5. 4.vim操作

    你想以最快的速度学习人类史上最好的文本编辑器VIM吗?你先得懂得如何在VIM幸存下来,然后一点一点地学习各种戏法. 我建议下面这四个步骤: 存活 感觉良好 觉得更好,更强,更快 使用VIM的超能力 当 ...

  6. BZOJ 3796 后缀数组+KMP

    思路: 写得我头脑发蒙,,, 旁边还有俩唱歌的 抓狂 (感谢lh大爷查错) 首先 1.w是s1的子串 2.w是s2的子串 这两步很好办啊~ 后缀数组一下O(n)就可以搞 重点是 这个:3.s3不是w的 ...

  7. POJ 1201 差分约束+SPFA

    思路: 差分约束,难在建图.(我是不会告诉你我刚学会SPFA的...) 把每个区间的ai–>bi连一条长度为ci的边. k–>k+1连一条长度为0的边. k+1–>k连一条长度为-1 ...

  8. caffe下python环境的编译

    安装python所需的依赖包 (1)sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-n ...

  9. line-height与间距总总

    一点说明(个人吐槽,可以略过) 之所以想写这篇文章,是因为自己工作的经验总结.以前的页面编写极度不注重间距大小,特别是行级元素间距.认为只要差不多好就行了,没必要花那么大的精力去抠几px的小细节.事实 ...

  10. 微信小程序-最新获取用户基本信息方案

    如果只是单纯的展示用户信息,那么最简单的方案就是 文档中组件: <open-data type="groupName" open-gid="xxxxxx" ...