Binary Search Heap Construction
题目链接
Time Limit: 2000MS
Memory Limit: 30000K
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.
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)))
思路:
这是一道笛卡尔树板子题,题目的主干思路是根据给定的字符序列构建一棵笛卡尔树,之后按照中序遍历的顺序输出。
关于笛卡尔树,这里补充说明一下。
笛卡尔树的性质:
1.笛卡尔树具有二叉堆的性质,以此题为例,本题是一个大根堆,父亲结点的权值是不小于两个子节点的。
2.具有二叉排序树的性质,即左子树的关键字(key)比根的关键字(key)小。但右子树的key比根的key大。
3.如果要查找 i 结点和j 结点之间的最大值,就是查找i和j的lca(最近公共祖先)即可。
AC代码:
/*笛卡尔树板子题*/
//poj:1785
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
const int maxn = 50010;
#define INF 0x3f3f3f
struct node
{
string str;
int pre; //模拟单调栈
int info;
int l, r;
void init() {
l = r = pre = 0;
}
bool operator < (const node b)const {
return str < b.str;
}
}tre[maxn];
void Init(int n) {
int i;
for (i = 0; i <= n; ++i)
tre[i].init();
tre[0].info = INF;
}
void build(int n) { //构建笛卡尔树
int i, j;
for (i = 1; i <= n; ++i) {
j = i - 1;
while (tre[j].info < tre[i].info)
j = tre[j].pre; //找到第一个不小于自己的结点,其余的弹出单调栈
tre[i].l = tre[j].r; //让该节点的右子树做自己的左子树
tre[j].r = i; //自己做该节点的右子树
tre[i].pre = j; //记录比比自己大的结点
}
}
void print(int root)
{
cout << "(";
if (tre[root].l)
print(tre[root].l);
cout << tre[root].str << "/" << tre[root].info;
if (tre[root].r)
print(tre[root].r);
cout << ")";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n, i;
while (cin >> n, n) {
Init(n);
for (i = 1; i <= n; ++i) {
char ch = cin.get();
getline(cin, tre[i].str, '/');
cin >> tre[i].info;
}
sort(tre + 1, tre + 1 + n);
build(n);
print(tre[0].r);
cout << endl;
}
return 0;
}
/*
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
*/