博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 2795 Billboard 线段树
阅读量:7222 次
发布时间:2019-06-29

本文共 1424 字,大约阅读时间需要 4 分钟。

  题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2795

  题目描述: 一个h * w的木板, 要将n个1 * L[i]的木条放进去, 要求的优先最上面, 优先最左面, 输出放入每一个木条应放入的行号

  解题思路: 查询区间最大值, 并且尽可能的向靠上的(小号开头的)区间中放置, 其实我们并不关心到底是否向左放, 我们只需要保证一旦有空就靠边放就行, 所以用一个减法就完全可以表示了, 我们只需要用tree[i] 表示第i行还剩余的容量, 放置时更新最大值就可以了

  代码: 

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define lson l, m, rt<<1#define rson m+1, r, rt<<1|1using namespace std;int h, w, n;const int treen = 222222;int tree[treen<<2];void Pushup(int rt) { tree[rt] = max( tree[rt<<1], tree[rt<<1|1] );}void build( int l, int r, int rt ) { tree[rt] = w; if( l == r ) return; int m = (l + r) >> 1; build( lson ); build( rson ); return;}int query( int x, int l, int r, int rt ) { if( l == r ) { tree[rt] -= x; return l; } int m = (l + r) >> 1; int ret = (tree[rt<<1] >= x) ? query(x , lson) : query(x , rson); Pushup(rt); return ret;}int main() { while( ~scanf( "%d%d%d", &h, &w, &n ) ) { if( h > n ) h = n; build(1, h, 1); while( n-- ) { int L; scanf( "%d", &L ); if( tree[1] < L ) printf( "-1\n" ); else { int ret = query(L, 1, h, 1); printf( "%d\n", ret ); } } } return 0;}
View Code

  思考: 自己码这个代码的时候特别不专心, 然后现在我接触到的线段树一个是求 最大值, 一个是求和, 然而以后肯定各种恶心的树层出不穷, 先把线段树学好, 然后进程溢出首先检查自己的输入......

转载于:https://www.cnblogs.com/FriskyPuppy/p/7294559.html

你可能感兴趣的文章
SpaceX发射机密间谍卫星,系与美国防部签订的第一单合作
查看>>
亚马逊推出FreeTime Android应用程序,开放适合儿童资源
查看>>
Python1
查看>>
jquery.idTabs使用方法
查看>>
需求分析详细设计概要设计说明书部分样本
查看>>
数字货币交易系统火爆的背后是政策的大力支持
查看>>
gulp与webpack的区别
查看>>
ORA-12547:TNS:lost contact 问题分析思路
查看>>
解决firefox疯狂读硬盘的问题
查看>>
清华产业十大创新项目评选 新华三H3Cloud OS夺冠
查看>>
事务操作的统计,TPS的计算,隔离级别的读提交
查看>>
转贴:Ms Sql Server 2008 集成 SP1的方法!!!
查看>>
Memcache监控工具 -- memcache-top
查看>>
3-9 读写缓存流 ——BufferedStream类
查看>>
linux head
查看>>
Oracle osw监控工具的使用示例
查看>>
在Spring中整合JUnit单元测试
查看>>
Namenode双机热备之Pacemaker
查看>>
Xcode调试断点不停止解决方案!
查看>>
CentOS6.6+Puppet3.7.4分布式部署Nagios监控系统
查看>>