Billboard

Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19225    Accepted Submission(s): 8042

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
 

题目链接: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(线段树的另类应用)的更多相关文章

  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. int *p()与int (*p)()的区别

    int *p()是返回指针的函数 int (*p)()是指向函数的指针   返回指针的函数: int *a(int x,int y); 有若干个学生的成绩(每个学生有4门课程),要求在用户输入学生序号 ...

  2. css局部概念的理解:

    1.DIV-Padding理解:一直以来对div中的padding属性,一直不理解,使用最多的也就是margin,padding是div的内空间的相对距离,margin是div的外部相对位置,如果用一 ...

  3. poj 1811 大数分解

    模板 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> ...

  4. matlab中的ishghandle

    ishghandle True for Handle Graphics object handles. ishghandle(H) returns an array that contains 1's ...

  5. hadoop系列讨论

    生产环境下cloudera的cdh版本更受欢迎,但是貌似cloudera对于YARN的开发力度不是很突出,所以在apache与cdh版本选择的时候,大家是怎么考虑的? 主要关注生产环境下的运维成本,期 ...

  6. 由文章缩略图读出banner图

    <div class="banner"> {dede:sql sql="select litpic FROM #@__archives where typei ...

  7. Effective C++ 学习笔记[2]

    2. 第一节 习惯C++ 2.1 C++是一个语言联邦,包括以下四个部分: C:包括区块.语句.预处理器.内置数据类型.数组.指针等,但是C语言本身存在局限:没有模板template.没有异常exce ...

  8. [转载]“浅拷贝”与“深拷贝”

    对于普通类型的对象来说,它们之间的复制是很简单的,例如: int a=88; int b=a; 而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员变量. C++中对象的复制就如同&quo ...

  9. 枚举 POJ 1753 Flip Game

    题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...

  10. flst与fitem命令是这么用的

    k,94,75.69947,44.09817,-40 k,95,50.9349,44.01963,-40 k,96,37.83799,45.04814,-40 k,97,15.1304,48.2934 ...