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值,这些是公告的尺寸. 贴公告 ...
随机推荐
- .Net C#向远程服务器Api上传文件
Api服务代码一: /// <summary> /// 服务器接收接口 /// </summary> [HttpPost] [Route("ReceiveFile&q ...
- Apple Developer Registration and DUNS Number Not Accepted
Now that I have my Mac and app source code. I’m ready to start working on my first app. The next ste ...
- HDU5087 Revenge of LIS II (LIS变形)
题目链接:pid=5087">http://acm.hdu.edu.cn/showproblem.php?pid=5087 题意: 求第二长的最长递增序列的长度 分析: 用step[i ...
- iOS开发中的 ARC
1. weak属性(弱引用) 被weak修饰的对象叫弱引用,不算对象持有者,一个方法执行完后会导致这个对象自动释放掉,并将对象的指针设置成nil,我使用GCD延时1000ms来验证,1000ms之后, ...
- StringUtils类常用方法介绍
StringUtils方法包的位置:org.apache.commons.lang.StringUtils StringUtils 方法的操作对象是 java.lang.String 类型的对象,是 ...
- MyEclipse中的内置浏览器中的历史记录怎么清除
eclipse内置浏览器的访问记录是存储在对应的工程目录下的.metadata配置中,也就是说你新建一个工程的话就没有了. 如果确实要删除那就找到工作空间中的org.eclipse.ui.browse ...
- java执行ping命令
public static void get() throws IOException{ String address="10.132.118.110"; Process proc ...
- Oracle 10g AND Oracle 11g手工建库案例--Oracle 10g
Oracle 10g AND Oracle 11g手工建库案例--Oracle 10g 系统环境: 操作系统: RedHat EL6 Oracle: Oracle 10g and Oracle 11 ...
- 【TYVJ 五月图论专项有奖比赛】
最短路+TSP+最小生成树+倍增LCA+TreeDP 第一题 其实是个TSP问题(然而我没发现),但是关键点很少,只有5个,所以用dij+heap分别预处理出来这五个点为源的最短路…… 然后枚举起点 ...
- QT笔记:数据库总结(三)之SQL模型类-QSqlTableModel模型
QSqlTableModel类继承至QSqlQueryModel类,该类提供了一个可读写单张SQL表的可编辑数据模型,功能:修改,插入,删除,查询,和排序 常用函数 QVariant headerDa ...