poj1785 Binary Search Heap Construction
此题可以先排序再用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的更多相关文章
- [POJ1785]Binary Search Heap Construction(笛卡尔树)
Code #include <cstdio> #include <algorithm> #include <cstring> #define N 500010 us ...
- 笛卡尔树 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(裸笛卡尔树的构造)
笛卡尔树: 每个节点有2个关键字key.value.从key的角度看,这是一颗二叉搜索树,每个节点的左子树的key都比它小,右子树都比它大:从value的角度看,这是一个堆. 题意:以字符串为关键字k ...
- POJ 1785 Binary Search Heap Construction (线段树)
题目大意: 给出的东西要求建立一个堆,使得后面的数字满足堆的性质.并且字符串满足搜索序 思路分析: 用线段树的最大询问建树.在建树之前先排序,然后用中序遍历递归输出. 注意输入的时候的技巧. .. # ...
- POJ-1785-Binary Search Heap Construction(笛卡尔树)
Description Read the statement of problem G for the definitions concerning trees. In the following w ...
- sdut2355Binary Search Heap Construction
链接 捣鼓了一下午..按堆建树 写完交 返回TLE..数据不大 感觉不会超了 无奈拿了数据来看什么奇葩数据会超 发现数据跟我输出不一样 看了好久才明白理解错题意了 给出的字符串有两个标签 按前一个来建 ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- [LeetCode] questions conclusion_ Binary Search
Binary Search T(n) = T(n/2) + O(1) => T(n) = O(lg n) proof: 如果能用iterable , 就用while loop, 可以防 ...
随机推荐
- TCP 状态图网摘
from unkonwn 1.CLOSED:起始点,在超时或者连接关闭时候进入此状态. 2.LISTEN:svr端在等待连接过来时候的状态,svr端为此要调用socket, bind,listen函数 ...
- wampserver下修改mysql root用户的登录密码
1.安装好wamp后,运行WampServer程序,进入MYSQL控制台; 2.进入控制台后,提示输入密码(不用输入任何密码,因为密码为空),按回车键进入; 3.输入“USE mysql;”然后回车, ...
- J2EE MyBatis使用
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis .20 ...
- ssh两台机器建立信任关系无密码登陆
在建立信任关系之前先看看基于公钥.私钥的加密和认证. 私钥签名过程 消息-->[私钥]-->签名-->[公钥]-->认证 私钥数字签名,公钥验证 Alice生成公钥和私钥,并将 ...
- Sublime 不自动打开上次未关闭的文件 设置方法
{ "font_size": 17, "hot_exit": false, "remember_open_files": false, &q ...
- RESTful架构详解(转)
1. 什么是REST REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移. 它首次出现在2000年Roy Fielding的 ...
- Ini文件操作函数
/// <summary> /// Copies a string into the specified section of an initialization file. /// &l ...
- php-引号中出现$
当双引号中包含变量时,变量对应的值会与双引号中的内容连接在一起: 当单引号中包含变量时,变量会被当做字符串输出. 慕课网,I love you!慕课网,$love
- CDC spyglass
SoC中会有着几百的clock domains,millions的async data crossing. Glitch等cdc问题是netlist level simulation的主要目的. CD ...
- linux下的网络配置
临时生效的命令: 设置静态ip: ip addr add 192.168.1.2/24 dev eth0 设置网关: ip route add default via 192.168.1.1 启动某个 ...