先上题目:

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*/

最新文章

  1. BPM公文管理解决方案分享
  2. Reconstruct Itinerary
  3. javascript基础语法——表达式
  4. MMORPG大型游戏设计与开发(客户端架构 part9 of vegine)
  5. 初识 MySQL 5.6 新功能、参数
  6. 深入学习系列--Data Structure--02字符串
  7. paper 11:matlab中fix函数,floor函数,ceil函数,round函数的区分
  8. Android开发之UI的编程方式创建
  9. C#函数式程序设计之泛型(下)
  10. bzoj 4173 打表???
  11. C# Winform 中DataGridView 实现单元格输入下拉框功能
  12. [mock open]PyUnit执行单元测试时使用字符串模拟文件对象
  13. 家庭版Windows10没有远程桌面的问题
  14. 洗礼灵魂,修炼python(91)-- 知识拾遗篇 —— pymysql模块之python操作mysql增删改查
  15. MySql 在cmd下的学习笔记 —— 有关储存过程的操作(procedure)
  16. 【刷题】BZOJ 4289 PA2012 Tax
  17. 5: EL 表达式小结
  18. Oracle 12c Windows安装、介绍及简单使用(图文)
  19. JS-cookie封装
  20. N-gram语言模型与马尔科夫假设关系(转)

热门文章

  1. MSP430:串口输出
  2. Django - 自定义请求头
  3. 【洛谷3648/BZOJ3675】[APIO2014]序列分割(斜率优化DP)
  4. [转]linux之ps命令
  5. Python之IPython开发实践
  6. html5——多媒体(一)
  7. java攻城狮之路--复习JDBC
  8. Java多线程学习笔记(三)——Future和FutureTask
  9. smtplib.SMTPDataError: (554, b&#39;DT:SPM 126 smtp
  10. Java中的接口详解