ZOJ - 2243 - Binary Search Heap Construction
先上题目:
Binary Search Heap Construction
Time Limit: 5 Seconds Memory Limit: 32768 KB
Read the statement of problem G for the definitions concerning trees. In the following we define the basic terminology of heaps. A heap is a tree whose internal nodes have each assigned a priority (a number) such that the priority of each internal node is less than the priority of its parent. As a consequence, the root has the greatest priority in the tree, which is one of the reasons why heaps can be used for the implementation of priority queues and for sorting.
A binary tree in which each internal node has both a label and a priority, and which is both a binary search tree with respect to the labels and a heap with respect to the priorities, is called a treap. Your task is, given a set of label-priority-pairs, with unique labels and unique priorities, to construct a treap containing this data.
Input Specification
The input contains several test cases. Every test case starts with an integer n. You may assume that 1<=n<=50000. Then follow n pairs of strings and numbers l1/p1,...,ln/pndenoting the label and priority of each node. The strings are non-empty and composed of lower-case letters, and the numbers are non-negative integers. The last test case is followed by a zero.
Output Specification
For each test case output on a single line a treap that contains the specified nodes. A treap is printed as (<left sub-treap><label>/<priority><right sub-treap>). The sub-treaps are printed recursively, and omitted if leafs.
Sample Input
7 a/7 b/6 c/5 d/4 e/3 f/2 g/1
7 a/1 b/2 c/3 d/4 e/5 f/6 g/7
7 a/3 b/6 c/4 d/7 e/2 f/5 g/1
0
Sample Output
(a/7(b/6(c/5(d/4(e/3(f/2(g/1)))))))
(((((((a/1)b/2)c/3)d/4)e/5)f/6)g/7)
(((a/3)b/6(c/4))d/7((e/2)f/5(g/1))) 题意:好像就是叫你求Treap树。给出字符串和优先值,要求建一棵二叉树,根据字符串排序,然后父亲的优先值要比儿子大。然后先序遍历输出这个Treap树。
最水的方法就是直接先按优先值排序,然后逐个逐个元素添加。但是这样做绝对超时。
可以通过的第一种方法:首先先按字符串大小排个序,然后从小到大扫描一次,求出每个元素左边优先值比它大的最近的元素的位置在哪。同理从大到小扫描,求出每个元素右边优先值比它大的最近的元素的位置在哪。(没错,就是单调栈),然后在每个扫描到的最近位置加一个对应的括号(左括号或者右括号)就是答案了。总的时间复杂度O(nlogn)。
第二种方法是用RMQ求出区间优先值的最大值的下标,然后每次找出区间最大值作为根构造两边的子树就可以了。总的时间复杂度也是O(nlogn)。 比赛的时候用的方法是第二种,但是当时求对数的时候底数不是2,所以提交一直都是段错误。 上代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAX 50002
#define INF 0x3f3f3f3f
using namespace std; typedef struct node{
char l[];
int p; bool operator < (const node& o)const{
return strcmp(l,o.l)<;
}
}node; int n;
node e[MAX];
char ch[MAX];
int l[MAX],r[MAX];
int al[MAX],ar[MAX]; inline void put(char c,int ti){
for(int i=;i<ti;i++) putchar(c);
} int main()
{
char* sp;
//freopen("data.txt","r",stdin);
while(scanf("%d",&n),n){
for(int i=;i<=n;i++){
scanf("%s",ch);
sp=strchr(ch,'/');
*sp='\0';
sp++;
strcpy(e[i].l,ch);
sscanf(sp,"%d",&e[i].p);
l[i]=r[i]=i;
al[i]=ar[i]=;
}
sort(e+,e+n+);
e[].p=e[n+].p=INF;
for(int i=;i<=n;i++){
while(e[i].p>=e[l[i]-].p) l[i]=l[l[i]-];
al[l[i]]++;
}
for(int i=n;i>;i--){
while(e[i].p>=e[r[i]+].p) r[i]=r[r[i]+];
ar[r[i]]++;
}
for(int i=;i<=n;i++){
put('(',al[i]);
printf("%s/%d",e[i].l,e[i].p);
put(')',ar[i]);
}
printf("\n");
}
return ;
}
/*单调栈*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#define MAX 60002
#define ll long long
using namespace std; typedef struct node{
string l;
ll p; bool operator <(const node& o)const{
if(l<o.l) return ;
return ;
}
}node; int n;
node e[MAX];
char ss[MAX];
int dp[MAX][]; void solve(){
int i,j,l,r;
for(i=;i<n;i++) dp[i][]=i;
for(j=;(<<j)<=n;j++){
for(i=;i+(<<j)-<n;i++){
l=dp[i][j-]; r=dp[i+(<<(j-))][j-];
if(e[l].p>e[r].p) dp[i][j]=l;
else dp[i][j]=r;
}
}
} int rmq(int a,int b){
int k;
k=log(b-a+1.0)/log(2.0);
return (e[dp[a][k]].p>e[dp[b-(<<k)+][k]].p ? dp[a][k] : dp[b-(<<k)+][k]);
} void print(int r,int L,int R){
int ne;
putchar('(');
if(r-L>){
ne=rmq(L,r-);
print(ne,L,r-);
}
for(unsigned int i=;i<e[r].l.size();i++) putchar(e[r].l[i]);
printf("/%lld",e[r].p);
if(R-r>){
ne=rmq(r+,R);
print(ne,r+,R);
}
putchar(')');
} int main()
{
int li;
char* st;
//freopen("data.txt","r",stdin);
while(scanf("%d",&n),n!=){
for(int i=;i<n;i++){
getchar();
scanf("%s",ss);
st=strchr(ss,'/');
*st='\0';
st++;
e[i].l=string(ss);
sscanf(st,"%lld",&e[i].p);
}
sort(e,e+n);
solve();
li=;
for(int i=;i<n;i++){
if(e[li].p<e[i].p) li=i;
}
print(li,,n-);
printf("\n");
}
return ;
}
/*RMQ*/
ZOJ - 2243 - Binary Search Heap Construction的更多相关文章
- 笛卡尔树 POJ ——1785 Binary Search Heap Construction
相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS Memory Limit: 30000K Total Subm ...
- poj1785 Binary Search Heap Construction
此题可以先排序再用rmq递归解决. 当然可以用treap. http://poj.org/problem?id=1785 #include <cstdio> #include <cs ...
- POJ 1785 Binary Search Heap Construction(裸笛卡尔树的构造)
笛卡尔树: 每个节点有2个关键字key.value.从key的角度看,这是一颗二叉搜索树,每个节点的左子树的key都比它小,右子树都比它大:从value的角度看,这是一个堆. 题意:以字符串为关键字k ...
- [POJ1785]Binary Search Heap Construction(笛卡尔树)
Code #include <cstdio> #include <algorithm> #include <cstring> #define N 500010 us ...
- 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, 可以防 ...
随机推荐
- 71.Ext.form.ComboBox 完整属性
转自:https://blog.csdn.net/taotaoqi/article/details/7409514 Ext.form.ComboBox 类全称: Ext.form.ComboBox 继 ...
- Kaka's Matrix Travels
http://poj.org/problem?id=3422 #include <stdio.h> #include <algorithm> #include <stri ...
- 一个不错的jquery插件模版
pageplugin.js (function ($) { $.PagePlugin = function (obj, opt) { var options = $.extend({}, $.Page ...
- 洛谷P1330 封锁阳光大学(二分图染色)
P1330 封锁阳光大学 题目描述 曹是一只爱刷街的老曹,暑假期间,他每天都欢快地在阳光大学的校园里刷街.河蟹看到欢快的曹,感到不爽.河蟹决定封锁阳光大学,不让曹刷街. 阳光大学的校园是一张由N个点构 ...
- 关于 node.js 小插曲
随着web2.0的时代到来,javascript在前端担任了更多的职责,事件也看得到了广泛的应用,node不像rhino那样受java的影响很大,而是将前端浏览器中应用广泛企鹅成熟的事件引入后端,配合 ...
- [Swift通天遁地]八、媒体与动画-(7)实现多个动画的顺序播放效果
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Appium + python - weixin公众号操作
from appium import webdriverfrom time import sleep desired_caps = { "platformName":"A ...
- 如何写出网页高性能的DOM来提升网页的加载速度
为什么要写高性能DOM? 一个网站,在页面上承载最多内容的就是DOM,而且无论是我们通过加载JS.加载图片,他们也是通过写HTML标签来实现的.而我们性能优化要做的无非就是几大块: 站点的网络消耗 D ...
- 使用Boost.Python构建混合系统(译)
目录 Building Hybrid Systems with Boost.Python 摘要(Abstract) 介绍(Introduction) 设计目标 (Boost.Python Design ...
- Coursera公开课-Machine_learing:编程作业4
编程作业: Neural Network Learning 源码上传到gitlab. 对于神经网络的理解也都在源码注释里面了,感兴趣可以看看.