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
 
 

Sample Output

1
2
1
3
-1
 
这个题乍一看10^9次方确实很吓人,但是n最多只有200,000,这样的话,即使每个广告牌占一行,也就前200,000行会被占用, 后面全部打酱油的。
再者,广告牌需要插在能放下它的最前面一行,自然插完这个广告牌,这行的容量就减少了。
最后,看这需要O(nlogn)才能过的,logn查询,总感觉像线段树。
然后稍微一想,就是一个线段树对区间最大值的维护。
起始每个点的值都是w,也就是每段区间的值都是w。
然后根据线段树查询,从左儿子到右儿子查询。
直到查到点对应的区间时,就修改该区间的val值,并返回lt或者rt值。
然而,这题数据规模有点BT,必须任意时候不满足就return -1,不能查到底部才返回。
 
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <algorithm>
#define LL long long using namespace std; int h, w, n; //线段树
//区间某点增值,求区间最大值
const int maxn = ;
struct node
{
int lt, rt;
int val;
}tree[*maxn]; //向上更新
void pushUp(int id)
{
tree[id].val = max(tree[id<<].val, tree[id<<|].val);
} //建立线段树
void build(int lt, int rt, int id)
{
tree[id].lt = lt;
tree[id].rt = rt;
tree[id].val = ;//每段的初值,根据题目要求
if (lt == rt)
{
tree[id].val = w;
return;
}
int mid = (lt + rt) >> ;
build(lt, mid, id<<);
build(mid + , rt, id<<|);
pushUp(id);
} //查询某段区间内的符合p的
int query(int lt, int rt, int id, int p)
{
if (tree[id].val < p)
return -;
if (tree[id].lt == tree[id].rt)
{
tree[id].val -= p;
return tree[id].lt;
}
int tmp;
tmp = query(lt, rt, id<<, p);
if (tmp != -)
{
pushUp(id);
return tmp;
}
tmp = query(lt, rt, id<<|, p);
pushUp(id);
return tmp;
} void work()
{
int k, ans;
int len = min(h, n);
build(, len, );
for (int i = ; i < n; ++i)
{
scanf("%d", &k);
ans = query(, len, , k);
printf("%d\n", ans);
}
} int main()
{
//freopen("test.in", "r", stdin);
while (scanf("%d%d%d", &h, &w, &n) != EOF)
{
work();
}
return ;
}
 

ACM学习历程—HDU 2795 Billboard(线段树)的更多相关文章

  1. ACM学习历程—HDU 5289 Assignment(线段树 || RMQ || 单调队列)

    Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered fro ...

  2. hdu 2795 Billboard 线段树单点更新

    Billboard Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=279 ...

  3. HDU 2795 Billboard (线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题目大意:有一块h*w的矩形广告板,要往上面贴广告;   然后给n个1*wi的广告,要求把广告贴 ...

  4. HDU 2795 Billboard (线段树+贪心)

    手动博客搬家:本文发表于20170822 21:30:17, 原地址https://blog.csdn.net/suncongbo/article/details/77488127 URL: http ...

  5. [HDU] 2795 Billboard [线段树区间求最值]

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. HDU 2795 Billboard 线段树,区间最大值,单点更新

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  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 ...

随机推荐

  1. java Map 实现类的对比

    java为数据结构中的映射定义了一个接口 java.util.Map ,他有四个实现类

  2. C语言日期计算器

    记录下码子 # define _CRT_SECURE_NO_WARNINGS # include <stdio.h> # include <stdlib.h> int days ...

  3. HTML经典标签用法

    1.marquee属性的使用说明 <marquee> ... </marquee>移动属性的设置 ,这种移动不仅仅局限于文字,也可以应用于图片,表格等等   鼠标属性 onMo ...

  4. error_logger 爆炸

    具有讽刺意味的是,负责错误日志的进程竟然是最为脆弱的之一.在Erlang的缺省安装中,error_logger39负责记录日志(写入磁盘或者发送到网络上),它的速度要比错误产生的速度慢得多.尤其是在记 ...

  5. gitHub静态页面托管

    github已经是众所周知的程序员同性交友网站了,我就不多说了,(+_+)? 下面讲一讲如何在不用自己购买空间域名备案的情况下,通过github来托管自己的一些小demo或者项目 让其能够通过gith ...

  6. 转载 -- 基于原生JS与OC方法互相调用并传值(附HTML代码)

    最近项目里面有有个商品活动界面,要与web端传值,将用户在网页点击的商品id 传给客户端,也就是js交互,其实再说明白一点就是方法的互相调用而已. 本文叙述下如何进行原生的JavaScript交互 本 ...

  7. cocos2d-js添加道有道插屏(通过jsb反射机制)

    1.导入jar包 2.修改AndroidManifest.xml文件 添加权限:      <activity android:configChanges="keyboard|keyb ...

  8. Tomcat学习笔记【1】--- WEB服务器、JavaEE、Tomcat背景、Tomcat版本

    本文主要讲学习Tomcat需要知道的基础知识. 一 Web服务器 1.1 简介 Web服务器可以解析HTTP协议.当Web服务器接收到一个HTTP请求,会返回一个HTTP响应,例如送回一个HTML页面 ...

  9. 【题解】At2370 Piling Up

    [题解]At2370 Piling Up \[ dp(i,j,0/1) \\ 正在进行i项操作并且此时黑球剩下j个,黑球[0/1]数量曾经到过0 \\ 为什么加第二位,判重.怎么想到的? \] 非常神 ...

  10. python manage.py shell 的增删改查

    python manage.py shell 的增删改查 guoguo-MacBook-Pro:myblog guoguo$ python manage.py shell Python 3.5.1 ( ...