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 ...
随机推荐
- 使用 Python 获取 Linux 系统信息
探索platform模块 platform模块在标准库中,它有很多运行我们获得众多系统信息的函数.让我们运行Python解释器来探索它们中的一些函数,那就从platform.uname()函数开始吧: ...
- 【bzoj2127】happiness 最大流
happiness Time Limit: 51 Sec Memory Limit: 259 MBSubmit: 2579 Solved: 1245[Submit][Status][Discuss ...
- swarm 集群管理
1.创建服务 docker service create --replicas 1 --name hello busybox ping baiud.com 2.显示服务详细信息 3.扩展服务数量 4. ...
- mac 应用程序安装目录
java 安装目录 :/Library/Java/JavaVirtualMachines/jdk1.8.0_<more numbers>.jdk/Contents/Home maven 安 ...
- JavaScript 的新特性:类的 #private 字段
这是什么,如何使用,为什么需要? 一边听“Noise Pollution” —— Portugal. The Man,一边阅读本文简直就是享受 JavaScript 标准的第二阶段(Stage 2)加 ...
- Func<T1, T2, TResult> Delegate 系统Func委托类型
原文发布时间为:2011-03-25 -- 来源于本人的百度文章 [由搬家工具导入] http://msdn.microsoft.com/en-us/library/bb534647%28v=VS.1 ...
- sql查询字段值只为汉字(桃)
SELECT * FROM roster WHERE roster.`name` >'zzzzzzzzzz' //查询roster表中name值为中文的 SELECT * FROM rost ...
- [LeetCode] Remove Nth Node From End of List 快慢指针
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- 【nginx】nginx的介绍
Nginx 反向代理初印象 Nginx (“engine x”) 是一个高性能的HTTP和反向代理 服务器,也是一个IMAP/POP3/SMTP服务器.其特点是占有内存少,并发能力强,事实上nginx ...
- Windows消息钩取
@author: dlive @date: 2016/12/19 0x01 SetWindowsHookEx() HHOOK SetWindowsHookEx( int idHook, //hook ...