您的位置:首页 > 其它

ZOJ - 2243 - Binary Search Heap Construction

2014-08-11 21:03 489 查看
先上题目:

Binary Search Heap ConstructionTime 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>
#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 1;
return 0;
}
}node;

int n;
node e[MAX];
char ss[MAX];
int dp[MAX][20];

void solve(){
int i,j,l,r;
for(i=0;i<n;i++) dp[i][0]=i;
for(j=1;(1<<j)<=n;j++){
for(i=0;i+(1<<j)-1<n;i++){
l=dp[i][j-1];   r=dp[i+(1<<(j-1))][j-1];
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-(1<<k)+1][k]].p ? dp[a][k] : dp[b-(1<<k)+1][k]);
}

void print(int r,int L,int R){
int ne;
putchar('(');
if(r-L>0){
ne=rmq(L,r-1);
print(ne,L,r-1);
}
for(unsigned int i=0;i<e[r].l.size();i++) putchar(e[r].l[i]);
printf("/%lld",e[r].p);
if(R-r>0){
ne=rmq(r+1,R);
print(ne,r+1,R);
}
putchar(')');
}

int main()
{
int li;
char* st;
//freopen("data.txt","r",stdin);
while(scanf("%d",&n),n!=0){
for(int i=0;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=0;
for(int i=1;i<n;i++){
if(e[li].p<e[i].p) li=i;
}
print(li,0,n-1);
printf("\n");
}
return 0;
}


/*RMQ*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: