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, 可以防 ...
随机推荐
- 连接sql server数据库的两种方式
class DB { private static SqlConnection conn; public static SqlConnection getConn() { //conn = n ...
- editPlus修改默认的文件编码
- linux 修改端口限制
1.显示当前临时端口的范围:一般情形下:linux临时端口号范围是(32768,61000) sysctl net.ipv4.ip_local_port_range 或 cat ...
- 封装page分页类
类: <?php //分页工具类 class Page{ /* * 获取分页字符串 * @param1 string $uri,分页要请求的脚本url ...
- demo02TextView
main.xml----- /layout/activity_main.xml <RelativeLayout xmlns:android="http://schemas.androi ...
- java将数组中的零放到末尾
package com.shb.java; /** * 将数组中的0放到数组的后边,然后原来的非零数的顺序不改变 * @author BIN * */ public class Demo2{ publ ...
- 10---Net基础加强
复习: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...
- springmvc+spring+mybatis分页查询实例版本2.0
先在改成纯利用js进行分页,首先查询出所有记录,初始化通过jquery控制只知显示首页内容,创建页面切换功能的函数,每次显示固定的内容行并把其他内容行隐藏,这样只需要一次提交就可以实现分页,但是仍有缺 ...
- RobotFrameWork接口报文测试-----(一)简单demo的实现
最近几个月的工作任务都是通过使用RF工具来搭建服务器端接口的自动化测试,使用python作为2次开发的语言,也是第一次去做这种项目,经验善浅,还是很有可能会走很多的弯路,为此,我希望自己能把每个阶段的 ...
- hadoop 启动停止命令
1 批量启动与停止 1.1 Start-all.sh # Start all hadoop daemons. Run this on master node. bin=`dirname ...