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. SQLite使用EF6的连接配置

    在配置文件中配置连接字符串 1. 使用nuget安装SQLite Install-Package System.Data.SQLite 安装好后的依赖项有: System.Data.SQLite.dl ...

  2. 【转】教你用C#读写、删除、更新excel表格记录

    文章出处:http://blog.csdn.net/kuangshazi515/article/details/6585118 如下图所示,编一个程序,鼠标单击窗体视图区(右边)时,获取一对坐标(X, ...

  3. extjs 动态设定 DateField 最大值 最小值

    yxrqDate.minValue = new Date();yxrqDate.maxValue = new Date(9000,1,1);yxrqDate.validate(); //var pic ...

  4. Rete算法

    RETE算法介绍一. rete概述Rete算法是一种前向规则快速匹配算法,其匹配速度与规则数目无关.Rete是拉丁文,对应英文是net,也就是网络.Rete算法通过形成一个rete网络进行模式匹配,利 ...

  5. AD域 根据 用户属性userAccountControl 来判断用户禁用属性

    参考:https://support.microsoft.com/zh-cn/help/305144/how-to-use-the-useraccountcontrol-flags-to-manipu ...

  6. “==”和equals之间的区别

    通俗点讲,==是看看左右是不是一个东西.equals是看看左右是不是长得一样.如果单纯是想记住,==:等于.equals:相同.两个长得一样的人,只能说长的相同(equals),但是不等于他们俩是一个 ...

  7. JQuery文件上传及以Base64字符串形式呈现图片

    一:上传之 首先,你必然得有一个 file input,如下: <td>     <img id="imgGif" style="display: no ...

  8. 【BZOJ】【2502】清理雪道

    网络流/上下界网络流 带下界的最小可行流…… 我SB了,跑网络流的时候是得从虚拟源0往出跑……而不是S…… Orz Hzwer /*********************************** ...

  9. JMS基本概念之一

    The Java Message Service(JMS) API is a messaging standard that allows application components based o ...

  10. 使用DBCA工具创建自己的数据库

    ylbtech-Oracle:使用DBCA工具创建自己的数据库  DBCA创建数据库 默认安装的Oracle数据库一般不能满足实际应用的需求,例如数据库名称.数据库块的大小等都需要修改,那么我们应该自 ...