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. win10+vs2008编译比特币1.0版源码总结

    https://zhuanlan.zhihu.com/p/25074960 https://zhuanlan.zhihu.com/p/25095222 总体上是参考这两个链接,感谢大神的分享,但是中间 ...

  2. 基于IdentityServer4的单点登录——Api

    1.新建项目并添加引用 新建一个asp .net core 2.0的项目引用IdentityServer4.AccessTokenValidation 2.配置 将Api与IdentityServer ...

  3. Selenium-简介

    一.简介 Selenium是UI自动化的一个框架. Selenium1.0时代就是用js注入技术与浏览器交互. Selenium WebDriver就是调用浏览器原生的API来实现的操作.他是Clie ...

  4. 【C#】wpf查找父子节点

    原文:[C#]wpf查找父子节点 using System; using System.Collections.Generic; using System.Linq; using System.Tex ...

  5. 起调UWP的几种方法

    原文:起调UWP的几种方法 由于种种原因吧,我需要使用一个WPF程序起调一个UWP程序,下面总结一下,给自己个备份. 启动UWP程序的关键是协议启动 给我们的UWP应用添加一个协议,like this ...

  6. Windows 10开发基础——XML和JSON (一)

    主要内容: JSON的序列化与反序列化 XML的序列化与反序列化 1.JSON的序列化与反序列化     JSON(JavaScript Object Notation)是一种轻量级的数据交换语言,它 ...

  7. UWP应用载入SVG图片的兼容性方案

    原文 UWP应用载入SVG图片的兼容性方案 新版本<纸书科学计算器>的更新点之一,就是优化了表达式的显示方式.在旧版本中,表达式里的符号是用png图片显示的,当用户放大看的时候会发现一些锯 ...

  8. Android零基础入门第48节:可折叠列表ExpandableListView

    原文:Android零基础入门第48节:可折叠列表ExpandableListView 上一期学习了AutoCompleteTextView和MultiAutoCompleteTextView,你已经 ...

  9. 基于VUE实现的新闻后台管理系统-二

    基础环境及最后的开发效果已完成说明,接下来就开始配置. ¶npm初始化 新建项目文件夹VueDemo,在其内执行如下脚本 npm init -y 安装vue-cli构建包 yarn add vue-c ...

  10. ORACLE 11.2.0.4 Single To Single Data Guard 安装 physical standby

    [root@ORACLE ~]# su - oracle [oracle@ORACLE ~]$ sqlplus / as sysdba . 查看主库归档模式: SQL> select log_m ...