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, 可以防 ...
随机推荐
- spring AutowireCapableBeanFactory 自动注入
文档:http://docs.spring.io/spring/docs/3.0.x/javadoc-api/org/springframework/beans/factory/config/Auto ...
- SQL top查询
select *from emp;
- SQL Server 身份验证 登陆
当遇到错误 检查SQL是否启动 SQL Server (MSSQLSERVER)在 打开 SQL Server 配置管理器 SQL Server (MSSQLSERVER) 鼠标右键->启动 再 ...
- java-JDBC-Oracle数据库连接
java-JDBC连接oracle数据库,StateMent和PreparedStatement对比(查询query) 1. PreparedStatement接口继承Statement, Prepa ...
- 【Origin】jquery.barddialog.js
/// <reference path="jquery-2.1.1.min.js" /> /** * @license jquery.bardDialog 1.0.0 ...
- .net 中 ref out params的区别
C#中有三个关键字-ref,out ,params,虽然本人不喜欢这三个关键字,因为它们疑似破坏面向对象特性.但是既然m$把融入在c#体系中,那么我们就来认识一下参数修饰符ref,out ,param ...
- tostring() 作用
tostring() 作用 -->显示类中属性的值 -->不想显示该类的内存地址
- php CI框架目录结构及运行机制
CI目录结构 CI主要组成部分为,application(应用文件夹).system(系统文件夹)和index.php入口文件. 应用文件夹中主要是存放控制器.模型和视图等,系统文件夹中主 ...
- Codeforces Round #325 (Div. 1) D. Lizard Era: Beginning
折半搜索,先搜索一半的数字,记录第一个人的值,第二个人.第三个人和第一个人的差值,开个map哈希存一下,然后另一半搜完直接根据差值查找前一半的答案. 代码 #include<cstdio> ...
- sql ROUND() 函数三个参数的含义
ROUND的格式:ROUND(p1,p2,p3),其作用是取四舍四入值P1:要被四舍五入的数字P2:保留的小数位数P3:如果为0或不输入,则表示进P1进入四舍五入,如ROUND(123.86,1) = ...