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, 可以防 ...
随机推荐
- 用回调函数创建一个XMLHttpRequest,并从一个TXT文件中检索数据。
<script> var xmlhttp; function loadXMLDoc(url,soyo) { if (window.XMLHttpRequest) {// IE7+, Fir ...
- E20170919-hm
infinity n. <数>无穷大; 无限的时间或空间;
- IOS上微信在输入框弹出键盘后,页面不恢复,下方有留白,有弹窗弹出时页面内容感应区域错位
问题说明: ios中,键盘的弹起,页面会往上挪动,使输入框展示在页面中间,键盘隐藏页面会下挪恢复原状. 在微信移动端,ios页面不恢复,下方有留白. 收起键盘的瞬间,如果有弹窗弹出,此时时页面内容应区 ...
- strupr函数
2019-06-03 15:13:39 strupr()函数! strupr,函数的一种,将字符串s转换为大写形式. 说明:只转换s中出现的小写字母,不改变其它字符.返回指向s的指针. 兼容性说明:s ...
- Objective-C copy(转)
一.从面向对象到Objective-C概览copy 1.面向对象: In object-oriented programming, object copying is creating a copy ...
- 对于民科吧s5_or吧友自增树的复杂度计算
原帖 自增树如s5_or所说,是一种思想像Splay的数据结构,每个节点维护一个堆权值,每当询问一个节点时,堆权值++,并返回时维护堆权值为堆的性质.这个树从旋转次数上比Splay小是肯定的,因为Sp ...
- ACM_“IP地址”普及(进制转换)
“IP地址”普及 Time Limit: 2000/1000ms (Java/Others) Problem Description: 大家都知道最近广财大校园网提速,现在就跟大家普及一下简单的互联网 ...
- 5CSS之字体font-family
---------------------------------------------------------------------------------------------------- ...
- 关于华为手机Log.d打印不出来log的问题
http://blog.csdn.net/picasso_l/article/details/52489560 拨号,进入后台设置,进行操作.
- 在ubuntu系统下下载和卸载skype
1.下载 sudo apt-get install skypeforlinux 2.卸载 sudo apt remove skypeforlinux