先上题目:

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的更多相关文章

  1. 笛卡尔树 POJ ——1785 Binary Search Heap Construction

    相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS   Memory Limit: 30000K Total Subm ...

  2. poj1785 Binary Search Heap Construction

    此题可以先排序再用rmq递归解决. 当然可以用treap. http://poj.org/problem?id=1785 #include <cstdio> #include <cs ...

  3. POJ 1785 Binary Search Heap Construction(裸笛卡尔树的构造)

    笛卡尔树: 每个节点有2个关键字key.value.从key的角度看,这是一颗二叉搜索树,每个节点的左子树的key都比它小,右子树都比它大:从value的角度看,这是一个堆. 题意:以字符串为关键字k ...

  4. [POJ1785]Binary Search Heap Construction(笛卡尔树)

    Code #include <cstdio> #include <algorithm> #include <cstring> #define N 500010 us ...

  5. POJ 1785 Binary Search Heap Construction (线段树)

    题目大意: 给出的东西要求建立一个堆,使得后面的数字满足堆的性质.并且字符串满足搜索序 思路分析: 用线段树的最大询问建树.在建树之前先排序,然后用中序遍历递归输出. 注意输入的时候的技巧. .. # ...

  6. POJ-1785-Binary Search Heap Construction(笛卡尔树)

    Description Read the statement of problem G for the definitions concerning trees. In the following w ...

  7. sdut2355Binary Search Heap Construction

    链接 捣鼓了一下午..按堆建树 写完交 返回TLE..数据不大 感觉不会超了 无奈拿了数据来看什么奇葩数据会超 发现数据跟我输出不一样 看了好久才明白理解错题意了 给出的字符串有两个标签 按前一个来建 ...

  8. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  9. [LeetCode] questions conclusion_ Binary Search

    Binary Search T(n) = T(n/2) + O(1)   =>    T(n) = O(lg n) proof: 如果能用iterable , 就用while loop, 可以防 ...

随机推荐

  1. 搭建Git服务器(转载)

    转自:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137583770360 ...

  2. [Swift通天遁地]六、智能布局-(8)布局框架的使用:多分辨率适配和横竖屏布局

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  3. 浅析SpringDataJpa继承结构

    一.SpringDataJpa的含义: SpringDataJpa: 是Spring基于ORM框架.JPA规范封装的一套JPA应用框架,是SpringData中的一个子模块,可让开发者用极简的代码即可 ...

  4. strupr函数

    2019-06-03 15:13:39 strupr()函数! strupr,函数的一种,将字符串s转换为大写形式. 说明:只转换s中出现的小写字母,不改变其它字符.返回指向s的指针. 兼容性说明:s ...

  5. C#中Random

    说明:C#中的随机数是一个伪随机数,随机数字从一组有限的数字选择以相同的概率,所选的数字不是完全随机的,因为使用数学算法来选择它们.在大多数Windows系统中,Random的15毫秒内创建的对象很可 ...

  6. ACM_统计字符串

    统计字符串 Time Limit: 2000/1000ms (Java/Others) Problem Description: 给定n个字符串,统计字符串的个数. 如给定 5 sss ab sss ...

  7. form表单中图片也可以当作提交按钮

    点击图片按钮时,不光提交其他输入框信息,还把鼠标点击图片上的x,y坐标也传输过去了

  8. Spring Cloud (11) Hystrix-监控聚合监控

    上一篇利用Hystrix Dashboard去监控断路器的Hystrix command,当我们有很多服务的时候,就需要聚合所有服务的Hystrix Dashboard数据了,这就需要Hystrix ...

  9. JS——math

    random() 方法可返回介于 0 ~ 1 之间的一个随机数. Math.random() 0.0 ~ 1.0 之间的一个伪随机数,但是不包括0和1.

  10. CSS——继承性

    继承性发生的前提是包含(嵌套关系). 1.文字颜色可以继承 2.文字大小可以继承 3.字体可以继续 4.字体粗细可以继承 5.文字风格可以继承 6.行高可以继承 总结:文字的所有属性都可以继承. 特殊 ...