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 ...
随机推荐
- 微信小程序之 SideBar(侧栏分类)
项目目录: 模拟数据: utils / data.js function getSData() { var data = [ { "id": 1, "tree" ...
- CSS多种方法实现分隔线
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8" /> <title&g ...
- 快看Sample代码,速学Swift语言(2)-基础介绍 快看Sample代码,速学Swift语言(1)-语法速览
快看Sample代码,速学Swift语言(2)-基础介绍 Swift语言是一个新的编程语言,用于iOS, macOS, watchOS, 和 tvOS的开发,不过Swift很多部分内容,我们可以从C或 ...
- Tomcat和Jetty对WebSocket的支持
公司项目须要,了解了下眼下几种支持WebSocket的框架.曾经用jWebSocket做过一些项目.相对来说.改jWebSocket的源代码略复杂,也不是一天两天能搞定的. 一调研才发现,如今非常多主 ...
- 汉诺塔算法c++源代码(递归与非递归)[转]
算法介绍: 其实算法非常简单,当盘子的个数为n时,移动的次数应等于2^n - 1(有兴趣的可以自己证明试试看).后来一位美国学者发现一种出人意料的简单方法,只要轮流进行两步操作就可以了.首先把三根柱 ...
- YTU 1020: I think it
1020: I think it 时间限制: 1 Sec 内存限制: 32 MB 提交: 501 解决: 63 题目描述 Xiao Ming is only seven years old, No ...
- Iterator 使用
Configuration cfg = new Configuration().configure(); SessionFactory factory = cfg.buildSessionFactor ...
- sphinx源码分析总结
http://www.cnblogs.com/bonelee/p/6667955.html shinx索引部分源码分析——过程:连接到CSphSource对应的sql数据源,通过fetch row取其 ...
- C语言-1.static 和 extern使用,2.文件,3.数据块读写
1.static 和 extern使用, 1)修饰局部变量 static修饰局部变量特点:延长局部变量的生命周期 ,static修饰的局部变量只会被执行一次 extern不能修饰局部变量 2)修饰全局 ...
- 二、Log4j基本使用方法
转自:https://blog.csdn.net/luohai859/article/details/52250807 Log4j由三个重要的组件构成:日志信息的优先级,日志信息的输出目的地,日志信息 ...