博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ1785 Binary Search Heap Construction
阅读量:6209 次
发布时间:2019-06-21

本文共 2517 字,大约阅读时间需要 8 分钟。

Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 10309   Accepted: 2838

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/17 a/1 b/2 c/3 d/4 e/5 f/6 g/77 a/3 b/6 c/4 d/7 e/2 f/5 g/10

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

 

树 笛卡尔树模板题

笛卡尔树满足两个性质:中序遍历就是原数组(二叉搜索树性质),以及数值满足堆的性质(上小下大或上大下小)。

基本上就是没加随机旋转的treap

这题把读入数据建成笛卡尔树即可,要求字符串满足二叉树性质,数值满足大根堆性质

strcmp跑得真够慢

 

1 /*by SilverN*/ 2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 using namespace std; 9 const int mxn=100010;10 struct node{11 char s[30];12 int val;13 int fa,l,r;14 bool operator < (node b)const{15 return strcmp(s,b.s)<0;16 }17 }t[mxn];18 void init(int n){19 for(int i=0;i<=n;i++){20 t[i].l=t[i].r=t[i].fa=0;21 }22 t[0].val=0x3f3f3f3f;23 }24 int n;25 //int st[mxn],top=0;26 void Build(){27 for(int i=1;i<=n;i++){28 int j=i-1;29 while(t[j].val

 

转载于:https://www.cnblogs.com/SilverNebula/p/6686584.html

你可能感兴趣的文章
Linux_DHCP中继服务配置
查看>>
预定义异常 - PHP手册笔记
查看>>
[原创]关于Infobright 的几种数据格式
查看>>
CF刷题-Codeforces Round #481-G. Petya's Exams
查看>>
CSS之transform-origin属性
查看>>
UVA 11584 最短回文串划分 DP
查看>>
3路抢答器(键盘+数码管+LED灯+蜂鸣器)
查看>>
C协程实现的效率对比
查看>>
EF-CodeFirst-1 玩起来
查看>>
Callable和Future
查看>>
使用正则表达式,取得点击次数,函数抽离
查看>>
JS正则表达式的基础用法
查看>>
一段代码,欢迎讨论
查看>>
UML结构与解析——BUAA OO第四单元作业总结
查看>>
Python网络编程(3)——SocketServer模块与简单并发服务器
查看>>
centos 编译安装mono
查看>>
XHTML 相对路径与绝对路径
查看>>
mysql 修改表的每个列的字符类型
查看>>
win7 环境下实现faster-rcnn
查看>>
2019全球智慧零售大会将于5月30-31在厦门召开!
查看>>