此题可以先排序再用rmq递归解决。

当然可以用treap。

http://poj.org/problem?id=1785

 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 5e4 + ;
struct Point{
char str[];
int pr;
bool operator < (const Point& rhs) const{
return strcmp(str, rhs.str) < ;
}
}a[maxn]; struct Data{
int p, v;
}st[maxn][]; int n; int query(int l, int r){
int len = r - l, d = ;
while(( << d) <= len) d++;
if(st[l][d - ].v > st[r - ( << (d - ))][d - ].v) return st[l][d - ].p;
else return st[r - ( << (d - ))][d - ].p;
} void print(int l, int r){
if(l > r) return;
putchar('(');
int maxp = query(l, r + );
print(l, maxp - );
printf("%s", a[maxp].str);
print(maxp + , r);
putchar(')');
} void RMQ(){
for(int j = ; j < n; j++) st[j][].v = a[j].pr, st[j][].p = j;
for(int i = ; ( << i) <= n; i++) for(int j = ; j < n; j++){
if(j + ( << i) <= n){
if(st[j][i - ].v > st[j + ( << (i - ))][i - ].v){
st[j][i].v = st[j][i - ].v;
st[j][i].p = st[j][i - ].p;
}else{
st[j][i].v = st[j + ( << (i - ))][i - ].v;
st[j][i].p = st[j + ( << (i - ))][i - ].p;
}
}
}
} int main(){
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
while(~scanf("%d", &n) && n){
for(int i = ; i < n; i++){
scanf("%s", a[i].str);
int len = strlen(a[i].str);
a[i].pr = ;
for(int j = len - , p = ; a[i].str[j] != '/'; j--, p *= )
a[i].pr += p * (a[i].str[j] - '');
}
sort(a, a + n);
RMQ();
print(, n - );
putchar('\n');
}
return ;
}

poj1785 Binary Search Heap Construction的更多相关文章

  1. [POJ1785]Binary Search Heap Construction(笛卡尔树)

    Code #include <cstdio> #include <algorithm> #include <cstring> #define N 500010 us ...

  2. 笛卡尔树 POJ ——1785 Binary Search Heap Construction

    相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS   Memory Limit: 30000K Total Subm ...

  3. ZOJ - 2243 - Binary Search Heap Construction

    先上题目: Binary Search Heap Construction Time Limit: 5 Seconds      Memory Limit: 32768 KB Read the sta ...

  4. POJ 1785 Binary Search Heap Construction(裸笛卡尔树的构造)

    笛卡尔树: 每个节点有2个关键字key.value.从key的角度看,这是一颗二叉搜索树,每个节点的左子树的key都比它小,右子树都比它大:从value的角度看,这是一个堆. 题意:以字符串为关键字k ...

  5. POJ 1785 Binary Search Heap Construction (线段树)

    题目大意: 给出的东西要求建立一个堆,使得后面的数字满足堆的性质.并且字符串满足搜索序 思路分析: 用线段树的最大询问建树.在建树之前先排序,然后用中序遍历递归输出. 注意输入的时候的技巧. .. # ...

  6. POJ-1785-Binary Search Heap Construction(笛卡尔树)

    Description Read the statement of problem G for the definitions concerning trees. In the following w ...

  7. sdut2355Binary Search Heap Construction

    链接 捣鼓了一下午..按堆建树 写完交 返回TLE..数据不大 感觉不会超了 无奈拿了数据来看什么奇葩数据会超 发现数据跟我输出不一样 看了好久才明白理解错题意了 给出的字符串有两个标签 按前一个来建 ...

  8. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  9. [LeetCode] questions conclusion_ Binary Search

    Binary Search T(n) = T(n/2) + O(1)   =>    T(n) = O(lg n) proof: 如果能用iterable , 就用while loop, 可以防 ...

随机推荐

  1. JAX-WS(三)构建简单webservice部署到tomcat上

    前言: 虽然构建本地的jax-ws的webservice很简单,但要部署到tomcat上要绕过点弯. tomcat本身和jdk都没有jaw-ws的API,所以部署的时候需要额外做点事情,有两种选择 1 ...

  2. 基于DES算法加密的防撞库密码系统项目总结

    项目内容:基于DES算法加密的防撞库密码系统 小组名:zqhzkzkj 目标:1.对用户输入的8位字符进行DES加密,要求用户输入8位密钥 2.对于不同的网站,不同的用户名生成不同的密码 小组成员:周 ...

  3. Xcode 遇到 App Transport Security has blocked a cleartext HTTP 错误

    今天用Xcode  创建新项目用到 URL 发送请求时,报下面的错: “App Transport Security has blocked a cleartext HTTP (http://) re ...

  4. fighting_使用CSS美化文字

    CSS3颜色渐变 background-image:linear-gradient(black,blue,green,red); 默认从上到下显示. 示例代码: <!DOCTYPE html&g ...

  5. Python学习总结2:raw_input() 与 input()

    参考http://www.cnblogs.com/way_testlife/archive/2011/03/29/1999283.html 1. 输入数据要求 raw_input() 直接读取控制台的 ...

  6. Ejb: remote调用

    用的是wildfly 9.0.2 一:在myeclipse中新建wildfly 9.0.2的server(如何新建去网上搜) 二:修改wildfly server的argument(在run conf ...

  7. UML:时序图

    时序图是用来描述对象的状态(或某数值)随时间变化而变化的图,一般软件开发中很少会用到. 灯有开和关两种状态,随着时间的推移,期间有人去开或者关这个灯,用时序图表示如下: 注意:蓝色和红色圈圈.黄色底色 ...

  8. 夺命雷公狗—angularjs—3—表单验证的高级用法

    其实我们的angularjs都是是块状代码,其实是可以在实际开发中保存下来以后就可以达到重复利用的目的了.. 废话不多说,直接上代码: <!doctype html> <html l ...

  9. Delphi中SQL批量插入记录

    http://www.cnblogs.com/azhqiang/p/4050331.html 在进行数据库操作时, 我们经常会遇到批量向数据库中写入记录的情况. 在这里我提供3种操作方式:   1.  ...

  10. css在IE和Firefox下的兼容性

    1.div的垂直居中问题 vertical-align:middle,将行距增加到和整个div高度一样,加line-height:200px;然后插入文字就垂直居中了.缺点是要控制内容不要换行. 2. ...