HDU 2795 Billboard (线段树)
Billboard
Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15625 Accepted Submission(s): 6580
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.
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 "-1" for this announcement.
3 5 5
2
4
3
3
3
1
2
1
3
-1
之后看到出这题的人越来越多。没办法还是硬着头皮開始读题。
看完之后,直接想到线段树维护区间最小值。敲起来也挺顺的,比想象中要简单。O(∩_∩)O哈!看来以后还是不能由于题目太长难读,而放弃AC机会了。
思路:线段树维护区间最小值,以1~h划分区间,每个区间中保存该区间已经被占用的最小长度,每次从最上面開始找。找到符合的位置贴上,该位置减去wi。更新区间的最小长度值。
/*
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 (线段树)的更多相关文章
- hdu 2795 Billboard 线段树单点更新
Billboard Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=279 ...
- HDU 2795 Billboard (线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题目大意:有一块h*w的矩形广告板,要往上面贴广告; 然后给n个1*wi的广告,要求把广告贴 ...
- HDU 2795 Billboard (线段树+贪心)
手动博客搬家:本文发表于20170822 21:30:17, 原地址https://blog.csdn.net/suncongbo/article/details/77488127 URL: http ...
- [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 ...
- ACM学习历程—HDU 2795 Billboard(线段树)
Description At the entrance to the university, there is a huge rectangular billboard of size h*w (h ...
- HDU 2795 Billboard (线段树单点更新 && 求区间最值位置)
题意 : 有一块 h * w 的公告板,现在往上面贴 n 张长恒为 1 宽为 wi 的公告,每次贴的地方都是尽量靠左靠上,问你每一张公告将被贴在1~h的哪一行?按照输入顺序给出. 分析 : 这道题说明 ...
- HDU 2795 Billboard 线段树活用
题目大意:在h*w 高乘宽这样大小的 board上要贴广告,每个广告的高均为1,wi值就是数据另给,每组数组给了一个board和多个广告,要你求出,每个广告应该贴在board的哪一行,如果实在贴不上, ...
- hdu 2795 Billboard 线段树+二分
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Probl ...
- HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧)
HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧) 题意分析 题目大意:一个h*w的公告牌,要在其上贴公告. 输入的是1*wi的w值,这些是公告的尺寸. 贴公告 ...
随机推荐
- MVC自定义编辑视图,DateTime类型属性显示jQuery ui的datapicker
实现的效果为:在编辑视图中,对DateTime类型的属性,显示jQuery UI的datepicker.效果如下: Student.cs public class Student { ...
- Google In-App Billing 实现(内含Unity 实现经验)
实现内购计费 傻逼目录 Adding the AIDL file Updating Your Manifest Creating a ServiceConnection Making In-app ...
- NodeJS学习资料合集
1. 官网 nodejs 2. How do I get started with Node.js,stackoverflow提问,收集非常多实用的网站 3. node-books.github收 ...
- 精选 5 个漂亮的 CSS3 图片滑过特效
这篇文章将为大家分享5款漂亮的CSS3图片滑过特效,比如滑过后显示图片的详细文字介绍,又比如滑过后对图片进行淡入淡出的效果等等.让我们一起来看看,喜欢的朋友赶紧收藏. 1.非常酷的CSS3图片说明效果 ...
- Cocos2d-x3.3RC0通过JNI调用Android的Java层URI代码发送短信
1.Jni不在赘述.翻看前面博客 2.直接上代码 1)Java层,直接加在AppActivity.java中 public class AppActivity extends Cocos2dxActi ...
- 理解js中的new
new 操作符 在有上面的基础概念的介绍之后,在加上new操作符,我们就能完成传统面向对象的class + new的方式创建对象,在Javascript中,我们将这类方式成为Pseudoclassic ...
- CSS结构和层叠
每个合法的文档都会生成一个文档树,从而能根据元素的祖先,属性,兄弟元素等创建选择器来选择元素.有了这个结构树,选择器才能起作用,这也是CSS继承的核心.继承是从一个元素向其后代元素传递属性值所采用的机 ...
- [Web前端]由cookies安全说开去
在Web应用中,Cookie很容易成为安全问题的一部分.从以往的经验来看,对Cookie在开发过程中的使用,很多开发团队并没有形成共识或者一定的 规范,这也使得很多应用中的Cookie成为潜在的易受攻 ...
- IOS开发之新浪微博OAuth2
说明:微博开放接口的调用,如发微博.关注等,都是需要获取用户身份认证的.目前微博开放平台用户身份鉴权主要采用的是OAuth2.0.为了方便开发者开发.测试自己的应用. OAuth2.0较1.0相比,整 ...
- Android 当修改一些代码时,使用什么编译命令可以最有效率
前言 欢迎大家我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net ...