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. eclipse原文件编码GBK-UTF8

    菜单Window-Preferences-General-Workspace,在右侧面板的Text file encoding选项中选择Other:UTF-8 上一步如果不起作用,可以尝试设置下: W ...

  2. select into from 提示 Undeclared variable.....错误的解决办法 && select into from 和 insert into select 的用法和区别

    然而今天在使用 SELECT INTO FROM 备份mysql数据表的时候,运行相关 sql 语句的时候却一直返回 [Err] 1327 - Undeclared variable: ...... ...

  3. Man简单介绍

    转自:http://os.51cto.com/art/201312/425525.htm Linux系统提供了相对比较丰富的帮助手册(man),man是manual的缩写,在日常linux系统管理中经 ...

  4. WPF中的画图

    1.border(边框):      <Border BorderBrush="Blue" BorderThickness="0,1,1,1" Grid. ...

  5. javascript的队列,优先队列,循环队列

    按书上的来弄的.慢慢理解了. function Queue() { var items = []; this.enqueue = function(element){ items.push(eleme ...

  6. ytu 1985:C语言实验——保留字母(水题)

    C语言实验——保留字母 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 136  Solved: 59[Submit][Status][Web Board] ...

  7. [原]AppPoolService-IIS应用程序池辅助类(C#控制应用程序池操作)

    using System.Collections.Generic; using System.DirectoryServices; using System.Linq; using Microsoft ...

  8. poj 2923 状压dp+01背包

    好牛b的思路 题意:一系列物品,用二辆车运送,求运送完所需的最小次数,两辆车必须一起走 解法为状态压缩DP+背包,本题的解题思路是先枚举选择若干个时的状态,总状态量为1<<n,判断这些状态 ...

  9. poj 3468 成段增减

    Sample Input 10 5 1 2 3 4 5 6 7 8 9 10 Q 4 4 Q 1 10 Q 2 4 C 3 6 3 Q 2 4 Sample Output 4 55 9 15 #inc ...

  10. 关于服务器响应,浏览器请求的理解以及javaWeb项目的编码问题

    1.服务器(Server)响应,浏览器(Brower)请求: 对于B/S的软件,数据的传递体现在,用户利用浏览器请求,以获得服务器响应.在JavaWeb项目中,大致包含.java文件的数据处理模块,和 ...