HDU 2795 Billboard(线段树的另类应用)
Billboard
Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19225 Accepted Submission(s): 8042
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.
2
4
3
3
3
2
1
3
-1
题目链接:HDU 2795
有点意思的一道题目,主要思想就是不停的找容得下所给宽度wid的区间,且要优先找靠左的区间,这个好办,优先递归左子树即可,然后怎么看是否还有空间能放海报呢?那看T[1]即树根,若T[1].max小于wid那说明整个区间最大空闲值都不够,不能放,否则就用update去递归寻找就好了,既然T[1].mx>=wid,那肯定存在某一个点的最大值大于wid,那直接利用update的递归方式不停地无脑二分下去肯定能找到,当然要优先递归左子树。
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=200010;
struct seg
{
int l,mid,r;
int mx;
}T[N<<2];
int h,w,n;
inline int max(int a,int b)
{
return a>b?a:b;
}
void pushup(int k)
{
T[k].mx=max(T[LC(k)].mx,T[RC(k)].mx);
}
void build(int k,int l,int r)
{
T[k].l=l;
T[k].r=r;
T[k].mid=MID(l,r);
if(l==r)
T[k].mx=w;
else
{
build(LC(k),l,T[k].mid);
build(RC(k),T[k].mid+1,r);
pushup(k);
}
}
void update(int k,int val)
{
if(T[k].l==T[k].r&&T[k].mx>=val)
{
printf("%d\n",T[k].l);
T[k].mx-=val;
}
else
{
if(T[LC(k)].mx>=val)
update(LC(k),val);
else
update(RC(k),val);
pushup(k);
}
}
int main(void)
{
int i,wid;
while (~scanf("%d%d%d",&h,&w,&n))
{
int R=min(h,n);
build(1,1,R);
for (i=0; i<n; ++i)
{
scanf("%d",&wid);
if(T[1].mx<wid)
puts("-1");
else
update(1,wid);
}
}
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值,这些是公告的尺寸. 贴公告 ...
随机推荐
- C语言字符串处理
一. C语言中,为什么字符串可以赋值给字符指针变量 char *p,a='5';p=&a; //显然是正确的,p="abcd"; ...
- javascript十六进制数字和ASCII字符之间转换
var hex="0x29";//十六进制 var charValue = String.fromCharCode(hex);//生成Unicode字符 var charCode ...
- C++中的static数据成员与static成员函数
本文要点: 1.static成员它不像普通的数据成员,static数据成员独立于该类的任意对象而存在,每个static数据成员是与类关联的对象,并不与该类的对象相关联! aka:每个static数据成 ...
- JUC回顾之-Semaphore底层实现和原理
1.控制并发线程数的Semaphore Semaphore(信号量)是用来控制同时访问特定资源的线程数量,它通过协调各个线程,保证合理的使用公共资源. 线程可以通过acquire()方法来获取信号量的 ...
- OneApm
cloudinsight-java-sdk https://github.com/cloudinsight
- Linux内核同步机制之(二):Per-CPU变量
转自:http://www.wowotech.net/linux_kenrel/per-cpu.html 一.源由:为何引入Per-CPU变量? 1.lock bus带来的性能问题 在ARM平台上,A ...
- ****CSS各种居中方法
水平居中的text-align:center 和 margin:0 auto 这两种方法都是用来水平居中的,前者是针对父元素进行设置而后者则是对子元素.他们起作用的首要条件是子元素必须没有被flo ...
- Redis/SSDB+Twemproxy的配置与使用(Mac/Linux平台)
对于redis而已,相信不少的后台开发人员一直都在使用,相比memcache而已,redis不仅可以作为key-value缓存使用,而且提供了丰富的数据结构如set.list.map等,能够实现很多复 ...
- vijos 1037 ***
链接:点我 #include <cstdio> #include <cstring> #include <algorithm> #include <iostr ...
- 网站构建学习笔记(0)——基本概念了解及资源学习(copy自w3school)
一.学习方面 1.WWW - 万维网 什么是 WWW? WWW 指万维网(World Wide Web) 万维网常被称为Web Web 是由遍布全球的计算机所组成的网络 所有 Web 中的计算机都可以 ...