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 ...
随机推荐
- linux之rsync远程数据同步备份
rsync服务是一种高效的远程数据备份的工具,该服务的port号为873, 是Liunx下的一种非独立服务.由xinetd超级服务管理,取代监听873port. 长处: 1.rsync能够利用ssh和 ...
- 在EasyUI的DataGrid中嵌入Combobox
在做项目时,须要在EasyUI的DataGrid中嵌入Combobox,花了好几天功夫,在大家的帮助下,最终看到了它的庐山真面: 核心代码例如以下: <html> <head> ...
- 关于Scrum
最近某些产品经理发出下两周的工作计划的时候,喜欢带上sprint这个字眼,看上去貌似是要走敏捷开发这一套,只可惜,我觉得他表现出来的是对敏捷开发和Scrum一窍不通,甚至对软件开发流程都完全不清楚,居 ...
- 性能监控 -- 中间件性能监控【Weblogic控制台】
通过WebLogic管理控制台可以实时获取各性能指标,通过控制台,可以对weblogic的性能及运行状况,发布的应用.资源等进行监视 1. 进入Weblogic管理控制台,单击服务器,选择一台需监控的 ...
- 微软Build2016:Xamarin杂记
去年的Build2015技术大会.留给人印象最深的莫过是Windows 10在手机端.PC端.Xbox等硬件平台上的大一统.还有非常具有科幻气质的HoloLens技术的各种展示.去年尽管也展示了Xam ...
- 使用命令行工具提升cocos2d-x开发效率 之CocosBuilder篇
http://www.cnblogs.com/flyFreeZn/p/3617983.html 假设你正在使用CocosBuilder或者是其它基于CocosBuilder源代码改装而成的工具为你的游 ...
- java.lang.ClassCastException: $Proxy0 cannot be cast to javax.servlet.ServletRequestWrapper
我是在myeclipse 8.5使用代理的时候遇到这个问题的.因为我是跟着视频敲的,所以按道理代码是不会有问题..最后经过我百折不挠的折腾,我发现问题的根源 是 myeclipse自带的tomcat. ...
- Android之——经常使用手机号码功能
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47374415 有些Android手机中会带有一些经常使用号码的功能,比方订餐电话. ...
- FFT做题记录
FFT是用来快速求卷积的..... 那么卷积有什么作用呢 https://www.zhihu.com/question/22298352 看完就懂了
- eclipse 重启/打开内置浏览器
重启 Eclipse 重启选项允许用户重启 Eclipse. 我们可以通过点击 File 菜单选择 Restart 菜单项来重启 Eclipse. Eclipse 内置浏览器 Web 浏览器 Ecli ...