题目大意:

给出的东西要求建立一个堆,使得后面的数字满足堆的性质。并且字符串满足搜索序

思路分析:

用线段树的最大询问建树。在建树之前先排序,然后用中序遍历递归输出。

注意输入的时候的技巧。

。。

#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 (线段树)的更多相关文章

  1. 笛卡尔树 POJ ——1785 Binary Search Heap Construction

    相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS   Memory Limit: 30000K Total Subm ...

  2. POJ 1785 Binary Search Heap Construction(裸笛卡尔树的构造)

    笛卡尔树: 每个节点有2个关键字key.value.从key的角度看,这是一颗二叉搜索树,每个节点的左子树的key都比它小,右子树都比它大:从value的角度看,这是一个堆. 题意:以字符串为关键字k ...

  3. ZOJ - 2243 - Binary Search Heap Construction

    先上题目: Binary Search Heap Construction Time Limit: 5 Seconds      Memory Limit: 32768 KB Read the sta ...

  4. [POJ1785]Binary Search Heap Construction(笛卡尔树)

    Code #include <cstdio> #include <algorithm> #include <cstring> #define N 500010 us ...

  5. poj1785 Binary Search Heap Construction

    此题可以先排序再用rmq递归解决. 当然可以用treap. http://poj.org/problem?id=1785 #include <cstdio> #include <cs ...

  6. 【POJ 2777】 Count Color(线段树区间更新与查询)

    [POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4094 ...

  7. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  8. 【POJ 2750】 Potted Flower(线段树套dp)

    [POJ 2750] Potted Flower(线段树套dp) Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4566   ...

  9. POJ-1785-Binary Search Heap Construction(笛卡尔树)

    Description Read the statement of problem G for the definitions concerning trees. In the following w ...

随机推荐

  1. PAT1039

    小红想买些珠子做一串自己喜欢的珠串.卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖.于是小红要你帮忙判断一下,某串珠子里是否包含了全部自己想要的珠子?如果是,那么告诉她有多少多余的珠子 ...

  2. Java和C#中神奇的String

    Java String: String 类适用于描述字符串事物.该类是不可以被继承的.我们主要学习: 1字符串特性.字符串最大的特性:一旦被初始化就不可以被改变.重赋值只是改变了引用. 2字符串操作. ...

  3. 【bzoj1097】[POI2007]旅游景点atr 状压dp+堆优化Dijkstra

    题目描述 FGD想从成都去上海旅游.在旅途中他希望经过一些城市并在那里欣赏风景,品尝风味小吃或者做其他的有趣的事情.经过这些城市的顺序不是完全随意的,比如说FGD不希望在刚吃过一顿大餐之后立刻去下一个 ...

  4. 基于Linux的嵌入式文件系统构建与设计

    摘 要:Linux是当今一种十分流行的嵌入式操作系统.由于其具有执行效率高.占用空间小.实时性能优良和可扩展性强等特点,因此被广泛应用于工业控制领域.该文对其文件系统进行了简单的介绍,结合嵌入式系统应 ...

  5. tomcat 启动慢解决(/dev/random)

    JRE默认使用 /dev/random作为随机数来源,当熵池大小不够的时候,random会很慢,造成随机数生成调用阻塞. 解决方案: 改用 /dev/urandom (1) tomcat的启动选项增加 ...

  6. vue-element-admin开发模式下style标签热更新失效[解决办法]

    参考:https://forum.vuejs.org/t/vue-cli-3-x-style/46306/3 vue.config.js添加配置 css: { sourceMap: false, mo ...

  7. c#.net用JavaScript实现 时钟显示

    原文发布时间为:2008-10-10 -- 来源于本人的百度文章 [由搬家工具导入] 显示日期,星期,以及时间: JS文件夹与default.aspx在同一个文件夹下 JS文件夹中有date.js文件 ...

  8. configurationmanager.getsection usage example.

    1.app.config(note that attribute case sensitive!) <?xml version="1.0" encoding="ut ...

  9. duilib入门简明教程 -- XML配置界面(6) (转)

    原文转自:http://www.cnblogs.com/Alberl/p/3343699.html     前面那些教程都是为了让小伙伴们从win32.MFC过渡到duilib,让大家觉得duilib ...

  10. Scrapy学习-22-扩展开发

    开发scrapy扩展 定义 扩展框架提供一个机制,使得你能将自定义功能绑定到Scrapy. 扩展只是正常的类,它们在Scrapy启动时被实例化.初始化   注意 实际上自定义扩展和spider中间件. ...