Billboard

Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 15625    Accepted Submission(s): 6580

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
 
Author
hhanger@zju
 
Source
 
Recommend
lcy

一開始看到题目时。仅仅扫了一眼。满满的全是英文,看不下去,直接pass,看其它题去了。

之后看到出这题的人越来越多。没办法还是硬着头皮開始读题。

看完之后,直接想到线段树维护区间最小值。敲起来也挺顺的,比想象中要简单。O(∩_∩)O哈!看来以后还是不能由于题目太长难读,而放弃AC机会了。


题意:有一块尺寸为h*w的宣传栏,要在上面贴1*wi的宣传单n张,选择的位置是要求为:尽可能高,对于同一高度,则选择尽可能靠左的地方。要求输出每张宣传单所在行的高度。

思路:线段树维护区间最小值,以1~h划分区间,每个区间中保存该区间已经被占用的最小长度,每次从最上面開始找。找到符合的位置贴上,该位置减去wi。更新区间的最小长度值。


AC代码例如以下:

/*
Author:ZXPxx
Memory: 6320 KB Time: 2698 MS
Language: C++ Result: Accepted
*/ #include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; #define lson l,m,rt<<1
#define rson m+1,r,(rt<<1)|1
#define root 1,n,1
#define mid (l+r)>>1
#define LL long long const int MX = 3e5+5 ;
int w,h,n;
int sum[MX<<2];
void pushup(int rt) {
sum[rt]=min(sum[rt<<1],sum[rt<<1|1]);
}
void build(int l,int r,int rt) {
memset(sum,0,sizeof(sum));
}
int query(int x,int l,int r,int rt) {
if(l==r) {
sum[rt]+=x;
return l;
}
int m=mid,ret=0;
if((sum[rt<<1])+x<=w) ret=query(x,lson);
else
ret=query(x,rson);
pushup(rt);
return ret;
}
int main() {
int x;
while(~scanf("%d%d%d",&n,&w,&h)) {
n=min(h,n);
build(root);
for(int i=1;i<=h;i++) {
scanf("%d",&x);
if(sum[1]+x>w)
printf("-1\n");
else
printf("%d\n",query(x,root));
}
}
return 0;
}

HDU 2795 Billboard (线段树)的更多相关文章

  1. hdu 2795 Billboard 线段树单点更新

    Billboard Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=279 ...

  2. HDU 2795 Billboard (线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题目大意:有一块h*w的矩形广告板,要往上面贴广告;   然后给n个1*wi的广告,要求把广告贴 ...

  3. HDU 2795 Billboard (线段树+贪心)

    手动博客搬家:本文发表于20170822 21:30:17, 原地址https://blog.csdn.net/suncongbo/article/details/77488127 URL: http ...

  4. [HDU] 2795 Billboard [线段树区间求最值]

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. HDU 2795 Billboard 线段树,区间最大值,单点更新

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. ACM学习历程—HDU 2795 Billboard(线段树)

    Description At the entrance to the university, there is a huge rectangular billboard of size h*w (h ...

  7. HDU 2795 Billboard (线段树单点更新 && 求区间最值位置)

    题意 : 有一块 h * w 的公告板,现在往上面贴 n 张长恒为 1 宽为 wi 的公告,每次贴的地方都是尽量靠左靠上,问你每一张公告将被贴在1~h的哪一行?按照输入顺序给出. 分析 : 这道题说明 ...

  8. HDU 2795 Billboard 线段树活用

    题目大意:在h*w 高乘宽这样大小的 board上要贴广告,每个广告的高均为1,wi值就是数据另给,每组数组给了一个board和多个广告,要你求出,每个广告应该贴在board的哪一行,如果实在贴不上, ...

  9. hdu 2795 Billboard 线段树+二分

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Probl ...

  10. HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧)

    HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧) 题意分析 题目大意:一个h*w的公告牌,要在其上贴公告. 输入的是1*wi的w值,这些是公告的尺寸. 贴公告 ...

随机推荐

  1. java 盒子模型

    http://www.cnblogs.com/xiaohuochai/tag/javascript%E6%80%BB%E7%BB%93/

  2. Delphi TClientDataSet的使用

    1.TClientDataSet的基本属性和方法介绍 1).FieldDefs: 字段定义列表属性 开发者可通过单击属性编辑器中该属性编辑按钮,或在该控件上单击右键选择弹出菜单中的"Fiel ...

  3. H5页面开发笔记(react技术栈)

    1.子组件接收父组件的参数,要在子组件的componentDidMount函数中更改当前组件的state,若写在componentWillMount函数中,则会导致初始化界面UI的时候不能得到预期的效 ...

  4. 将asi-http-request引入到ARC工程需要做的 转

    在发表前,容许我发两句牢骚,太折腾了.但结合网路上各种大侠的答案相助,最终我终于可以在ARC项目使用该库了. 1.需要下载asi-http-request这个包.https://github.com/ ...

  5. 3种方式实现KVO并进行对比

    KVO KVO属于设计模式中的观察者模式,在观察者模式中,一个对象任何状态的变更都会通知另外的对改变感兴趣的对象.这些对象之间不需要知道彼此的存在,这其实是一种松耦合的设计.当某个属性变化的时候,我们 ...

  6. Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!

    执行Maven Install打包的时候,提示以下警告信息: [WARNING] Using platform encoding (GBK actually) to copy filtered res ...

  7. dwz ajax session超时跳转登录页(struts2自定义拦截器)

    1.定义struts2拦截器(网上例子很多) 代码如下: package rt.intercepter; import java.util.Map; import javax.servlet.http ...

  8. 深度学习数据集Deep Learning Datasets

    Datasets These datasets can be used for benchmarking deep learning algorithms: Symbolic Music Datase ...

  9. 第四章 mybatis批量insert

    批量插入sql语句: INSERT INTO table (field1,field2,field3) VALUES ('a',"b","c"), ('a',& ...

  10. 【记录】cygwin下折腾个人配置环境

    (本文由hcbbt发布,转载请注明出处,blog.csdn[dot]net/hcbbT)      cygwin是windows下的linux的模拟环境,不仅可以执行linux的各种命令,可以在cyg ...