题目大意:

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

思路分析:

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

注意输入的时候的技巧。

。。

#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. win7删除一个空白文件夹总是显示:“找不到该项目,该项目不在E盘中,请确认该项目的位置,重试”的解决办法

    把下面的代码复制粘贴到一新建的txt记事本文档中,并另存为del.bat文件(或者你喜欢的名字),注意扩展名为批处理文件bat:           DEL /F /A /Q \\?\%1 RD /S ...

  2. 实现段落文字两端对齐的css样式

    有时候网站中的文字比较多,虽然为父元素设置了宽度,但是总是会出现两端参差不齐的情况,看起来不整齐.其实实现段落的两端对齐,只需要设置两个css 样式即可. .demo{ text-align: jus ...

  3. iOS不用官方SDK实现微信和支付宝支付XHPayKit

    作者:朱晓辉Allen 链接:https://juejin.im/post/5a90dd3a6fb9a0634912b755 前言 前段时间由于项目需求,移除了项目中的微信支付SDK和支付宝支付SDK ...

  4. mysql source、mysqldump 导入导出数据(转)

    解决了mysql gbk编码的导入导出问题,感谢作者. 一.导入数据 1.确定 数据库默认编码,比如编码 为gbk,将读入途径编码同样设为gbk,命令为:           set names gb ...

  5. sql2008百万级数据排除重复信息

    --高性能排除重复select userid from table where userid in ( select userid from ( select userid, row_number() ...

  6. favicon还是这个网站生成的比较正确

    原文发布时间为:2011-11-16 -- 来源于本人的百度文章 [由搬家工具导入] http://tools.dynamicdrive.com/favicon/ http://www.rw-desi ...

  7. java网络编程学习笔记(三):ServerSocket详解

    1.ServerSocket的构造方法 ServerSocket(); ServerSocket(int port); ServerSocket(int port,int backlog); Serv ...

  8. 【字符集及字符编码】UTF-8、UTF-16和UTF-32

    UTF-32 用 4 个字节存储每一个字符,以保证能把 UCS 完全表达出来.但实际上 UCS 的字符数量根本不需要用 32 位表示,UTF-32 极大地浪费了空间.另外,由于组合字符的存在,定长表示 ...

  9. alloc_chrdev_region申请一个动态主设备号,并申请一系列次设备号

    ret = alloc_chrdev_region(&ndev, 0, 1, "chr_dev"); //分配设备号 alloc_chrdev_region申请一个动态主设 ...

  10. c# Thread类

    现在C#已经建议摈弃使用 Suspend, Resume 暂停/恢复线程, 也尽量少用 Abort方法中断一个线程. 建议使用线程的同步手段有:  Mutex.ManualResetEvent.Aut ...