HDU 2795:Billboard(线段树)
http://acm.hdu.edu.cn/showproblem.php?pid=2795
Billboard
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.
#include <cstdio>
#include <cstring>
using namespace std;
#define N 200005
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
struct node
{
int value;
}tree[N<<];
/*
线段树
记得要用h和n的较小值来建树
思路比较难想:每个区间的value装的是该区间还能放下的最大长度
然后与输入进来的len进行比较
题意要求优先放左边,所以如果左儿子放得下优先放左儿子,不然看右儿子
然后要PushUp更新父节点信息
如果根节点即第一个节点放不下,即整棵树必定没有一个区间可以放得下该广告
*/
void Build(int rt,int l,int r,int k)
{
tree[rt].value=k;
if(l==r) return ;
int m=(l+r)>>;
Build(lson,k);
Build(rson,k);
} int Query(int rt,int l,int r,int len)
{
int ans;
if(l==r){
tree[rt].value-=len;
return l;
}
int m=(l+r)>>;
//如果左边能放下,优先放左边,不然看右边能不能放下
if(tree[rt<<].value>=len) ans=Query(lson,len);
else ans=Query(rson,len);
// tree[rt].value=max(tree[rt<<1].value,tree[rt<<1|1].value);
//PushUp
if(tree[rt<<].value>=tree[rt<<|].value)
tree[rt].value=tree[rt<<].value;
else tree[rt].value=tree[rt<<|].value;
return ans;
} int main()
{
int h,w,n;
while(~scanf("%d%d%d",&h,&w,&n)){
if(h>n) h=n;
Build(,,h,w);
while(n--){
int len;
scanf("%d",&len);
if(tree[].value<len) printf("-1\n");
else printf("%d\n",Query(,,h,len));
}
}
return ;
}
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值,这些是公告的尺寸. 贴公告 ...
随机推荐
- Cocos2d-x 脚本语言Lua介绍
Cocos2d-x 脚本语言Lua介绍 本篇博客记录Lua学习.学习来自eoe论坛,Lua语言开发Cocos2d-x游戏入门视频教程,猛戳下面地址: http://www.eoeandroid.com ...
- MSRA专访摘要
前段时间有幸参加微软亚洲研究院之旅,顺便投简历,没想到在两次访谈迎来,并且是连续 的两次被拒绝.严重的刺激到了我.导致我疯狂的复习刷Offer.如今最终算是告于段落.如今也最终有空沉下心来总结 总结近 ...
- 一步一步造个IoC轮子(一):IoC是什么
一步一步造个Ioc轮子目录 一步一步造个IoC轮子(一):IoC是什么 一步一步造个IoC轮子(二):详解泛型工厂 一步一步造个IoC轮子(三):构造基本的IoC容器 前言 .net core正式版前 ...
- 【转】关于List排序的时效性
不多说了,就是说明List排序的时效性,仅仅用来备忘,改造自: http://blog.csdn.net/wanzhuan2010/article/details/6205884,感谢原作者 usin ...
- wpf中防止界面卡死的写法
原文:wpf中防止界面卡死的写法 ); this.Dispatcher.BeginInvoke(new Action(() => { this.button1.Content = "计 ...
- 领域驱动设计(DDD)的实践经验分享之持久化透明
原文:领域驱动设计(DDD)的实践经验分享之持久化透明 前一篇文章中,我谈到了领域驱动设计中,关于ORM工具该如何使用的问题.谈了很多我心里的想法,大家也对我的观点做了一些回复,或多或少让我深深感觉到 ...
- 压缩大文件时如何限制CPU使用率?----几种CPU资源限制方法的测试说明
一.说明 我们的MySQL实例在备份后需要将数据打包压缩,部分低配机器在压缩时容易出现CPU打满导致报警的情况,需要在压缩文件时进行CPU资源的限制. 因此针对此问题进行了相关测试,就有了此文章. 二 ...
- xgboost参数及调参
常规参数General Parameters booster[default=gbtree]:选择基分类器,可以是:gbtree,gblinear或者dart.gbtree和draf基于树模型,而gb ...
- Android 8通过startService引起crash问题
Android 8.0 不再允许后台service直接通过startService方式去启动,否则就会引起IllegalStateException.解决方式: if (Build.VERSION.S ...
- JavaScript API for Office Outlook Add-in - “一页纸文档“
上一篇文章 Office Add-in Model 为 Outlook Mail Add-in 提供的 JavaScript API 介绍 ,简单地在表格中列出了所有的 Object 定义,但是个人感 ...