Billboard HDU 2795 (线段树)
Problem Description
At the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and w is its width). The board is the place where all possible announcements are posted: nearest programming competitions, changes in the dining room menu, and other important information.
On September 1, the billboard was empty. One by one, the announcements started being put on the billboard.
Each announcement is a stripe of paper of unit height. More specifically, the i-th announcement is a rectangle of size 1 * wi.
When someone puts a new announcement on the billboard, she would always choose the topmost possible position for the announcement. Among all possible topmost positions she would always choose the leftmost one.
If there is no valid location for a new announcement, it is not put on the billboard (that's why some programming contests have no participants from this university).
Given the sizes of the billboard and the announcements, your task is to find the numbers of rows in which the announcements are placed.
Input
There are multiple cases (no more than 40 cases).
The first line of the input file contains three integer numbers, h, w, and n (1 <= h,w <= 10^9; 1 <= n <= 200,000) - the dimensions of the billboard and the number of announcements.
Each of the next n lines contains an integer number wi (1 <= wi <= 10^9) - the width of i-th announcement.
Output
For each announcement (in the order they are given in the input file) output one number - the number of the row in which this announcement is placed. Rows are numbered from 1 to h, starting with the top row. If an announcement can't be put on the billboard, output "-1" for this announcement.
Sample Input
3 5 5
2
4
3
3
3
Sample Output
1
2
1
3
-1
题意:
有h×w大的公告板。有n条公告要写入,每条公告高度都是1,宽度是wi,每次从最上最左的空位写,假设有空位输出第几行。假设没有足够空位输出-1。
分析:
用高度来构建线段树,记录left行到right行中的最大留白空间,然后每贴一个广告,就在线段树中插,如果左子树的空位长度大于当前要贴的广告长度,就先查找左子树,(因为题目要求贴最左上方),当找到贴的位置,则该行的新的剩余长度就在原值上减去当前长度。并更新整棵线段树。
代码:
#include <iostream>
#include <stdio.h>
#define lchild left,mid,root<<1
#define rchild mid+1,right,root<<1|1
using namespace std;
const int maxn=1e8;
int Max[maxn<<2]; ///用来存放某些行区间所能存放的最大值
int height,width,n;
///构建线段树
void push_up(int root)
{
Max[root] = max(Max[root<<1],Max[root<<1|1]);
}
void build(int left,int right,int root)
{
Max[root] = width; ///初始,每个区间所剩余的最大长度都为width
if(left == right) return;
int mid = (left+right)>>1;
///递归构建左右子树
build(lchild);
build(rchild);
}
///查询并插入
int query(int w,int left,int right,int root)
{
if(left == right) ///则将该板插入到这一行,这一节点所剩余的宽度减去w
{
Max[root] -= w;
return left; ///返回木板所插入的行
}
int mid = (left+right)>>1;
int ans=0;
if(w<=Max[root<<1]) ///如果广告的宽度小于左区间的最大宽度,题目要求往左区间靠
ans = query(w,lchild);
else
ans = query(w,rchild);
push_up(root);
return ans;
}
int main()
{
while(~scanf("%d%d%d",&height,&width,&n))
{
if(n<height) ///所要贴的广告数小于高度,则线段树按n建,否则按height建立
{
height = n;
}
build(1,height,1); ///构建线段树
int w;
while(n--)
{
scanf("%d",&w);
if(Max[1]<w)
printf("-1\n");
else
{
int ans = query(w,1,height,1);
printf("%d\n",ans);
}
}
}
return 0;
}
Billboard HDU 2795 (线段树)的更多相关文章
- hdu 2795 线段树(二维问题一维化)
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 2795 线段树单点更新
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 2795 线段树区间最大值,单点更新+二分
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 2795 线段树(纵向)
注意h的范围和n的范围,纵向建立线段树 题意:h*w的木板,放进一些1*L的物品,求每次放空间能容纳且最上边的位子思路:每次找到最大值的位子,然后减去L线段树功能:query:区间求最大值的位子(直接 ...
- HDU 2795 (线段树 单点更新) Billboard
h*w的木板,放进一些1*L的物品,求每次放空间能容纳且最上边的位子. 每次找能放纸条而且是最上面的位置,询问完以后可以同时更新,所以可以把update和query写在同一个函数里. #include ...
- hdu 2795 线段树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 #include <cstdio> #include <cmath> # ...
- Billboard HDU - 2795(树状数组,单点修改,区间查询)
题目链接:https://vjudge.net/problem/HDU-2795 思路:h = 1e9行不通,因为广告是1*w的,所以n个广告最多只需要 h = n的高度,那么h=2e5就可以接受了. ...
- hdu 2795线段树
#include<stdio.h> #define N 200005 int h,w,n; struct node { int x,y,max; }a]; int mmax(int e,i ...
- Billboard (HDU 2795)
Billboard (HDU 2795) Hdu 2795 注意到每个广告的长度是1,因此可以将每这一张广告牌当成一个数列表示,每个初始值为w.使用线段树维护这个数列,每次查询为找到这个数列第一个大于 ...
- hdu 5877 线段树(2016 ACM/ICPC Asia Regional Dalian Online)
Weak Pair Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
随机推荐
- linux上使用J-Link调试S3C2440裸机代码
linux上使用J-Link调试S3C2440裸机代码 工具: segger的jlink仿真器 segger的jlink for linux 交叉编译工具链里面的arm-xx-linux-xx-gdb ...
- DAY1敏捷冲刺
站立式会议 工作安排 (1)服务器配置 (2)数据库建表 (3)页面初步样式设计 (4)主要页面之间的交互 燃尽图 代码提交记录 感想 林一心:后端云服务器的配置确实是一个挑战,目前还在摸索中 赵意: ...
- MindManager2018 修改过期时间 配置文件路径
路径:C:\Users\likui\AppData\Roaming\MindManager\MindManager2018.ini 文件中记录了安装时间和最后一次启动时间. [MindManager] ...
- C/S结构
C/S结构 编辑 同义词 C/S架构一般指C/S结构 C/S 结构,即大家熟知的客户机和服务器结构.它是软件系统体系结构,通过它可以充分利用两端硬件环境的优势,将任务合理分配到Client端和Serv ...
- 使用bat执行java项目
前提:java项目要有main方法 类似写法如下: set JAVA_HOME=C:\jdk1.6 set LIB_HOME=. set JAVA_JAR=. set JAVA_JAR=%JAVA_J ...
- bzoj4332[JSOI2012]分零食
一下午被这题的精度续掉了...首先可以找出一个多项式的等比数列的形式,然后类似poj的Matrix Series,不断倍增就可以了.用复数点值表示进行多次的多项式运算会刷刷地炸精度...应当用int存 ...
- [BZOJ4036] [HAOI2015]按位或
传送门:https://lydsy.com/JudgeOnline/problem.php?id=4036 Description 刚开始你有一个数字0,每一秒钟你会随机选择一个[0,2^n-1]的数 ...
- webpack散记
1. manifest manifest存储了webpack的chunk相关的信息.具体为一个对象,或者包含runtime的一段代码.其中包含着一个chunkId,已经对应chunkId的相关信息,例 ...
- 【DP】【P1586】四方定理
传送门 Description Input 第一行为一个整数T代表数据组数,之后T行每行一个数n代表要被分解的数 Output 对于每个n输出一行,为方案个数 Sample Input Sample ...
- selenium - webdriver - ActionChains类(鼠标操作)
ActionChains 类提供了鼠标操作的常用方法: perform(): 执行所有 ActionChains 中存储的行为: context_click(): 右击: double_click() ...