笛卡尔树 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.在这里只介绍几种常用 ...
随机推荐
- linux中直接进行系统调用和通过C库调用的示例
深入了解LINUX,这方面内容不可少,这段时间再补补.. #include <syscall.h> #include <unistd.h> #include <stdio ...
- sourceforge的FTP镜像
https://www.mirrorservice.org/sites/ftp.sourceforge.net/
- HDU 2045 LELE的RPG难题(递推)
不容易系列之(3)—— LELE的RPG难题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- [Python Debug]Kernel Crash While Running Neural Network with Keras|Jupyter Notebook运行Keras服务器宕机原因及解决方法
最近做Machine Learning作业,要在Jupyter Notebook上用Keras搭建Neural Network.结果连最简单的一层神经网络都运行不了,更奇怪的是我先用iris数据集跑了 ...
- Python3 字典(map)
ayout: post title: Python3 字典(map) author: "luowentaoaa" catalog: true tags: mathjax: true ...
- Codeforces Round 536 (Div. 2) (E)
layout: post title: Codeforces Round 536 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- 细菌(disease) (位运算)(状态压缩)
细菌(disease) 时间限制: 1 Sec 内存限制: 64 MB提交: 9 解决: 5[提交][状态][讨论版] 题目描述 近期,农场出现了D(1≤D≤15)种细菌.John要从他的N(1≤ ...
- java中为什么要用多线程
我们可以在计算机上运行各种计算机软件程序.每一个运行的程序可能包括多个独立运行的线程(Thread).线程(Thread)是一份独立运行的程序,有自己专用的运行栈.线程有可能和其他线程共享一些资源,比 ...
- thinkpad win8.1 无线连接受限
把博通的驱动回滚到6.30.223.102就好了
- Asp.Net MVC part3 路由Route
路由Route路由规则Route:可以查看源代码了解一下构造方法,需要指定路由格式.默认值.处理器三个值路由数据RouteData:当前请求上下文匹配路由规则而得到的一个对象,可以在Action中通过 ...