hdu 2795 Billboard(线段树单点更新)
Billboard
Total Submission(s): 14337 Accepted Submission(s): 6148
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.
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.
be put on the billboard, output "-1" for this announcement.
3 5 5
2
4
3
3
3
1
2
1
3
-1
题目大意:在一块h*w的黑板上贴广告。尽量往左上角贴,每张广告高度为1。对每次贴一张广告询问广告能贴在第几行(假设不能贴就输出-1)。
思路:数据非常大,但不用离散化。由于广告高度为1,最多200000张广告。最差就是每行贴一张。
所以当h>n的时候,能够令h=n。
方法是线段树。首先建树,每一个点代表每行的剩余长度,对于一个区间能够找出其最大的剩余长度。
查询的时候假设左子树能够插入就往左子树插,左子树不行就看右子树,假设两者都不行就表示不能贴这张广告。
#include<stdio.h>
#include<string.h>
#define M 220005
struct tree{
int l,r,len;
}tree[M<<2];
int h,W,n,w[M];
int max(int a,int b)
{
if(a>b)return a;
else return b;
}
void build(int l,int r,int root)
{
tree[root].l=l;
tree[root].r=r;
tree[root].len=W;
if(l==r){
return;
}
int mid=l+r>>1;
build(l,mid,root<<1);
build(mid+1,r,root<<1|1);
} void pushup(int root)
{
if(tree[root].l==tree[root].r)return;
tree[root].len=max(tree[root<<1].len,tree[root<<1|1].len);
}
void update(int root,int z)
{ if(tree[root].l==tree[root].r){
tree[root].len=tree[root].len-z;
printf("%d\n",tree[root].l); return ;
}
if(tree[root<<1].len>=z)update(root<<1,z); //假设左边能够,就往左插。
else if(tree[root<<1|1].len>=z)update(root<<1|1,z); //否则往右插。 pushup(root);
}
int main()
{
int i,j,k;
while(scanf("%d%d%d",&h,&W,&n)!=EOF)
{
if(h>n)h=n;
build(1,h,1);
for(i=1;i<=n;i++)
{
scanf("%d",&w[i]);
if(tree[1].len<w[i])printf("-1\n"); //假设1-h行里面都没有一行能够让该海报贴,就输出-1
else
update(1,w[i]);
}
}
return 0;
}
hdu 2795 Billboard(线段树单点更新)的更多相关文章
- hdu 2795 Billboard 线段树单点更新
Billboard Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=279 ...
- HDU 2795 Billboard (线段树单点更新 && 求区间最值位置)
题意 : 有一块 h * w 的公告板,现在往上面贴 n 张长恒为 1 宽为 wi 的公告,每次贴的地方都是尽量靠左靠上,问你每一张公告将被贴在1~h的哪一行?按照输入顺序给出. 分析 : 这道题说明 ...
- HDU 2795 Billboard 线段树,区间最大值,单点更新
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU 2795 Billboard (线段树+贪心)
手动博客搬家:本文发表于20170822 21:30:17, 原地址https://blog.csdn.net/suncongbo/article/details/77488127 URL: http ...
- HDU 2795 Billboard (线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题目大意:有一块h*w的矩形广告板,要往上面贴广告; 然后给n个1*wi的广告,要求把广告贴 ...
- HDU 3308 LCIS(线段树单点更新区间合并)
LCIS Given n integers. You have two operations: U A B: replace the Ath number by B. (index counting ...
- ACM学习历程—HDU 2795 Billboard(线段树)
Description At the entrance to the university, there is a huge rectangular billboard of size h*w (h ...
- hdu 5480 Conturbatio 线段树 单点更新,区间查询最小值
Conturbatio Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=54 ...
- [HDU] 2795 Billboard [线段树区间求最值]
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 2795 Billboard 线段树活用
题目大意:在h*w 高乘宽这样大小的 board上要贴广告,每个广告的高均为1,wi值就是数据另给,每组数组给了一个board和多个广告,要你求出,每个广告应该贴在board的哪一行,如果实在贴不上, ...
随机推荐
- P1824 进击的奶牛
题目描述 Farmer John建造了一个有N(2<=N<=100,000)个隔间的牛棚,这些隔间分布在一条直线上,坐标是x1,...,xN (0<=xi<=1,000,000 ...
- Android自定义组件系列【14】——Android5.0按钮波纹效果实现
今天任老师发表了一篇关于Android5.0中按钮按下的波纹效果实现<Android L中水波纹点击效果的实现>,出于好奇我下载了源代码看了一下效果,正好手边有一个Nexus手机,我结合实 ...
- Oracle修改表空间自增长
下面列出详细过程: 1.通过sql plus 命令登录数据库. 在命令行下输入sqlplus “登录用户名/口令 as 登录类型”就可以登录,系统内建的用户名常用的是sys,密码是在安装oracle过 ...
- rowcount和@@rowcount的区别
1 rowcount rowcount的作用就是用来限定后面的sql在返回指定的行数之后便停止处理,比如下面的示例, set rowcount 10select * from 表A 这样的查询只会返回 ...
- 局域网内机器不能对ping问题
局域网内有台A机器能ping同其他机器,但其他机器不能ping A机器.属于同一个网段 解决办法: cmd->msconfig->常规->诊断启动,仅加载基本设备和服务. 重启后试下 ...
- ArcGIS Engine中删除要素的几种方法总结
转自原文 ArcGIS Engine中删除要素的几种方法总结 /// <summary> /// 通过IFeature.Delete方法删除要素 /// </summary> ...
- [Python] Understand List Comprehensions in Python
List comprehensions provide a concise way to create new lists, where each item is the result of an o ...
- (诡异Floyd&自环)MZ Training 2014 #15 E题(POJ 2240)
你们见过这么诡异的FLOYD吗? 先上题. [Description] 货币的汇率存在差异.比如,如果1美元购买0.5英镑,1英镑买10法郎.而1法国法郎买0.21美元.然后,通过转换货币,一个聪明的 ...
- QTP校验数据库中数据后台项目
数据校验功能后台主要包含两个类:QTPCommonServlet.java和QTPCommonDao.java 当中QTPCommonServlet.java为: package com.runqia ...
- AVEVA PDMS Text Tool
AVEVA PDMS Text Tool eryar@163.com 网上有个文字工具插件,可以在PDMS中创建三维的字母.数字,不过不能创建中文.所以开发一个小工具,可以在PDMS中创建任意文字,如 ...