笛卡尔树 POJ ——1785 Binary Search Heap Construction
来源:互联网 发布:印度南部 知乎 编辑:程序博客网 时间:2024/05/16 05:22
对应POJ 题目:点击打开链接
Binary Search Heap Construction
Time Limit: 2000MS Memory Limit: 30000KTotal 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.
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/17 a/1 b/2 c/3 d/4 e/5 f/6 g/77 a/3 b/6 c/4 d/7 e/2 f/5 g/10
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 0struct 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;}
0 0
- 笛卡尔树 POJ ——1785 Binary Search Heap Construction
- poj 1785 Binary Search Heap Construction(笛卡尔树)
- POJ-1785-Binary Search Heap Construction(笛卡尔树)
- poj-1785 Binary Search Heap Construction(笛卡尔树)
- POJ 1785 Binary Search Heap Construction 笛卡尔树
- POJ1785 Binary Search Heap Construction【笛卡尔树】
- ZOJ 2243 & POJ 1785 Binary Search Heap Construction 笛卡尔树 || 单调栈
- POJ 1785 Binary Search Heap Construction 【笛卡尔树构造,线段树RMQ(Range Max/Min Query)】
- poj1785&zoj2243 Binary Search Heap Construction(笛卡尔树)
- zoj 2243 Binary Search Heap Construction(笛卡尔树)
- POJ 1785 Binary Search Heap Construction
- POJ 1785 Binary Search Heap Construction(RMQ)
- POJ 1785 Binary Search Heap Construction [treap]
- POJ 1785 : Binary Search Heap Construction
- POJ 1785 Binary Search Heap Construction 笔记
- POJ 1785 Binary Search Heap Construction (线段树)
- poj 1785 Binary Search Heap Construction( 分治 + RMQ )
- SDUT 1232 / POJ 1785 Binary Search Heap Construction
- 鸣人和佐助
- 使用highcharts插件时图表无法显示且jQuery报错:Uncaught ReferenceError: $ is not defined
- 读书笔记17:桥接模式
- 充实的每一天
- 九度oj 1442
- 笛卡尔树 POJ ——1785 Binary Search Heap Construction
- 打印最优路径
- android studio ADB无响应
- 读书笔记18:命令模式
- 配置SharePoint搜索服务
- SQL基础总结——20150730
- Oracle中的COALESCE,NVL,NVL2,NULLIF函数
- linux下安装mysql-5.6.25.tar.gz
- iOS开发常用软件和好用的第三方类库