ZOJ - 2243 - Binary Search Heap Construction
先上题目:
Binary Search Heap Construction
Time Limit: 5 Seconds Memory Limit: 32768 KB
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/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))) 题意:好像就是叫你求Treap树。给出字符串和优先值,要求建一棵二叉树,根据字符串排序,然后父亲的优先值要比儿子大。然后先序遍历输出这个Treap树。
最水的方法就是直接先按优先值排序,然后逐个逐个元素添加。但是这样做绝对超时。
可以通过的第一种方法:首先先按字符串大小排个序,然后从小到大扫描一次,求出每个元素左边优先值比它大的最近的元素的位置在哪。同理从大到小扫描,求出每个元素右边优先值比它大的最近的元素的位置在哪。(没错,就是单调栈),然后在每个扫描到的最近位置加一个对应的括号(左括号或者右括号)就是答案了。总的时间复杂度O(nlogn)。
第二种方法是用RMQ求出区间优先值的最大值的下标,然后每次找出区间最大值作为根构造两边的子树就可以了。总的时间复杂度也是O(nlogn)。 比赛的时候用的方法是第二种,但是当时求对数的时候底数不是2,所以提交一直都是段错误。 上代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAX 50002
#define INF 0x3f3f3f3f
using namespace std; typedef struct node{
char l[];
int p; bool operator < (const node& o)const{
return strcmp(l,o.l)<;
}
}node; int n;
node e[MAX];
char ch[MAX];
int l[MAX],r[MAX];
int al[MAX],ar[MAX]; inline void put(char c,int ti){
for(int i=;i<ti;i++) putchar(c);
} int main()
{
char* sp;
//freopen("data.txt","r",stdin);
while(scanf("%d",&n),n){
for(int i=;i<=n;i++){
scanf("%s",ch);
sp=strchr(ch,'/');
*sp='\0';
sp++;
strcpy(e[i].l,ch);
sscanf(sp,"%d",&e[i].p);
l[i]=r[i]=i;
al[i]=ar[i]=;
}
sort(e+,e+n+);
e[].p=e[n+].p=INF;
for(int i=;i<=n;i++){
while(e[i].p>=e[l[i]-].p) l[i]=l[l[i]-];
al[l[i]]++;
}
for(int i=n;i>;i--){
while(e[i].p>=e[r[i]+].p) r[i]=r[r[i]+];
ar[r[i]]++;
}
for(int i=;i<=n;i++){
put('(',al[i]);
printf("%s/%d",e[i].l,e[i].p);
put(')',ar[i]);
}
printf("\n");
}
return ;
}
/*单调栈*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#define MAX 60002
#define ll long long
using namespace std; typedef struct node{
string l;
ll p; bool operator <(const node& o)const{
if(l<o.l) return ;
return ;
}
}node; int n;
node e[MAX];
char ss[MAX];
int dp[MAX][]; void solve(){
int i,j,l,r;
for(i=;i<n;i++) dp[i][]=i;
for(j=;(<<j)<=n;j++){
for(i=;i+(<<j)-<n;i++){
l=dp[i][j-]; r=dp[i+(<<(j-))][j-];
if(e[l].p>e[r].p) dp[i][j]=l;
else dp[i][j]=r;
}
}
} int rmq(int a,int b){
int k;
k=log(b-a+1.0)/log(2.0);
return (e[dp[a][k]].p>e[dp[b-(<<k)+][k]].p ? dp[a][k] : dp[b-(<<k)+][k]);
} void print(int r,int L,int R){
int ne;
putchar('(');
if(r-L>){
ne=rmq(L,r-);
print(ne,L,r-);
}
for(unsigned int i=;i<e[r].l.size();i++) putchar(e[r].l[i]);
printf("/%lld",e[r].p);
if(R-r>){
ne=rmq(r+,R);
print(ne,r+,R);
}
putchar(')');
} int main()
{
int li;
char* st;
//freopen("data.txt","r",stdin);
while(scanf("%d",&n),n!=){
for(int i=;i<n;i++){
getchar();
scanf("%s",ss);
st=strchr(ss,'/');
*st='\0';
st++;
e[i].l=string(ss);
sscanf(st,"%lld",&e[i].p);
}
sort(e,e+n);
solve();
li=;
for(int i=;i<n;i++){
if(e[li].p<e[i].p) li=i;
}
print(li,,n-);
printf("\n");
}
return ;
}
/*RMQ*/
ZOJ - 2243 - Binary Search Heap Construction的更多相关文章
- 笛卡尔树 POJ ——1785 Binary Search Heap Construction
相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS Memory Limit: 30000K Total Subm ...
- poj1785 Binary Search Heap Construction
此题可以先排序再用rmq递归解决. 当然可以用treap. http://poj.org/problem?id=1785 #include <cstdio> #include <cs ...
- POJ 1785 Binary Search Heap Construction(裸笛卡尔树的构造)
笛卡尔树: 每个节点有2个关键字key.value.从key的角度看,这是一颗二叉搜索树,每个节点的左子树的key都比它小,右子树都比它大:从value的角度看,这是一个堆. 题意:以字符串为关键字k ...
- [POJ1785]Binary Search Heap Construction(笛卡尔树)
Code #include <cstdio> #include <algorithm> #include <cstring> #define N 500010 us ...
- POJ 1785 Binary Search Heap Construction (线段树)
题目大意: 给出的东西要求建立一个堆,使得后面的数字满足堆的性质.并且字符串满足搜索序 思路分析: 用线段树的最大询问建树.在建树之前先排序,然后用中序遍历递归输出. 注意输入的时候的技巧. .. # ...
- POJ-1785-Binary Search Heap Construction(笛卡尔树)
Description Read the statement of problem G for the definitions concerning trees. In the following w ...
- sdut2355Binary Search Heap Construction
链接 捣鼓了一下午..按堆建树 写完交 返回TLE..数据不大 感觉不会超了 无奈拿了数据来看什么奇葩数据会超 发现数据跟我输出不一样 看了好久才明白理解错题意了 给出的字符串有两个标签 按前一个来建 ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- [LeetCode] questions conclusion_ Binary Search
Binary Search T(n) = T(n/2) + O(1) => T(n) = O(lg n) proof: 如果能用iterable , 就用while loop, 可以防 ...
随机推荐
- telnet端口问题
今天测试发现telnet 一个端口不通,开始还以为是服务开放这个端口有问题,后来才发现这个端口是udp的.而telnet下层走的tcp协议,自然无法测试那些tcp的端口. 而之前下意识里还总以为都可以 ...
- Flink之Stateful Operators
Implementing Stateful Functions source function的stateful看官网,要加lock Declaring Keyed State at the Runt ...
- P2924 [USACO08DEC]大栅栏Largest Fence
传送门 反正我是看不出来这题和凸包有什么关系--大佬们是怎么想到的-- 不准确一点的说,本题就是要我们求一个边上点数最多的凸包 我们可以先把所有的边都取出来,然后按极角排序.枚举这个凸包的起点,然后做 ...
- Java线程之Synchronized用法
synchronized是Java中的关键字,是一种同步锁.它修饰的对象有以下几种: 修饰一个代码块,被修饰的代码块称为同步语句块,其作用的范围是大括号{}括起来的代码,作用的对象是调用这个代码块的对 ...
- Golang 入门 : goroutine(协程)
在操作系统中,执行体是个抽象的概念.与之对应的实体有进程.线程以及协程(coroutine).协程也叫轻量级的线程,与传统的进程和线程相比,协程的最大特点是 "轻"!可以轻松创建上 ...
- javascript中for...in和for...of的区别
for...of循环是ES6引入的新的语法. for...in遍历拿到的x是键(下标).而for...of遍历拿到的x是值,但在对象中会提示不是一个迭代器报错.例子如下: let x; let a = ...
- DropDownList 数据源绑定和获取
前台代码: <td>账户名称:</td> <td> <asp:DropDownList ID="DropDownListAccount" ...
- 如何解决error LNK2001(转载)
转自:http://www.cnblogs.com/myzhijie/articles/1658545.html 解决外部符号错误:_main,_WinMain@16,__beginthreadex ...
- 升级Xcode或 MacOS编译iOS出现resource fork, Finder information, or similar detritus not allowed
很久没有在网上留下足迹了,冒个泡吧 最近升级了Xcode,编译之前的一个项目是出现问题,问题结尾如下: resource fork, Finder information, or similar de ...
- win32之bitmap篇
先讲一下LoadBitmap的用法,代码如下: PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd,&ps); HDC hMemDC = CreateCompa ...