笛卡尔树 POJ ——1785 Binary Search Heap Construction
相应POJ 题目:点击打开链接
| Time Limit: 2000MS | Memory Limit: 30000K | |
| Total Submissions: 9075 | Accepted: 2566 |
Description
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
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
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("\n");
}
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;
}
笛卡尔树 POJ ——1785 Binary Search Heap Construction的更多相关文章
- 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-1785-Binary Search Heap Construction(笛卡尔树)
Description Read the statement of problem G for the definitions concerning trees. In the following w ...
- POJ 2559 Largest Rectangle in a Histogram ——笛卡尔树
[题目分析] 本来是单调栈的题目,用笛卡尔树可以快速的水过去. 把每一个矩阵看成一个二元组(出现的顺序,高度). 然后建造笛卡尔树. 神奇的发现,每一个节点的高度*该子树的大小,就是这一块最大的子矩阵 ...
- POJ 2201 Cartesian Tree ——笛卡尔树
[题目分析] 构造一颗笛卡尔树,然后输出这棵树即可. 首先进行排序,然后用一个栈维护最右的树的节点信息,插入的时候按照第二关键字去找,找到之后插入,下面的树成为它的左子树即可. 然后插入分三种情况讨论 ...
- 平衡树及笛卡尔树讲解(旋转treap,非旋转treap,splay,替罪羊树及可持久化)
在刷了许多道平衡树的题之后,对平衡树有了较为深入的理解,在这里和大家分享一下,希望对大家学习平衡树能有帮助. 平衡树有好多种,比如treap,splay,红黑树,STL中的set.在这里只介绍几种常用 ...
随机推荐
- hdu 5747(数学,贪心)
Aaronson Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- w3cschool javascript学习
name与Id属性区别 1.我们知道在网页做Post提交时,是以Form(即表单域)为单位进行提交的,一个Form里有若干个表单对象(如<input type="text" ...
- NumPy、SciPy 等Python包在Windows下的whl安装包下载
http://www.lfd.uci.edu/~gohlke/pythonlibs/ 感谢加利福尼亚大学尔湾分校(University of California, Irvine)荧光动力实验室(瞎翻 ...
- Nodejs调用Aras Innovator服务,处理AML并返回AML
公司已经布署了Aras Innovator服务器,如果需要与Aras Innovator进行交互,需要进行自主开发程序,例如使用C#.VB.Java等,都是可以与它进行交互的 C#:调用Aras In ...
- postman用法总结+newman持续集成
一.postman 1.GET 请求:点击Params,输入参数及value,可输入多个显示在URL链接上(GET请求的请求头与请求参数如在接口文档中无特别声明时可以不填) 2.POST请求:在bod ...
- redhat 安装 setuptools【成功】
1. wget --no-check-certificate http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py ...
- Windows下python的第三方库的安装
D:\Python27\Scripts\pip.exe install beautifulsoup4
- lca最短公共祖先模板(hdu2586)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 #include<iostream> #include<cstdio> ...
- [BZOJ5461][LOJ#2537[PKUWC2018]Minimax(概率DP+线段树合并)
还是没有弄清楚线段树合并的时间复杂度是怎么保证的,就当是$O(m\log n)$吧. 这题有一个显然的DP,dp[i][j]表示节点i的值为j的概率,转移时维护前缀后缀和,将4项加起来就好了. 这个感 ...
- [CF819B]Mister B and PR Shifts
题意:定义一个排列$p_{1\cdots n}$的“偏移量”$D=\sum _{i=1}^n\left|p_i-i\right|$ 求它所有的轮换排列中偏移量最小的是多少,要求输出轮换序数 暴力就是求 ...