捣鼓了一下午。。按堆建树 写完交 返回TLE。。数据不大 感觉不会超了 无奈拿了数据来看什么奇葩数据会超 发现数据跟我输出不一样 看了好久才明白理解错题意了

给出的字符串有两个标签 按前一个来建二叉搜索树 按后一个建堆 搜索树直接排序 然后依次插入右子树  若当前值比前一个的值大 就在与其父亲比较 总之放到合适位置。

这样的树叫笛卡尔树

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<cmath>
#include<queue>
#include<vector>
#include<stack>
#include<set>
#include<string>
using namespace std;
#define N 50010
struct node
{
int l,r,p,f;
char s[];
void init()
{
l=r=f=;
}
}tr[N];
int g;
void build(int u)
{
int v = u-;
while(tr[u].p>tr[v].p&&v) v = tr[v].f;
tr[u].l = tr[v].r;
tr[v].r = u;
tr[u].f = v;
}
void solve(int root)
{
if(tr[root].l)
{
printf("(");
solve(tr[root].l);
printf(")");
}
printf("%s",tr[root].s);
if(tr[root].r)
{
printf("(");
solve(tr[root].r);
printf(")");
}
}
bool cmp(node x,node y)
{
return strcmp(x.s,y.s)<;
}
int main()
{
// freopen("data8.in","r",stdin);
// freopen("data.out","w",stdout);
int i,j,k,n;
while(scanf("%d",&n)&&n)
{
int mm = ,st;g=;
for(i = ; i <= n ;i++)
{
scanf("%s",tr[i].s);
}
sort(tr+,tr+n+,cmp);
for(i = ; i <= n ;i++)
{
for(j = ; j < strlen(tr[i].s) ; j++)
{
if(tr[i].s[j]=='/') break;
}
tr[i].p = atoi(tr[i].s+j+);
if(mm<tr[i].p)
{
mm = tr[i].p;
st = i;
}
tr[i].init();
}
tr[].r = ;
tr[].f = ;
for(i = ; i <= n ;i++)
build(i);
printf("(");
solve(st);
printf(")");
puts("");
}
return ;
}

sdut2355Binary Search Heap Construction的更多相关文章

  1. 笛卡尔树 POJ ——1785 Binary Search Heap Construction

    相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS   Memory Limit: 30000K Total Subm ...

  2. ZOJ - 2243 - Binary Search Heap Construction

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

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

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

  4. poj1785 Binary Search Heap Construction

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

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

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

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

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

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

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

  8. FTP客户端上传下载Demo实现

    1.第一次感觉MS也有这么难用的MFC类: 2.CFtpFileFind类只能实例化一个,多个实例同时查找会出错(因此下载时不能递归),采用队列存储目录再依次下载: 3.本程序支持文件夹嵌套上传下载: ...

  9. table2excel使用

    原table2excel代码 /* * 采用jquery模板插件——jQuery Boilerplate * * Made by QuJun * 2017/01/10 */ //table2excel ...

随机推荐

  1. 【iOS开发】-NSString的扩展使用

    第一:基本数据类型与字符串转换 //基本数据类型(int float,double char) 1)基本数据类型->NSString //1.int类型换换成字符串 int a = 88; NS ...

  2. HDFS集中式缓存管理(Centralized Cache Management)

    Hadoop从2.3.0版本号開始支持HDFS缓存机制,HDFS同意用户将一部分文件夹或文件缓存在HDFS其中.NameNode会通知拥有相应块的DataNodes将其缓存在DataNode的内存其中 ...

  3. Library Project里面使用Case语句判断R.id值报错。case expressions must be constant expressions

    原文地址:http://blog.csdn.net/wchinaw/article/details/7325641 在一般的Android项目里R里面的资源声明看起来是这样的: public stat ...

  4. Calling a parent window function from an iframe

    I want to call a parent window JavaScript function from an iframe. <script>function abc(){ ale ...

  5. FOUNDATION OF ASYNCHRONOUS PROGRAMMING

    The async and await keywords are just a compiler feature. The compiler creates code by using the Tas ...

  6. JVM垃圾回收面试题

    Java垃圾回收有个经典面试题,什么时候,对什么对象,做了什么操作? 垃圾回收里涉及内容很多,要准确回答这个问题首先要先限定边界.分清楚虚拟机规范定义和不同虚拟机实现的差异.以工作中用到的hotspo ...

  7. 分析函数Ratio_to_report使用

    分析函数Ratio_to_report( ) over()使用说明 表中需要计算单项占比:比如单项在部门占比多少,单项在公司占比多少.特别是在财务单项计算,部门个人薪水计算上. Ratio_to_re ...

  8. Ocelot(十一)- 服务发现

    Ocelot允许您指定服务发现提供程序,并使用它来查找Ocelot正在将请求转发给下游服务的主机和端口.目前,这仅在GlobalConfiguration部分中受支持,这意味着所有ReRoute将使用 ...

  9. Luogu P1541 乌龟棋 【线性dp】

    题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行 N 个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第 N 格是终点,游戏要求玩家控制一个乌龟 ...

  10. 《开源自主OdooERP部署架构指南》试读:第二章数据库服务构建

    文/开源智造联合创始人老杨 本文来自<开源自主OdooERP部署架构指南>的试读章节.书籍尚未出版,请勿转载.欢迎您反馈阅读意见. 使用apt.postgresql.org 您可以选择使用 ...