Billboard

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

Total Submission(s): 21203    Accepted Submission(s): 8750

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
 

思路:

刚开始是按照宽度去想的,每一个宽度里面的各自最大数目是h。然后一边一边刷,开数组就需要1e9<<2。果断放不下。然后按照h去考虑,相当于求区间最大值。但是h最大取到1e9,数组还是需要开1e9<<2。瞬间不知道怎么办。讨论区里面有人说不用开那么大,太大无意义。重新看题。题目上约束了n的范围,200000。假如最理想的情况我们每一行放置一个广告,最多需要n行就可以,如果给定的h比这个数值要大,那么有h-n行就当于浪费空间。所以,线段树的大小就是min(h,n)<<2。

刚开始用的不是区间最大值,用的还是区间求和,然后过了样例,但是想法是错误,因为有一些情况他仍旧考虑不到。比如下面的图片:

假如我现在插入宽度为5的,那么按照sum区间和的情况肯定往左边走,然后就会返回-1。而实际上,右子树就可以放置下。

更新直接在查询的时候顺带完成

代码:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int maxn=200005;
long long maxnum[maxn<<2];
int h,w,n;
void pushup(int rt) {
maxnum[rt]=max(maxnum[rt<<1],maxnum[rt<<1|1]);
}
void build(int l, int r, int rt) {
maxnum[rt]=w;
if(l==r) return;
int mid=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
int query(int width, int l, int r, int rt) {
if(l==r) {
maxnum[rt]-=width;
return l;
}
int mid=(l+r)>>1,pos;
if(maxnum[rt<<1]>=width) pos=query(width,lson);
else pos=query(width,rson);
pushup(rt);
return pos;
}
int main() {
while(~scanf("%d%d%d",&h,&w,&n)) {
h=min(h,n);
build(1,h,1);
for(int i=1;i<=n;++i) {
int width,pos;scanf("%d",&width);
if(maxnum[1]<width) pos=-1;
else pos=query(width,1,h,1);
printf("%d\n",pos);
}
}
return 0;
}

HDU 2795 Billboard 线段树,区间最大值,单点更新的更多相关文章

  1. HDU 2795 线段树区间最大值,单点更新+二分

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

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

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

  3. HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧)

    HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧) 题意分析 题目大意:一个h*w的公告牌,要在其上贴公告. 输入的是1*wi的w值,这些是公告的尺寸. 贴公告 ...

  4. ACM学习历程—HDU 2795 Billboard(线段树)

    Description At the entrance to the university, there is a huge rectangular billboard of size h*w (h ...

  5. hdu 1116 敌兵布阵 线段树 区间求和 单点更新

    线段树的基本知识可以先google一下,不是很难理解 线段树功能:update:单点增减 query:区间求和 #include <bits/stdc++.h> #define lson ...

  6. HDU 2795 Billboard (线段树单点更新 && 求区间最值位置)

    题意 : 有一块 h * w 的公告板,现在往上面贴 n 张长恒为 1 宽为 wi 的公告,每次贴的地方都是尽量靠左靠上,问你每一张公告将被贴在1~h的哪一行?按照输入顺序给出. 分析 : 这道题说明 ...

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

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

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

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

  9. HDU 2795 Billboard (线段树)

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

随机推荐

  1. Java正则表达式详解+练习

    一.导读 正则表达式,又称规则表达式.(英文名Regular Expression,所以代码中常以regex.regexp.RE表示).正则表达式简单说就是用于操作文本数据的规则表达式,在Java中我 ...

  2. centos 安装giblab

    本文章转载自:http://www.cnblogs.com/fanjingfeng/p/6665597.html 一, 服务器快速搭建gitlab方法 可以参考gitlab中文社区 的教程 cento ...

  3. 在EF中正确的使用事务

    1.EF中使用事务: using (TransactionScope tran = new TransactionScope()) { try { using(var _context = new D ...

  4. 用BroadcastReceiver监听手机网络状态变化

    android--解决方案--用BroadcastReceiver监听手机网络状态变化 标签: android网络状态监听方案 2015-01-20 15:23 1294人阅读 评论(3) 收藏 举报 ...

  5. <script src="xxx.php"></script>

    应热情粉丝的殷切期待,我决定从百忙之中抽出时间来完成这篇博文.(开玩笑啦) 我也是近期才接触到这种引用js的办法.例如,有这样一段js代码 <script src='http://ww.***. ...

  6. shell编程下 特殊变量、test / [ ]判断、循环、脚本排错

    第1章 shell中的特殊变量 1.1 $# $# 表示参数的个数 1.1.1 [示例]脚本内容 [root@znix ~]# cat /server/scripts/show2.sh #!/bin/ ...

  7. L2-2. 链表去重

    L2-2. 链表去重 时间限制300 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者陈越给定一个带整数键值的单链表L,本题要求你编写程序,删除那些键值的绝对值有重复的 ...

  8. ajax请求发送和页面跳转的冲突

    http://blog.csdn.net/allenliu6/article/details/77341538?locationNum=7&fps=1 在A页面中,有一个click事件,点击时 ...

  9. 图片格式 WebP APNG

    WebP  是一种支持有损压缩和无损压缩的图片文件格式,派生自图像编码格式 VP8.根据 Google 的测试,无损压缩后的 WebP 比 PNG 文件少了 45% 的文件大小,即使这些 PNG 文件 ...

  10. NFS服务安装及配置

    服务器环境:CentOS6.9  Linux 2.6.32-696.el6.x86_64 安装NFS服务 nfs客户端和服务端都只需要安装nfs-utils包即可,并且yum安装时会连带安装rpcbi ...