POJ 1785 Binary Search Heap Construction (线段树)
题目大意:
给出的东西要求建立一个堆,使得后面的数字满足堆的性质。并且字符串满足搜索序
思路分析:
用线段树的最大询问建树。在建树之前先排序,然后用中序遍历递归输出。
注意输入的时候的技巧。
。。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define lson num<<1,s,mid
#define rson num<<1|1,mid+1,e
#define maxn 55555 using namespace std; struct node
{
int data;
char str[50];
bool operator < (const node &cmp)const
{
//return data<cmp.data;
return strcmp(str,cmp.str)<0;
}
}save[maxn];
int res[maxn<<2];
int pos[maxn<<2];
int n; void pushup(int num)
{
res[num]=max(res[num<<1],res[num<<1|1]);
if(res[num<<1]>res[num<<1|1])pos[num]=pos[num<<1];
else pos[num]=pos[num<<1|1];
}
void build(int num,int s,int e)
{
if(s==e)
{
res[num]=save[s].data;
pos[num]=s;
return;
}
int mid=(s+e)>>1;
build(lson);
build(rson);
pushup(num);
}
int query(int num,int s,int e,int l,int r)
{
if(l<=s && r>=e)
{
return pos[num];
}
int mid=(s+e)>>1;
if(r<=mid)return query(lson,l,r);
else if(l>mid)return query(rson,l,r);
else
{
int lmost=query(lson,l,mid);
int rmost=query(rson,mid+1,r);
if(save[lmost].data<=save[rmost].data)return rmost;
else return lmost;
}
}
void print(int l,int r)
{
if(l>r)return;
if(l==r)
{
printf("(%s/%d)",save[l].str,save[l].data);
return;
}
int mid=query(1,1,n,l,r);
printf("(");
print(l,mid-1);
printf("%s/%d",save[mid].str,save[mid].data);
print(mid+1,r);
printf(")");
}
int main()
{
while(scanf("%d",&n)!=EOF && n)
{
for(int i=1;i<=n;i++)
{
scanf(" %[a-z]/%d",save[i].str,&save[i].data);
// printf("%s%d",save[i].str,save[i].data);
}
sort(save+1,save+1+n);
build(1,1,n);
print(1,n);
puts("");
}
return 0;
}
POJ 1785 Binary Search Heap Construction (线段树)的更多相关文章
- 笛卡尔树 POJ ——1785 Binary Search Heap Construction
相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS Memory Limit: 30000K Total Subm ...
- POJ 1785 Binary Search Heap Construction(裸笛卡尔树的构造)
笛卡尔树: 每个节点有2个关键字key.value.从key的角度看,这是一颗二叉搜索树,每个节点的左子树的key都比它小,右子树都比它大:从value的角度看,这是一个堆. 题意:以字符串为关键字k ...
- ZOJ - 2243 - Binary Search Heap Construction
先上题目: Binary Search Heap Construction Time Limit: 5 Seconds Memory Limit: 32768 KB Read the sta ...
- [POJ1785]Binary Search Heap Construction(笛卡尔树)
Code #include <cstdio> #include <algorithm> #include <cstring> #define N 500010 us ...
- poj1785 Binary Search Heap Construction
此题可以先排序再用rmq递归解决. 当然可以用treap. http://poj.org/problem?id=1785 #include <cstdio> #include <cs ...
- 【POJ 2777】 Count Color(线段树区间更新与查询)
[POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4094 ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- 【POJ 2750】 Potted Flower(线段树套dp)
[POJ 2750] Potted Flower(线段树套dp) Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4566 ...
- POJ-1785-Binary Search Heap Construction(笛卡尔树)
Description Read the statement of problem G for the definitions concerning trees. In the following w ...
随机推荐
- hdu 3714 Error Curves(三分)
Error Curves Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- nyoj 1282 部分和问题
部分和问题(入门题) 时间限制:1000 ms | 内存限制:65535 KB 难度:0 描述 给你n个数(a1,a2,a3.......an) ,是否存在某一些数字加起来等于k,有就输出 & ...
- mysql-Innodb事务隔离级别-repeatable read详解
http://blog.csdn.net/dong976209075/article/details/8802778 经验总结: Python使用MySQLdb数据库后,如使用多线程,每个线程创建一个 ...
- 编写可移植的PHP代码
1. 保持配置集中放置. 作为一个通用的准则,建议将大多数信息保存在一个位置(可能是一个文件中),这样在需要修改信息时,就能在同一个位置进行所有必要的修改. 2. 编写可重用的代码: 如果刚刚结束了其 ...
- jquery 实践操作:load()方法
最近决定总结下实际项目中的 JS 相关的一些操作,因此开启此系列,记录使用过程中用到的一些实用操作问题和解决方法,给自己一份记录. jquery load方法是对jQuery.ajax()进行封装以方 ...
- mongoDB权威指南学习笔记
//mongoDB第1-3章节添加,修改,修改器的笔记: //备注:和MySQL查询一样,时刻想着优化查询数据的时间和性能 //db.help() //数据库帮助信息 //db.blog.help() ...
- about loops in assembly code
总结: 实际上只有一种结构,都是 do-while 结构
- Html.AntiForgeryToken 防止伪造提交
原文发布时间为:2011-05-03 -- 来源于本人的百度文章 [由搬家工具导入] In this tutorial, I am not going to discuss the concept i ...
- Java中的内存机制及管理
1. Java根据虚拟机以及平台的版本不同而在内存中开辟不同大小的内存,通常不会关注这个大小. 2. 程序中的对象存储在内存的堆(heap)中 3. 程序中的方法和局部变量存储在内存的栈(Stack) ...
- 调用Outlook发送邮件
#region 查找与指定文件关联在一起的程序的文件名 /// <summary> /// 查找与指定文件关联在一起的程序的文件名 /// </summary> /// < ...