http://acm.hdu.edu.cn/showproblem.php?pid=2795

Billboard

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
#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(线段树)的更多相关文章

  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. springboot使用logback日志,部署到tomcat不生效问题解决

    1.springboot 配置日志方法 使用该方法配置日志,在本地调试可以正常写入日志文件,但是打包发布到tomcat以后日志配置不生效. 2.修改配置 Spring Boot官方推荐优先使用带有-s ...

  2. docker include not found: networks

    启动clickhouse的docker镜像时,出现了以下错误 include not found: networks google之后发现是因为可能不支持ipv6导致的解决方法 就是通过设置 /etc ...

  3. EF 导航属性的使用

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  4. Visual Studio更改编码格式为“UTF-8”

    原文:Visual Studio更改编码格式为"UTF-8" 用VS2015新建了个Python文件,在VS2015打开时中文显示正常, 用Visual Studio Code文本 ...

  5. LOCK_TIMEOUT

    SET LOCK_TIMEOUT 1000 begin tran TranNameA select * from tablenameA WITH (updlock) where... waitfor  ...

  6. 利用Python sklearn的SVM对AT&T人脸数据进行人脸识别

    要求:使用10-fold交叉验证方法实现SVM的对人脸库识别,列出不同核函数参数对识别结果的影响,要求画对比曲线. 使用Python完成,主要参考文献[4],其中遇到不懂的功能函数一个一个的查官方文档 ...

  7. LINQ查询表达式---------into

    LINQ查询表达式---------into into 上下文关键字创建一个临时标识符,以便将 group.join 或 select 子句的结果存储到新的标识符 class Program { pu ...

  8. 【Windows10 IoT开发系列】API 移植工具

    原文:[Windows10 IoT开发系列]API 移植工具 Windows 10 IoT Core 中是否提供你的当前 Win32 应用程序或库所依赖的 API? 如果不提供,是否存在可使用的等效 ...

  9. PC-lint 简明教程(C/C++静态代码检查工具)

    前言 PC-lint是一款小而强大的C/C++静态代码检查工具,它可以检查未初始化变量,数组越界,空指针等编译器很难发现的潜在错误.在很多专业的软件公司如Microsoft,PC-Lint检查无错误无 ...

  10. 第一式、单例模式-Singleton模式(创建型)

    一.简介 单例模式主要用的作用是用于保证程序运行中某个类只有一个实例,并提供一个全局入口点.单例模式(Singleton)为GOF阐述的标准24种设计模式中最简单的一个.但随着时间推移,GOF所阐述的 ...