相应POJ 题目:点击打开链接

Binary Search Heap Construction
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 9075   Accepted: 2566

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)))

题意:

每次有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的更多相关文章

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

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

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

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

  3. ZOJ - 2243 - Binary Search Heap Construction

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

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

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

  5. poj1785 Binary Search Heap Construction

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

  6. POJ-1785-Binary Search Heap Construction(笛卡尔树)

    Description Read the statement of problem G for the definitions concerning trees. In the following w ...

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

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

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

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

  9. 平衡树及笛卡尔树讲解(旋转treap,非旋转treap,splay,替罪羊树及可持久化)

    在刷了许多道平衡树的题之后,对平衡树有了较为深入的理解,在这里和大家分享一下,希望对大家学习平衡树能有帮助. 平衡树有好多种,比如treap,splay,红黑树,STL中的set.在这里只介绍几种常用 ...

随机推荐

  1. 理解Java中【包】的概念

    Java中用package关键字定义一个包,下面通过几个实验,理解Java中的包的概念和作用. 实验1:先看一个无包的情形 在G盘下新建一个Test.java,如图1: 写下面这些代码 <spa ...

  2. (21)python Anaconda

    1.安装 Anaconda下载镜像      --官网下载太慢 https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/ 下载安装后 2.添加第三方源 管 ...

  3. HDU 1556 Color the ball【差分数组裸题/模板】

    N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一 ...

  4. ( 转 ) MySQL高级 之 explain执行计划详解

    使用explain关键字可以模拟优化器执行SQL查询语句,从而知道MySQL是如何处理你的SQL语句的,分析你的查询语句或是表结构的性能瓶颈. explain执行计划包含的信息 其中最重要的字段为:i ...

  5. HDOJ 2582 f(n)

    Discription This time I need you to calculate the f(n) . (3<=n<=1000000) f(n)= Gcd(3)+Gcd(4)+… ...

  6. [LOJ6436]神仙的游戏

    感觉border的性质还是挺神奇的 一个border的性质是$S$有长度为$len$的border当且仅当对$\forall i\equiv j\left(\bmod(n-len)\right)$有$ ...

  7. 【树上莫队】【带修莫队】【权值分块】bzoj1146 [CTSC2008]网络管理Network

    #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using ...

  8. [NEERC2007][SHOI2008]Cactus Reloaded

    题目大意: 给你一个仙人掌,求图中相距最远的点对之间的距离. 思路: Tarjan+DP. 我们先考虑一个树的情况. 设用far[u]表示点u出发到其子树中叶子节点的最大距离,若v为u的子结点,很显然 ...

  9. 【R笔记】给R加个编译器——notepad++

    R的日记-给R加个编译器 转载▼ R是一款强大免费且开源的统计分析软件,这是R的长处,可也是其“缺陷”的根源:不似商业软件那样user-friendly.记得初学R时,给我留下最深印象的不是其功能的强 ...

  10. MathType插入带序号公式的两种方法

    方法一: 由于我之前使用表格15% 70% 15%来布局的,所以最开始相的就是如何录入公示后插入公式序号,如下图所示 先设置序号格式 录好公式后点“Insert Number”就好了,这样的话需要紧挨 ...