Billboard
hdu2795:http://acm.hdu.edu.cn/showproblem.php?pid=2795
题意:给一个h*w的公告牌,h是高度,w是宽度,一个单位高度1为一行,然后会有一些公告贴上去,公告是1*wi大小的长纸条,优先贴在最上面并且最左边的位置,如果没有空间贴得下,就输出-1,可以的话,就输出所贴的位置(第几行)。
题解:用线段树来维护。把高度看成每一个节点,即每一行看成线段树的一个节点,而w看成底层节点的值,然后每个节点维护区间的最大值。由于h会达到10的9次方,但是只有200000的海报。随意当h大于200000时候,只需建立n==200000的树。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int h,w,post,num,temp;//记录高度,长度,海报的数量,
struct Node{
int left;
int right;
int maxn;//区间的最大值
int id;//底层的点的序号,用来标记行数
}node[*];
void build(int l,int r,int idx){
node[idx].left=l;
node[idx].right=r;
node[idx].id=;
if(l==r){
node[idx].maxn=w;
node[idx].id=num++;//逐步记录
return;
}
int mid=(l+r)/;
build(l,mid,idx<<);
build(mid+,r,idx<<|);
node[idx].maxn=max(node[idx<<].maxn,node[idx<<|].maxn);//pushup上去
}
void update(int value,int idx){
if(node[idx].left==node[idx].right){//如果到达底层的点
if(node[idx].maxn>=value){//如果满足要求,则更新节点的值
node[idx].maxn-=value;
printf("%d\n",node[idx].id);//输出行号
}
return;
}
if(node[idx<<].maxn>=value)update(value,idx<<);//如果左儿子的manx满足要求,则优先考虑左边
else if(node[idx<<|].maxn>=value)update(value,idx<<|);//如果左边不满足,右边满足则进入右儿子
else {//否则则没有空间,输出-1,并且返回
printf("-1\n");
return ;
}
node[idx].maxn=max(node[idx<<].maxn,node[idx<<|].maxn);//更新父节点的maxn
}
int main(){
while(~scanf("%d%d%d",&h,&w,&post)){//多组数据
num=;//初始化
if(h<=)build(,h,);
else build(,,);
for(int i=;i<=post;i++){
scanf("%d",&temp);
update(temp,);
}
}
}
Billboard的更多相关文章
- HDU 2795 Billboard(区间求最大值的位置update的操作在query里做了)
Billboard 通过这题,我知道了要活用线段树的思想,而不是拘泥于形式, 就比如这题 显然更新和查询放在一起很简单 但如果分开写 那么我觉得难度会大大增加 [题目链接]Billboard [题目类 ...
- 三维场景中使用BillBoard技术
三维场景中对于渲染效果不是很精致的物体可以使用BillBoard技术实现,使用该技术需要将物体实时朝向摄像机,即计算billboard的旋转矩阵M. 首先根据摄像机位置cameraPos和billBo ...
- HDU 2795 Billboard(线段树)
题目链接: 传送门 Billboard Time Limit: 2000MS Memory Limit: 32768 K Description At the entrance to the ...
- ACM: Billboard 解题报告-线段树
Billboard Time Limit:8000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descript ...
- 【hdu2795】Billboard
Problem Description At the entrance to the university, there is a huge rectangular billboard of size ...
- Billboard(线段树)
Billboard Time Limit:8000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- HDU 2795 Billboard(线段树的另类应用)
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU-------(2795)Billboard(线段树区间更新)
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- UVALive 3953 Strange Billboard (状态压缩+枚举)
Strange Billboard 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/A Description The marke ...
- HDU 1882 Strange Billboard(位运算)
题目链接 题意 : 给你一个矩阵,有黑有白,翻转一个块可以让上下左右都翻转过来,问最少翻转多少次能让矩阵变为全白. 思路 : 我们从第一行开始枚举要翻转的状态,最多可以枚举到2的16次方,因为你只要第 ...
随机推荐
- shell 获取网关 以及修改ip 启用网卡
shell 获取网关 以及修改ip 启用网卡 #!/bin/bash #autho freefei #script is a init computer eth #data 2014 10 09 19 ...
- Bash算术计算
1:$(( )) 2:$[ ] 3:`expr $x + $y` 4:bc命令 #!/bin/bash x= y= echo $(( x + y )) echo $[ $x + $y ] echo ` ...
- NUll在oracle与sqlserver中使用相同与区别
最近在使用Oracle进行开发,遇到很多与以前使用sqlserver的不同语法.今天遇到null在两种数据库上面操作上的差别,在此记录两种数据库上的差异. null 与字符串相加 1.在oracle中 ...
- widows sever2003 PHP环境搭建
此文仅为文字笔记,非原创,摘阅自互联网 1.安装IIS6.0及Framework 2.0 2.安装fastcgi http://www.iis.net/download/FastCGI 安装fas ...
- angularjs-ngTable select filter
jsp <td title="'Status'" filter="{status: 'select'}" filter-data="fn.sta ...
- &与&
- 自己动手写一个iOS 网络请求库的三部曲[转]
代码示例:https://github.com/johnlui/Swift-On-iOS/blob/master/BuildYourHTTPRequestLibrary 开源项目:Pitaya,适合大 ...
- [转]一个备份MySQL数据库的简单Shell脚本
本文翻译自 iSystemAdmin 的 <A Simple Shell Script to Backup MySQL Database> Shell脚本是我们写不同类型命令的一种脚本,这 ...
- mac 连接linux
1.ssh 通过ssh 的方式直接连接linux ssh name@ip -22 例:ssh zyc@192.168.1.100 -22 这个的前提是linux 要开启ssh 服务 先看一下linu ...
- sublime text snippet代码片断
$0 代表补全代码后放的位置 0 的权重是最低的 $1 最高 也等于${1:} ${1: name} 1输入点的序号(1权重最高) name 自动补全的默认值 <conte ...