POJ 1785 Binary Search Heap Construction
时间:2014-02-27 19:51:46
收藏:0
阅读:496
Binary Search Heap Construction
| Time Limit: 2000MS | Memory Limit: 30000K | |
| Total Submissions: 8383 | Accepted: 2418 |
Description
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.
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
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/pn denoting 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
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)))
Source
看到这题后学的treap,没想到tle,了解到因为事先分配了优先级,复杂度可能退化到O(n*n),然后又学的笛卡尔树,区别在于笛卡尔树(只关注右链)优先级已经事先分配好、
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#define N 50010
using namespace std;
struct num
{
char label[20];
int val;
}a[N];
struct Num
{
int ch[2];
int val;
char label[20];
}b[N];
char s1[20];
int n,stack[N],op[N];
int cmp(const void *p1,const void *p2)
{
struct num * pt1 = (struct num *)p1;
struct num * pt2 = (struct num *)p2;
if(strcmp(pt1->label,pt2->label)>0)
{
return 1;
}else
{
return -1;
}
}
int main()
{
//freopen("data.txt","r",stdin);
int insert();
void find(int k);
while(scanf("%d",&n)!=EOF)
{
if(n==0)
{
break;
}
for(int i=1;i<=n;i++)
{
scanf("%s",s1);
int Top=0;
int l = strlen(s1);
int pos;
for(int j=0;j<=l-1;j++)
{
if(s1[j]==‘/‘)
{
pos = j;
a[i].label[Top]=‘\0‘;
break;
}
a[i].label[Top++] = s1[j];
}
int sum = 0;
for(int j=pos+1;j<=l-1;j++)
{
sum = sum*10+s1[j]-‘0‘;
}
a[i].val = sum;
}
qsort(a+1,n,sizeof(a[0]),cmp);
for(int i=1;i<=n;i++)
{
b[i].ch[0] = b[i].ch[1] = 0;
b[i].val = a[i].val;
strcpy(b[i].label,a[i].label);
}
int head= insert();
find(head);
printf("\n");
}
return 0;
}
int insert()
{
int Top=-1;
for(int i=1;i<=n;i++)
{
int k = Top;
while(k!=-1&&a[stack[k]].val<a[i].val)
{
k--;
}
if(k!=-1)
{
op[i] = stack[k];
b[stack[k]].ch[1] = i;
}
if(k!=Top)
{
op[stack[k+1]] = i;
b[i].ch[0] = stack[k+1];
}
Top = k;
stack[++Top] = i;
}
return stack[0];
}
void find(int k)
{
if(k==0)
{
return ;
}
printf("(");
find(b[k].ch[0]);
printf("%s/%d",b[k].label,b[k].val);
find(b[k].ch[1]);
printf(")");
}
POJ 1785 Binary Search Heap Construction,布布扣,bubuko.com
原文:http://blog.csdn.net/yongxingao/article/details/19995915
评论(0)