POJ-1785-Binary Search Heap Construction(笛卡尔树)
Description
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
of lower-case letters, and the numbers are non-negative integers. The last test case is followed by a zero.
Output
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(笛卡尔树)的更多相关文章
- 笛卡尔树 POJ ——1785 Binary Search Heap Construction
相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS Memory Limit: 30000K Total Subm ...
- POJ 1785 Binary Search Heap Construction(裸笛卡尔树的构造)
笛卡尔树: 每个节点有2个关键字key.value.从key的角度看,这是一颗二叉搜索树,每个节点的左子树的key都比它小,右子树都比它大:从value的角度看,这是一个堆. 题意:以字符串为关键字k ...
- POJ 1785 Binary Search Heap Construction (线段树)
题目大意: 给出的东西要求建立一个堆,使得后面的数字满足堆的性质.并且字符串满足搜索序 思路分析: 用线段树的最大询问建树.在建树之前先排序,然后用中序遍历递归输出. 注意输入的时候的技巧. .. # ...
- ZOJ - 2243 - Binary Search Heap Construction
先上题目: Binary Search Heap Construction Time Limit: 5 Seconds Memory Limit: 32768 KB Read the sta ...
- [POJ1785]Binary Search Heap Construction(笛卡尔树)
Code #include <cstdio> #include <algorithm> #include <cstring> #define N 500010 us ...
- poj1785 Binary Search Heap Construction
此题可以先排序再用rmq递归解决. 当然可以用treap. http://poj.org/problem?id=1785 #include <cstdio> #include <cs ...
- POJ 2559 Largest Rectangle in a Histogram ——笛卡尔树
[题目分析] 本来是单调栈的题目,用笛卡尔树可以快速的水过去. 把每一个矩阵看成一个二元组(出现的顺序,高度). 然后建造笛卡尔树. 神奇的发现,每一个节点的高度*该子树的大小,就是这一块最大的子矩阵 ...
- POJ 2201 Cartesian Tree ——笛卡尔树
[题目分析] 构造一颗笛卡尔树,然后输出这棵树即可. 首先进行排序,然后用一个栈维护最右的树的节点信息,插入的时候按照第二关键字去找,找到之后插入,下面的树成为它的左子树即可. 然后插入分三种情况讨论 ...
- sdut2355Binary Search Heap Construction
链接 捣鼓了一下午..按堆建树 写完交 返回TLE..数据不大 感觉不会超了 无奈拿了数据来看什么奇葩数据会超 发现数据跟我输出不一样 看了好久才明白理解错题意了 给出的字符串有两个标签 按前一个来建 ...
随机推荐
- linux驱动之poll操作
POLL操作 1.POLL运行过程: poll是一个系统调用,其内核入口函数为sys_poll,sys_poll差点儿不做不论什么处理直接调用do_sys_poll,do_sys_poll的运行过程能 ...
- linux删除多行
光标放到行dd:删除所在行 光标放到行Ndd: 删除所在行下的N行
- Android学习笔记(十二)——使用意图传递数据的几种方式
使用意图传递数据的几种方式 点此获取完整代码 我们除了要从活动返回数据,也经常要传递数据给活动.对此我们能够使用Intent对象将这些数据传递给目标活动. 1.创建一个名为PassingData的项目 ...
- Troubleshooting Failed Requests Using Tracing in IIS 8.5
https://docs.microsoft.com/en-us/iis/troubleshoot/using-failed-request-tracing/troubleshooting-faile ...
- electron-vue中使用iview 报错this. is readonly的解决办法
title: electron-vue中使用iview 报错this. is readonly的解决办法 toc: false date: 2019-02-12 19:33:28 categories ...
- 算法入门经典-第六章 例题6-21 SystemDependencies
题意:软件组件之间会有依赖关系,比如你下一个Codeblocks你也得顺带着把编译器给下上.你的任务是模拟安装和卸载软件组件的过程.有以下五种指令,如果指令为“END”则退出程序:若为以下四种指令,则 ...
- SQLSERVER 链接服务器执行存储过程
1.创建链接服务器 exec sp_addlinkedserver 'server_tmp','','SQLOLEDB','192.168.1.1' -- server_tmp 为别名 exec sp ...
- PHP验证器类Validator
Particle\Validator是一个小巧优雅的实用的PHP验证类库,提供了一个非常简洁的API.它无需依赖其他组件,提供友好的文档,并且有利于扩展. 安装 composer require pa ...
- Android蓝牙2.0连接以及数据接收发送
1.加入权限 <uses-feature android:name="android.hardware.bluetooth_le" android:required=&quo ...
- JS面向对像编程四—— prototype 对象
http://blog.csdn.net/fanwenjieok/article/details/54575560 大部分面向对象的编程语言,都是以“类”(class)作为对象体系的语法基础.Java ...