HDU2795 billboard【转化为线段树。】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795
hhanger大神的题目,水题都得有点思维。
题意:h*w的木板,放进一些1*L的物品,求每次放空间能容纳且最上边的位子
思路:每次找到最大值的位子,然后减去L
线段树功能:query:区间求最大值的位子(直接把update的操作在query里做了)
技巧挺好,一开始自己思路建个超大二维数组,显然内存不够。
然后。 线段树的话其实就是深搜,if(l==r) 返回的肯定是最左边的结点,哈~
判断的时候直接用Max[1]与p比较就能判断是否输出-1,赞一个!!
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std; #define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r int h,w,n; const int maxn=200005;// 是200000还是20000要搞清楚
int Max[maxn<<2]; //开maxn的多少倍结合最底层的结点数加以注意 void Pushup(int rt)
{
Max[rt]=max(Max[rt<<1],Max[rt<<1|1]);
}
void build(int rt,int l,int r)
{
Max[rt]=w;
if(l==r) return;
int m=(l+r)>>1;
build(lson);
build(rson);
}
int query(int p,int rt,int l,int r)//技巧5 . update放在query里面了
{
if(l==r)
{
Max[rt]-=p;
return l;// 2. 返回行数
} int m=(l+r)>>1;
int ret;
ret=(Max[rt<<1]>=p)?query(p,lson):query(p,rson);// 技巧1.这地方深搜返回最左边的结点即符合的。
Pushup(rt);// 技巧4 . 自己写这个地方有可能会忘
return ret;
} int main()
{
while(scanf("%d%d%d",&h,&w,&n)!=EOF)
{
if(h>n) {h=n;}
build(1,1,h); int p;
for(int i=1;i<=n;i++)
{
scanf("%d",&p);
if(Max[1]<p) printf("-1\n"); // 技巧3.Max[1]最根结点一定是最大值
else
{
int ans=query(p,1,1,h);
printf("%d\n",ans);
}
}
}
return 0;
}
HDU2795 billboard【转化为线段树。】的更多相关文章
- BNU 28887——A Simple Tree Problem——————【将多子树转化成线段树+区间更新】
A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on ZJU. O ...
- ACM: Billboard 解题报告-线段树
Billboard Time Limit:8000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descript ...
- [CERC2017]Intrinsic Interval——扫描线+转化思想+线段树
[CERC2017]Intrinsic Interval https://www.luogu.org/blog/ywycasm/solution-p4747# 这种“好的区间”,见得还是比较多的了. ...
- 线段树详解 (原理,实现与应用)(转载自:http://blog.csdn.net/zearot/article/details/48299459)
原文地址:http://blog.csdn.net/zearot/article/details/48299459(如有侵权,请联系博主,立即删除.) 线段树详解 By 岩之痕 目录: 一:综述 ...
- bzoj2124 等差子序列(hash+线段树)
2124: 等差子序列 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 719 Solved: 261[Submit][Status][Discuss] ...
- 线段树(build,insert,dfs操作)
模板原型: 解决零散数点在已知线段上的出现次数.思想是将线段用长线覆盖,将长线转化成线段树.用权值记录各个数点出现的次数,最后进行查询.代码解释见注释. #include <bits/stdc+ ...
- ●线段树的三个题(poj 3225,hdu 1542,hdu 1828)
●poj 3225 Help with Intervals(线段树区间问题) ○赘述题目 给出以下集合操作: 然后有初始的一个空集S,和以下题目给出的操作指令,并输入指令: 要求进行指令操作后,按格式 ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- ZOJ 3686 A Simple Tree Problem(线段树)
Description Given a rooted tree, each node has a boolean (0 or 1) labeled on it. Initially, all the ...
随机推荐
- [C#参考]细说进程、应用程序域与上下文之间的关系
原文转载链接:http://www.cnblogs.com/leslies2/archive/2012/03/06/2379235.html Written by:风尘浪子 引言 本文主要是介绍进程( ...
- (Problem 92)Square digit chains
A number chain is created by continuously adding the square of the digits in a number to form a new ...
- Qt信息隐藏(Q_D/Q_Q)介绍——从二进制兼容讲起
http://www.cnblogs.com/SkylineSoft/articles/2046404.html
- 如何使用picasso 对Android图片下载缓存
相比较其他,picasso的图片缓存更加简单一些,他只需要一行代码就可以表述:导入相关jar包 Picasso.with(context).load("图片路径").into(Im ...
- perl 爬取上市公司业绩预告
<pre name="code" class="python">use LWP::UserAgent; use utf8; use DBI; use ...
- DrawerLayout、CoordinatorLayout、CollapsingToolbarLayout的使用--AndroidSupportDesign练手
先po一张效果图 PS:原谅题主的懒惰吧.. 看着是不是很酷炫,那是因为5.0的动画做得好,代码其实没有多少,搞清楚这个布局的层次关系很重要. 废话不多说了,先来看布局文件 最外层是一个DrawerL ...
- Juicy Couture_百度百科
Juicy Couture_百度百科 Juicy Couture
- 封装的localstorge的插件,store.js
封装的localstorge的插件,store.js https://github.com/marcuswestin/store.js/
- js中this指向问题
1.在全局范围内使用this的时候 它指向的是全局对象 this Window {top: Window, window: Window, location: Location, external: ...
- 关于给javascript对象添加、删除、修改对象的属性
以下是自己总结的几种方法 利用动态特性 function Person(){}; var person = new Person(); person.name = 'yy'; person.gende ...