HDU 2795 Billboard(宣传栏贴公告,线段树应用)
HDU 2795 Billboard(宣传栏贴公告,线段树应用)
ACM
题目地址:HDU 2795 Billboard
题意:
要在h*w宣传栏上贴公告,每条公告的高度都是为1的,并且每条公告都要尽量贴最上面最靠左边的,给你一系列的公告的长度,问它们能不能贴上。
分析:
不是非常好想,只是想到了就非常好写了。
仅仅要把宣传栏倒过来就好办了,这时候就是变成有h条位置能够填公告,填放公告时就能够尽量找最左边的合适的位置来放了。
能够用线段树实现,查找的复杂度是O(logn),须要注意的坑点是h的范围很大,假设真的那么大线段树是开不下去的,可是n的范围才20w,而即使全部公告都要占一栏也不会超过n,所以线段树开min(h, n)即可了。
代码:
/*
* Author: illuz <iilluzen[at]gmail.com>
* Blog: http://blog.csdn.net/hcbbt
* File: 2795.cpp
* Create Date: 2014-08-05 16:12:47
* Descripton: segment tree
*/ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
#define repf(i,a,b) for(int i=(a);i<=(b);i++) #define lson(x) ((x) << 1)
#define rson(x) ((x) << 1 | 1) typedef long long ll; const int N = 200010;
const int ROOT = 1; int h, w, n, t; // below is sement point updated version
struct seg {
ll w;
}; struct segment_tree {
seg node[N << 2]; void update(int pos) {
node[pos].w = max(node[lson(pos)].w, node[rson(pos)].w);
} void build(int l, int r, int pos) {
if (l == r) {
node[pos].w = w;
return;
}
int m = (l + r) >> 1;
build(l, m, lson(pos));
build(m + 1, r, rson(pos));
update(pos);
} int queryandmodify(int l, int r, int pos, ll y) {
if (y > node[pos].w) {
return -1;
}
if (l == r) {
node[pos].w -= y;
return l;
}
int m = (l + r) >> 1;
int res;
if (y <= node[lson(pos)].w)
res = queryandmodify(l, m, lson(pos), y);
else
res = queryandmodify(m + 1, r, rson(pos), y);
update(pos);
return res;
} } sgm; int main() {
while (~scanf("%d%d%d", &h, &w, &n)) {
h = min(h, n);
sgm.build(1, h, ROOT);
repf (i, 1, n) {
scanf("%d", &t);
printf("%d\n", sgm.queryandmodify(1, h, ROOT, t));
}
}
return 0;
}
HDU 2795 Billboard(宣传栏贴公告,线段树应用)的更多相关文章
- hdu 1698:Just a Hook(线段树,区间更新)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 5274 Dylans loves tree(LCA + 线段树)
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- HDU 3074.Multiply game-区间乘法-线段树(单点更新、区间查询),上推标记取模
Multiply game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)
HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意: 给一个序列由 ...
- HDU 2795 Billboard 【线段树维护区间最大值&&查询变形】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=2795 Billboard Time Limit: 20000/8000 MS (Java/Others) ...
- hdu 2795 Billboard 线段树单点更新
Billboard Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=279 ...
- HDU 2795 Billboard (线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题目大意:有一块h*w的矩形广告板,要往上面贴广告; 然后给n个1*wi的广告,要求把广告贴 ...
- 线段树练习[单点更新] HDU 2795 Billboard
题目大意:有一个h*w的公告榜,可以依次在上面添加信息.每个信息的长度为x,高为1. 优先在最上面加入,如果空间足够的话,然后优先放在最左面.统计每条公告最终的位置,即它所在的行数. 这里是线段树来存 ...
- HDU 2795 Billboard (线段树)
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
随机推荐
- ubuntu18.04server 真机无法自动获取IP解决方法
输入命令ip a,查看自己网卡编号,比如我的就是ens33 因为此图为虚拟机搭建的,所以网卡名称为ens33,如果是真机的话则是enp0s**的名字 2.修改netwlpan文件 1 sudo vim ...
- java session cookie的使用
Cookie; Session; URL重写; cookie在J2EE项目中的使用,Java中把Cookie封装成了java.servlet.http.Cookie类.每个Cookie都是该Cooki ...
- Spring框架 (log4j :WARN No appenders could be found for logger log4j:WARN Please initialize the log4j system properly.)问题解决
Spring框架需要的jar包 1.Spring压缩包中的四个核心JAR包 beans .context.core 和expression 下载地址: https://pan.baidu.com/s/ ...
- 使用Auto Layout中的VFL(Visual format language)——代码实现自动布局
本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:api介绍 1.NSLayoutConstraint API NSL ...
- 二叉树的创建一数据结构一C++
#include <iostream> using namespace std; //二叉树结点typedef struct BitNode { cha ...
- HNOI2006 花仙子的魔法
题目描述 题解: 考试的时候手画打表,然后半个小时磨了个式子:$$f[i][j]=f[i-1][j-1]+f[i][j-1]$$ 交上去$A$的时候都蒙了. 考后才知道原因. 考虑$n$维空间内原来有 ...
- [LUOGU] P2187 小Z的笔记
看范围猜方程,应该是O(n)级别的 f[i]表示前i个合法的最小代价,转移需要枚举断点位置,O(n^2) f[i]表示前i个合法留下的最大个数,同时更新距离最近的26个字母的位置,O(n)转移 f[i ...
- css module
来源:CSS Modules 用法教程 后面项目地址:https://github.com/947133297/lwj-webpack-demo 关键是打开这一行,表示开启loader的css mod ...
- POJ 3468 A Simple Problem with Integers (线段树多点更新模板)
题意: 给定一个区间, 每个区间有一个初值, 然后给出Q个操作, C a b c是给[a,b]中每个数加上c, Q a b 是查询[a,b]的和 代码: #include <cstdio> ...
- luogu1856 [USACO5.5]矩形周长Picture
看到一坨矩形就要想到扫描线.(poj atantis) 我们把横边竖边分开计算,因为横边竖边其实没有区别,以下论述全为考虑竖边的. 怎样统计一个竖边对答案的贡献呢?答:把这个竖边加入线段树,当前的总覆 ...