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, 可以防 ...
随机推荐
- leetcode-5 最长回文子串(动态规划)
题目要求: * 给定字符串,求解最长回文子串 * 字符串最长为1000 * 存在独一无二的最长回文字符串 求解思路: * 回文字符串的子串也是回文,比如P[i,j](表示以i开始以j结束的子串)是回文 ...
- C++Primer 第十七章
//1.当我们希望将一些数据组合成单一对象,但又不想麻烦地定义一个新的数据结构来表示这些数据的时候,tuple非常有用.其和其伴随类型和函数都定义在头文件tuple中,声明在命名空间std中. tup ...
- PostgreSQL 同步复制(1master+2standby)
OS: Red Hat Enterprise Linux Server release 6.5 (Santiago) PostgreSQL: postgresql-9.4.5.tar.bz2 mast ...
- web 前端:连接mysql中文乱码问题的解决办法
当使用tomcat部署servlet连接时,总是出现各种原因的中文乱码问题.有的是因为mysql的默认字符集的问题,有的是客户端的字符配置问题. 现在对于这两种问题进行配置,来解决乱码. 服务器端(m ...
- [转] FastJson---高性能JSON开发包
原文地址: FastJson---高性能JSON开发包 Fastjson介绍 Fastjson是一个Java语言编写的JSON处理器,由阿里巴巴公司开发.1.遵循http://json.org标准,为 ...
- Ruby操作VBA的注意事项和技巧(1):乱码、获取VBA活动和非活动窗口的名称与路径、文件路径的智能拼接与截取(写入日期)
1.VBA编辑器复制粘贴出来的代码乱码 解决方法:切换到中文输入模式再复制出来就行了 2.获取VBA活动和非活动窗口的名称与路径 Dim wbpath, filename As String ...
- PHP XDEBUG
PHP调试时,不得不提XDEBUG这个调试利器.学习PHP以来,几乎所有的问题我都利用它来解决. 首先关于如何安装,不在赘述,请自行google之.(需要特别注意的是:PHP5.2 和5.3 ,关于加 ...
- logstash中的redis插件
redis作为logstash中的官方broker,既有input插件,还有output插件. redis input插件 data_type属性: 有三种类型, list -> BLPOP - ...
- EBS常用小常识(转)
值集: 1.编辑信息:取上一个值集所选的数据.(值集关联) WHERE BANK_ACCOUNT_ID = :$FLEX$.CE_BANK_ACCOUNT_NUM_NAME ORDER BY STAT ...
- spring的事务回滚
@Transactional(rollbackFor = { Exception.class }) 需要把异常抛出到带有@Transactional(rollbackFor = { Exception ...