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 ...
随机推荐
- AFNetworking配合Swift3.0请求数据
首先用桥接或pods将AFNetworking导入项目,在这不再赘述,然后创建一个单例NetWorkTools.swift 继承:AFHTTPSessionManager import UIKit i ...
- fixedBox固定div漂浮代码 支持ie6以上大部分浏览器
fixedBox固定div漂浮代码 支持ie6以上大部分浏览器 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E ...
- UIButton的图片和文字相对位置调整
通常.假设直接设置UIButton的图片和文字,默认的两者相对位置可能不是我们想要的,那么须要进行调整. 须要用到的函数例如以下: UIEdgeInsetsMake(CGFloat top, CGFl ...
- Vue框架之组件系统
1,Vue组件系统之全局组件 1.1Vue全局组件的在实例化调用Vue的模板中导入组件的名称 <!DOCTYPE html> <html lang="zh-cn" ...
- 如何用DOS命令,获取一个目录下的文件数目
发信人: GOOGOODALLS (我爱Figo), 信区: DOS 标 题: 如何用DOS命令,获取一个目录下的文件数目? 发信站: 水木社区 (Fri Mar 9 08:40:01 2007) ...
- CMake使用总结【转】
本文转载自:https://www.mawenbao.com/note/cmake.html 总结CMake的常用命令,并介绍有用的CMake资源. CMake意为cross-platform mak ...
- POJ2594 Treasure Exploratio —— 最小路径覆盖 + 传递闭包
题目链接:https://vjudge.net/problem/POJ-2594 Treasure Exploration Time Limit: 6000MS Memory Limit: 655 ...
- idea新建springmvc+spring+mybaties项目2
1,项目创建完成后,src-main下建立java目录后,是无法在该目录下创建新的包和java类等文件的.在idea中需要对目录进行标注 Sources 一般用于标注类似 src 这种可编译目录.有时 ...
- sphinx是支持结果聚类的——WHERE、ORDER BY和GROUP BY
原生API提供的匹配筛选.排序和分组配置和SQL语法提供的WHERE.ORDER BY和GROUP BY语句的效果是一样的,你可以对匹配结果进行你需要的筛选.排序和分组匹配.例如,如果你要搜索MySQ ...
- Splay基本操作
我们以一道题来引入吧! 传送门 题目说的很清楚,我们的数据结构要支持:插入x数,删除x数,查询数的排名和排名为x的数,求一个数前驱后继. 似乎用啥现有的数据结构都很难做到在O(nlogn)的复杂度中把 ...