ZOJ 2243 Binary Search Heap Construction笛卡尔树(二叉搜索+堆)
来源:互联网 发布:访客网络应该限速多少 编辑:程序博客网 时间:2024/05/21 19:28
Binary Search Heap Construction
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 Specification
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/pndenoting 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 Specification
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)))
题意:每一个节点有两个属性,“标号-优先级”,相对于标号是二叉搜索树,相对与优先级是大根堆 然后中根遍历
分析: 对标号进行从小到大排序,此时已是中根遍历了,然后在构造堆,插入的节点如果比他的父节点优先级小就放在右儿子位置。如果大的话就往上找直到找打比他大的那个节点记作x,此时x原来的右儿子更新为该节点的左儿子(中序遍历左儿子在前),该节点成为x的新右儿子。
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;const int INF = 100000000;struct node{ int lson,rson,fa; int v; char s[100];};node tree[50010];int cmp(node x,node y){ return strcmp(x.s, y.s) < 0; //字符串比较用这个;换成return x.s < y.s 就WA了}void inset_node(int now){ int j = now - 1; while(tree[j].v < tree[now].v) j = tree[j].fa; tree[now].lson = tree[j].rson; tree[j].rson = now; tree[now].fa = j;}void travel(int now){ if(now == 0) return ; printf("("); travel(tree[now].lson); printf("%s/%d",tree[now].s,tree[now].v); travel(tree[now].rson); printf(")");}int main(){ int n; while(scanf("%d", &n) != EOF && n) { getchar(); for(int i = 1; i <= n; i++) { scanf("%[a-z]/%d",tree[i].s,&tree[i].v); getchar(); tree[i].lson = tree[i].rson = tree[i].fa = 0; } sort(tree + 1,tree + n + 1, cmp); tree[0].fa = tree[0].lson = tree[0].rson = 0; tree[0].lson = 0; tree[0].v = INF; for(int i = 1; i <= n; i++) inset_node(i); travel(tree[0].rson); printf("\n"); } return 0;}
- ZOJ 2243 Binary Search Heap Construction笛卡尔树(二叉搜索+堆)
- zoj 2243 Binary Search Heap Construction(笛卡尔树)
- ZOJ 2243 & POJ 1785 Binary Search Heap Construction 笛卡尔树 || 单调栈
- POJ1785 Binary Search Heap Construction【笛卡尔树】
- poj 1785 Binary Search Heap Construction(笛卡尔树)
- poj1785&zoj2243 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 笛卡尔树
- POJ 1785 Binary Search Heap Construction 【笛卡尔树构造,线段树RMQ(Range Max/Min Query)】
- 二叉堆(Binary heap)
- POJ 1785 Binary Search Heap Construction (线段树)
- POJ 1875 Binary Search Heap Construction(treap)
- POJ 1785 Binary Search Heap Construction
- POJ 1785 Binary Search Heap Construction(RMQ)
- POJ 1785 Binary Search Heap Construction [treap]
- POJ 1875 Binary Search Heap Construction
- FZU 2168
- 从头认识java-6.2 继承(Generalization)(2)
- Java多线程
- 【HttpClient4.5中文教程】【第一章 :基础】1.2 HttpClient接口
- 【Android成长之路】全局大喇叭——广播机制的浅谈(使用本地广播)
- ZOJ 2243 Binary Search Heap Construction笛卡尔树(二叉搜索+堆)
- 推荐使用string直接量来赋值
- android---(volley网络通信框架)
- 记录python数据持久存储的一点问题
- LeetCode Contains Duplicate II 数组
- java.util.logging.Logger使用详解
- 【HttpClient4.5中文教程】【第一章 :基础】1.3 HttpClient执行上下文
- Java HashMap的数据结构以及put和get方法
- 计算机系统的硬件结构之存储器