笛卡爾樹 POJ ——1785 Binary Search Heap Construction

對應POJ 題目:點擊打開鏈接

Binary Search Heap Construction
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 9075   Accepted: 2566

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)))

 

題意:

每次有n個輸入,每個輸入格式爲(字符串/數字),字符串(長度未知,反正我開100也能過)和數字都不會重複;要求建立一棵樹,使得中序遍歷按字符串字典序排序,而且數字符合大根堆。輸出格式爲((左子樹)根節點(右子樹))。

 

思路:

赤裸裸的Treap樹,可惜會TLE,可用笛卡爾樹順利AC。建樹時在右鏈從下往上找適合位置插入。讀入的時候有點技巧,%*[ ]表示忽略[]裏面的字符,%[^/]表示讀入字符串時遇到'/'就結束,沒有讀入'/'且會在字符串後面添加結束符。

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <time.h>
using namespace std;
#define N 50010
#define inf 0x7fffffff
#define nil 0

struct Node
{
	int pri, l, r, fa;
	char str[100];
};

bool cmp(Node n1, Node n2)
{
	return strcmp(n1.str, n2.str) < 0;
}

class CartesianTree
{
public:
	void Init(int n)
	{
		a[0].pri = inf;
		a[0].l = a[0].r = a[0].fa = nil;
		int i;
		for(i = 1; i <= n; i++){
			scanf("%*[ ]%[^/]/%d", a[i].str, &a[i].pri);
			a[i].l = a[i].r = a[i].fa = nil;
		}
		sort(a + 1, a + n + 1, cmp);
		for(i = 1; i <= n; i++)
			Insert(i);
	}
	void Insert(int p)
	{
		int t = p - 1; //從下往上找
		while(a[t].pri < a[p].pri) t = a[t].fa;
		a[p].l = a[t].r;
		a[t].r = p;
		a[p].fa = t;
	}
	void Show()
	{
		InOrder(a[0].r);
		printf("\n");
	}
	void InOrder(int t)
	{
		if(nil == t) return;
		printf("(");
		InOrder(a[t].l);
		printf("%s/%d", a[t].str, a[t].pri);
		InOrder(a[t].r);
		printf(")");
	}
private:
	Node a[N];
};

CartesianTree ct;

int main()
{
	//freopen("in.txt","r",stdin);
	int n;
	while(scanf("%d", &n), n)
	{
		ct.Init(n);
		ct.Show();
	}
	return 0;
}


 

 

 

 

 

 

 

 

 

發佈了165 篇原創文章 · 獲贊 42 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章