sdut2355Binary Search Heap Construction
链接
捣鼓了一下午。。按堆建树 写完交 返回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的更多相关文章
- 笛卡尔树 POJ ——1785 Binary Search Heap Construction
相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS Memory Limit: 30000K Total Subm ...
- ZOJ - 2243 - Binary Search Heap Construction
先上题目: Binary Search Heap Construction Time Limit: 5 Seconds Memory Limit: 32768 KB Read the sta ...
- POJ-1785-Binary Search Heap Construction(笛卡尔树)
Description Read the statement of problem G for the definitions concerning trees. In the following w ...
- poj1785 Binary Search Heap Construction
此题可以先排序再用rmq递归解决. 当然可以用treap. http://poj.org/problem?id=1785 #include <cstdio> #include <cs ...
- POJ 1785 Binary Search Heap Construction(裸笛卡尔树的构造)
笛卡尔树: 每个节点有2个关键字key.value.从key的角度看,这是一颗二叉搜索树,每个节点的左子树的key都比它小,右子树都比它大:从value的角度看,这是一个堆. 题意:以字符串为关键字k ...
- [POJ1785]Binary Search Heap Construction(笛卡尔树)
Code #include <cstdio> #include <algorithm> #include <cstring> #define N 500010 us ...
- POJ 1785 Binary Search Heap Construction (线段树)
题目大意: 给出的东西要求建立一个堆,使得后面的数字满足堆的性质.并且字符串满足搜索序 思路分析: 用线段树的最大询问建树.在建树之前先排序,然后用中序遍历递归输出. 注意输入的时候的技巧. .. # ...
- FTP客户端上传下载Demo实现
1.第一次感觉MS也有这么难用的MFC类: 2.CFtpFileFind类只能实例化一个,多个实例同时查找会出错(因此下载时不能递归),采用队列存储目录再依次下载: 3.本程序支持文件夹嵌套上传下载: ...
- table2excel使用
原table2excel代码 /* * 采用jquery模板插件——jQuery Boilerplate * * Made by QuJun * 2017/01/10 */ //table2excel ...
随机推荐
- [MAT]使用MAT比較多个heap dump文件
使用MAT比較多个heap dump文件 调试内存泄露时,有时候适时比較2个或多个heap dump文件是非常实用的.这时须要生成多个单独的HPROF文件. 以下是一些关于怎样在MAT里比較多个hea ...
- Linux下怎么添加和查看PATH环境变量
linux下查看和添加PATH环境变量来自:http://apps.hi.baidu.com/share/detail/32942984 $PATH:决定了shell将到哪些目录中寻找命令或程序,PA ...
- IOS 字典模型互转框架 MJExtension
IOS 字典模型互转框架 MJExtension 能做什么? MJExtension是一套字典和模型之间互相转换的超轻量级框架 MJExtension能完成的功能 字典(JSON) --> ...
- Filter 详解
一.Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态 ...
- Oracle 表的创建 及相关參数
1. 创建表完整语法 CREATE TABLE [schema.]table (column datatype [, column datatype] - ) [TABLESPACE tablespa ...
- java语法基础(一)
这个是自己前两年做java视频教程时候的课件.感兴趣的同学可以参考下. 这里是纯粹的语法行总结. editplus的使用 选择项目目录 打开editplus 左侧目录导航栏 可以打开盘符,文件夹 可以 ...
- HDU1540 Tunnel Warfare —— 线段树 区间合并
题目链接:https://vjudge.net/problem/HDU-1540 uring the War of Resistance Against Japan, tunnel warfare w ...
- 【转】浏览器中输入url后发生了什么
原文地址:http://www.jianshu.com/p/c1dfc6caa520 在学习前端的过程中经常看到这样一个问题:当你在浏览器中输入url后发生了什么?下面是个人学习过程中的总结,供个人复 ...
- uva 11401
Triangle Counting Input: Standard Input Output: Standard Output You are given n rods of length 1, 2… ...
- MD5 密码加密算法 系统等待
MD5 密码加密算法 public static String md(String md, String pass) { MessageDigest m; String passok = " ...