-
描述
-
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.
输入
- 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. 输出
- 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. 样例输入
-
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
样例输出
-
(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)))
来源
- Ulm Local 2004
我先是手写treap,常规插入,结果超时了…….
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
char label[100], temp;
int n, pri;
int a[30010];
struct node
{
node *son[2];
char val[100];
int rand_key;
};
void rotate(node *&root,int judge) // ˜µƒ–˝◊™
{
node *k = root->son[judge^1];
root->son[judge^1] = k->son[judge];
k->son[judge] = root;
root = k;
}
void insert(node *&root,char val[100],int pri) // ˜µƒ≤»Î
{
if(root == NULL)
{
root = new node;
root->son[0] = NULL;
root->son[1] = NULL;
root->rand_key = pri;
strcpy(root->val,val);
//root->val = val;
}
else
{
if(strcmp(val,root->val) > 0) //µ›πÈ≤È’“”“◊” ˜
{
insert(root->son[1],val,pri);
if(root->son[1]->rand_key > root->rand_key) //–Œ≥…◊Ó¥Û÷µ∂—
rotate(root,0);
}
else
{
insert(root->son[0],val,pri);
if(root->son[0]->rand_key > root->rand_key)
rotate(root,1);
}
}
}
//(< left sub-treap >< label >/< priority >< right sub-treap >).
void print(node *root)
{
if(root == NULL)
return;
printf("(");
print(root->son[0]);
printf("%s/%d",root->val,root->rand_key);
print(root->son[1]);
printf(")");
}
int main()
{
while(scanf("%d",&n) && n)
{
node *root = NULL;
while(n--)
{
scanf(" %[a-z]/%d", label, &pri);//标签,/,优先级
insert(root,label,pri);
}
print(root);
printf("\n");
}
return 0;
}
然后参考了网上代码,写了一个treap,居然不超时
首先将第一关键字(搜索序的关键字)排序(笛卡尔树中序遍历结果是按照第一关键字升序的),然后逐个向树中插入元素,这时只需要考虑后插入的元素的优先级即可。
a、优先级比上一个节点优先级高:将这个节点调整为根节点。
b、优先级比上一个节点优先级低:将这个节点设为当前节点的右儿子
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct node
{
int val, lson, rson, fa;
char s[100];
}a[50005];
int n;
bool cmp(node x, node y)
{
return strcmp(x.s, y.s) < 0;
}
void Insert(int now)
{
int j = now - 1;
while (a[j].val < a[now].val)
j = a[j].fa;
a[now].lson = a[j].rson;
a[j].rson = now;
a[now].fa = j;
}
void Traval(int now)
{
if (now == 0)
return;
printf("(");
Traval(a[now].lson);
printf("%s/%d", a[now].s, a[now].val);
Traval(a[now].rson);
printf(")");
}
int main() {
while (scanf("%d",&n) && n)
{
for (int i=1; i<=n; i++)
{
scanf(" %[a-z]/%d", a[i].s, &a[i].val);
a[i].lson = a[i].rson = a[i].fa = 0;
}
sort(a+1, a+n+1, cmp);
a[0].val = 0xfffff;
a[0].lson = a[0].rson = a[0].fa = 0;
for (int i=1; i<=n; i++)
Insert(i);
Traval(a[0].rson);
printf("\n");
}
return 0;
}
然后看到网上还有用线段树做的,转化成RMQ问题.
依然是现按第一关键字排序,然后就是区间询问最大值,这个最大值就是整个树的根,然后分别对左右两个区间进行查询.
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct node
{
char s[100];
int val;
} a[50005];
int n, v[50005*4];
void build(int l, int r, int rt)
{
if (l == r)
{
v[rt] = l;
return ;
}
int mid = (l + r)/2;
build(l, mid, rt*2);
build(mid+1, r, rt*2+1);
v[rt] = (a[v[rt*2]].val > a[v[rt*2+1]].val) ? v[rt*2] : v[rt*2+1];
}
int query(int L, int R, int l, int r, int rt)
{
if (L <= l && r <= R)
return v[rt];
int ret1, ret2, mid = (l + r)/2;
ret1 = ret2 = 0;
if (L <= mid)
ret1 = query(L, R, l, mid, rt*2);
if (mid < R)
ret2 = query(L, R, mid+1, r, rt*2+1);
if (ret1 == 0)
return ret2;
if (ret2 == 0)
return ret1;
return (a[ret1].val > a[ret2].val) ? ret1 : ret2;
}
void print(int l, int r)
{
if (l > r)
return ;
if (l == r)
{
printf("(%s/%d)", a[l].s, a[l].val);
return ;
}
int m = query(l, r, 1, n, 1);
printf("(");
print(l, m-1);
printf("%s/%d", a[m].s, a[m].val);
print(m+1, r);
printf(")");
}
bool cmp(node x, node y)
{
return strcmp(x.s, y.s) < 0;
}
int main()
{
while (scanf("%d", &n) && n)
{
for (int i=1; i<=n; i++)
scanf(" %[a-z]/%d", a[i].s, &a[i].val);
sort(a+1, a+n+1, cmp);
build(1, n, 1);
print(1, n);
printf("\n");
}
return 0;
}