相应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. CWnd创建WS_CHILD和WS_POPUP窗口的不同

    转载:http://blog.csdn.net/tangaowen/article/details/6054152 最近在写一个从CWnd派生出来的自绘窗口,以前在包装自己的类的Create函数都是这 ...

  2. 云平台服务运行情况检测脚本V0.1

    1.准备Python3环境 yum groupinstall "Development tools" -y yum install zlib-devel bzip2-devel o ...

  3. java.lang.ClassCastException: com.sun.proxy.$Proxy13 cannot be cast to sm.dao.UserDao

    在Spring中添加事物管理以后出现的问题 源代码 ApplicationContext applicationContext = new ClassPathXmlApplicationContext ...

  4. luogu P1186 玛丽卡

    题目描述 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们知道从一个城市到另一个城 ...

  5. 【后缀数组】poj2406 Power Strings

    连续重复子串(pku2406)给定一个字符串 L,已知这个字符串是由某个字符串 S 重复 R 次而得到的,求 R 的最大值.算法分析:做法比较简单,穷举字符串 S 的长度 k,然后判断是否满足.判断的 ...

  6. Problem P: 素数求和

    #include<stdio.h> int main() { ; scanf("%d",&n); n>=&&n<=; ;i<= ...

  7. Problem Z: 零起点学算法22——求正弦和余弦

    #include<stdio.h> #include <math.h> int main() { int n; ); double a,b; while(scanf(" ...

  8. 使用jQuery操作dom(追加和删除样式-鼠标移入移出)练习

    1.实现鼠标移入则添加背景色,鼠标移出则删除背景色 <!DOCTYPE html> <html> <head> <title>test1.html< ...

  9. Asp.Net MVC part2 View、Controller详解

    View详解Razor视图引擎简介HtmlHelper强类型页面 Razor视图引擎简介强大的@:表示使用C#代码,相当于aspx中的<%%>可以完成输出功能当遇到html标签时会认为C# ...

  10. NSOperation的并发与非并发

    NSoperation也是多线程的一种,NSopertaion有2种形式  (1) 并发执行       并发执行你需要重载如下4个方法     //执行任务主函数,线程运行的入口函数    - (v ...