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

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

这样的树叫笛卡尔树

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<stdlib.h>
  6. #include<cmath>
  7. #include<queue>
  8. #include<vector>
  9. #include<stack>
  10. #include<set>
  11. #include<string>
  12. using namespace std;
  13. #define N 50010
  14. struct node
  15. {
  16. int l,r,p,f;
  17. char s[];
  18. void init()
  19. {
  20. l=r=f=;
  21. }
  22. }tr[N];
  23. int g;
  24. void build(int u)
  25. {
  26. int v = u-;
  27. while(tr[u].p>tr[v].p&&v) v = tr[v].f;
  28. tr[u].l = tr[v].r;
  29. tr[v].r = u;
  30. tr[u].f = v;
  31. }
  32. void solve(int root)
  33. {
  34. if(tr[root].l)
  35. {
  36. printf("(");
  37. solve(tr[root].l);
  38. printf(")");
  39. }
  40. printf("%s",tr[root].s);
  41. if(tr[root].r)
  42. {
  43. printf("(");
  44. solve(tr[root].r);
  45. printf(")");
  46. }
  47. }
  48. bool cmp(node x,node y)
  49. {
  50. return strcmp(x.s,y.s)<;
  51. }
  52. int main()
  53. {
  54. // freopen("data8.in","r",stdin);
  55. // freopen("data.out","w",stdout);
  56. int i,j,k,n;
  57. while(scanf("%d",&n)&&n)
  58. {
  59. int mm = ,st;g=;
  60. for(i = ; i <= n ;i++)
  61. {
  62. scanf("%s",tr[i].s);
  63. }
  64. sort(tr+,tr+n+,cmp);
  65. for(i = ; i <= n ;i++)
  66. {
  67. for(j = ; j < strlen(tr[i].s) ; j++)
  68. {
  69. if(tr[i].s[j]=='/') break;
  70. }
  71. tr[i].p = atoi(tr[i].s+j+);
  72. if(mm<tr[i].p)
  73. {
  74. mm = tr[i].p;
  75. st = i;
  76. }
  77. tr[i].init();
  78. }
  79. tr[].r = ;
  80. tr[].f = ;
  81. for(i = ; i <= n ;i++)
  82. build(i);
  83. printf("(");
  84. solve(st);
  85. printf(")");
  86. puts("");
  87. }
  88. return ;
  89. }

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. MySQL基础笔记(三) 复杂查询

    所谓复杂查询,指涉及多个表.具有嵌套等复杂结构的查询.这里简要介绍典型的几种复杂查询格式. 一.连接查询 连接是区别关系与非关系系统的最重要的标志.通过连接运算符可以实现多个表查询.连接查询主要包括内 ...

  2. js将月份转换为英文简写的形式

    本文通过代码实例介绍一下如何实现将月份转换为英文简写的形式. 比如将一月份转换为Jan,需要的朋友可以做一下参考. 代码实例如下: [JavaScript] 纯文本查看 复制代码运行代码 1 2 3 ...

  3. C#数据库连接池 MySql SqlServer

    查阅了一天的资料来学习MySql数据库连接池,终于在一篇博文上找到了,自己也整理了一下,希望对大家有用处 1. 建立连接池 using MySql.Data.MySqlClient; using Sy ...

  4. 实战c++中的string系列--指定浮点数有效数字并转为string

    上一篇博客讲了好几种方法进行number到string的转换,这里再单独说一下float或是double到string的转换. 还是处于控件显示的原因.比方说要显示文件的大小,我们从server能够获 ...

  5. flask的CBV,flash,Flask-Session,及WTForms-MoudelForm

    1,CBV: from flask import vews class LoginView(views.MethodView): def get(self): return "雪雪其实也很好 ...

  6. slf4j的总结

    参考文章 log4j2使用总结 slf4j介绍以及实现原理窥探 使用Slf4j集成Log4j2构建项目日志系统的完美解决方案 slf4j(全称是Simple Loging Facade For Jav ...

  7. Koa2学习(八)使用session

    Koa2学习(八)使用session koa2框架不提供session的处理方法,这里我们需要借助一个第三方中间件koa-session来处理session. 先安装插件: $ npm i koa-s ...

  8. Poisson distribution 泊松分布 指数分布

    Poisson distribution - Wikipedia https://en.wikipedia.org/wiki/Poisson_distribution Jupyter Notebook ...

  9. Test redis

    单机测试: public class RedisClient{ private Jedis jedis; private JedisPool jedisPool; private ShardedJed ...

  10. String的属性和方法

    package com.zzu.java.array; public class TtString { /** * @author 程路超 * @param args */ public static ...