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 ...
随机推荐
- Debian常用設置
1. 更新軟件源 sudo cp /etc/apt/sources.list /etc/apt/sources.list_bak #備份 sudo vi /etc/apt/sources.list / ...
- js设计模式之代理模式以及订阅发布模式
为啥将两种模式放在一起呢?因为这样文章比较长啊. 写博客的目的我觉得首要目的是整理自己的知识点,进而优化个人所得知识体系.知识成为个人的知识,就在于能够用自己的话表达同一种意义. 本文是设计模式系列文 ...
- LR脚本编写时的几个小技巧
参数化空值 如上图所示,当参数化时某个值需要为空值(非空格),直接在参数化文件中空一行/格即可,虽然Parameter List界面上没有显示空的那一行,但并不影响取值. 手工日志跟踪 lr_set_ ...
- 【Linux】- CentOS 7 安装.NET Core 2.1
添加dotnet产品Feed 在安装.NET Core之前,您需要注册Microsoft产品Feed. 这只需要做一次. 首先,注册Microsoft签名密钥,然后添加Microsoft产品Feed. ...
- perf record -c
如果perf record -c -c后面接的是sample_period,也就是说你让这个事件没 我的loop进程一直在执行,我的CPU的频率是2.6G hz,也就是说每一秒会有2,600,000, ...
- SpringBoot2.0(一) mybatis
使用mybatis springboot使用mybatis主要依赖 mybatis-spring-boot-starter 来实现.其提供了2中解决方案,一种是使用注解:另一种是简化后的传统的xml方 ...
- 客户端 new socket时候 就像服务端发起连接了
客户端 new socket时候 就像服务端发起连接了
- 【bzoj4715】囚人的旋律 dp
题目描述 给你一个 $1\sim n$ 的排列 $a_i$ ,若 $i\le j$ 且 $a_i\ge a_j$ ,则 $i$ 到 $j$ 有一条边.现在给你这张图,求既是独立集(任意两个选定点都没有 ...
- hdu 1879 继续畅通工程 (最小生成树)
继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- paramiko连接远程主机,上传下载文件
Paramiko是基于SSHv2协议实现的一个Python模块,提供客户端和服务器的功能.Paramiko本身是一个围绕SSH网络概念的纯Python接口. Client: # 创建一个SSH连接对象 ...