线段树(hdu 2795)
Billboard
Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18408 Accepted Submission(s): 7716
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.
-1
先说这个题的意思吧
一个高度为H, 宽度为 w 的黑板 现在有 n 条小道消息 每一个都只占用一行 但是长度不一定 所以我们就需要找到 能写下它的哪一行 然后输出行号 行号要要求最小。也就是尽量写在上面
这题最简单的思路就是 用一个数组来表示每一行剩余的长度 来判断能不能插入这个消息
但是一定会超时
这个时候我们就需要用线段树来优化 用线段树表示这个区间最长的剩余长度
#include<iostream>
#include<cmath>
#include<stdio.h>
using namespace std;
int h,w,n,m;
struct node
{
int l,r,the_rest; //保存这个区间的 最大剩余
}data[*];
void built(int l_,int r_,int i)
{
data[i].l=l_; data[i].r=r_;
if(l_==r_){
data[i].the_rest=w; return ;
}
else{
int mid=(l_+r_)/;
built(l_,mid,i*);
built(mid+,r_,i*+);
data[i].the_rest=max(data[i*].the_rest,data[i*+].the_rest);
}
} int insert(int len,int i) //开始自己想的是 用一个flag 来标记是否已经找到可以插入 来避免后面的多次插入
//但是后面看了下别人的代码发现 还是这种好 左子树没有找到再找右子树
{
if(data[i].the_rest<len) return -;
if(data[i].l==data[i].r&&data[i].the_rest>=len)
{
data[i].the_rest-=len; return data[i].l;
}
int a=insert(len,i*);
if(a==-){
a=insert(len,i*+);
}
data[i].the_rest=max(data[i*].the_rest,data[i*+].the_rest);
return a;
}
int main()
{
while(~scanf("%d%d%d",&h,&w,&n))
{
built(,min(h,n),);
while(n--)
{
scanf("%d",&m);
cout<<insert(m,)<<endl;
}
}
return ;
}
线段树(hdu 2795)的更多相关文章
- 主席树[可持久化线段树](hdu 2665 Kth number、SP 10628 Count on a tree、ZOJ 2112 Dynamic Rankings、codeforces 813E Army Creation、codeforces960F:Pathwalks )
在今天三黑(恶意评分刷上去的那种)两紫的智推中,突然出现了P3834 [模板]可持久化线段树 1(主席树)就突然有了不详的预感2333 果然...然后我gg了!被大佬虐了! hdu 2665 Kth ...
- 最大矩阵覆盖权值--(静态连续最大子段 (线段树) )-HDU(6638)Snowy Smile
这题是杭电多校2019第六场的题目 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6638 题意:给你平面上n个点,每个点都有权值(有负权),让你计算一 ...
- 敌兵布阵(线段树HDU 1166)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submissi ...
- HDU 6464 权值线段树 && HDU 6468 思维题
免费送气球 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- 区间第k大问题 权值线段树 hdu 5249
先说下权值线段树的概念吧 权值平均树 就是指区间维护值为这个区间内点出现次数和的线段树 用这个加权线段树 解决第k大问题就很方便了 int query(int l,int r,int rt,int k ...
- 线段树 HDU 3397(真)
5 种操作 0 1 然后 异或 似乎这种2个更新的先后每次都搞不清 覆盖有覆盖就可以不异或 也不知道为什么 #include<stdio.h> #include<string.h& ...
- 线段树 HDU 3397
5种操作 具体看代码 #include<iostream> #include<stdio.h> #include<string.h> #include<alg ...
- 线段树 HDU 3308
t 题目大意:给你n个数,m个操作.操作有两种:1.U x y 将数组第x位变为y 2. Q x y 问数组第x位到第y位连续最长子序列的长度.对于每次询问,输出一个答案 #include< ...
- 二维线段树 HDU 1823最简单的入门题
xiaoz 征婚,首先输入M,表示有M个操作. 借下来M行,对每一行 Ih a l I 表示有一个MM报名,H是高度, a是活泼度,L是缘分. 或 Q h1 h2 a1 a2 求 ...
- bzoj 3038: 上帝造题的七分钟2 线段树||hdu 4027
3038: 上帝造题的七分钟2 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 1066 Solved: 476[Submit][Status][Dis ...
随机推荐
- SIFT中的高斯模糊
高斯模糊是众多模糊算法中的一种,所谓的模糊,就是平滑图像,消除像素之间的差异,最容易想到的方法就是均值平滑. .均值模糊 均值模糊就是取目标像素周围像素的平均值.譬如 像素矩阵. |1|1|1| |1 ...
- Bootstrap学习-环境安装
1.<meta http-equiv="X-UA-Compatible" content="IE=edge"> 让IE运行最新的渲染模式. 2.&l ...
- Robot Framework自动化测试环境部署
文档版本:v1.0 作者:令狐冲 如有问题请发邮件到:1146009864@qq.com 使用Robot Framework框架(以下简称RF)来做自动化测试. 模块化设计 1.所需环境一览表 软件 ...
- wemall app商城源码中实现带图片和checkbox的listview
wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享其中实现带图片和checkbox的listvi ...
- ajax三级联动下拉菜单
ajax写三级联动,先写一个文件类吧,以后用的时候直接调用即可: 来找一张表: 实现: 中国地域的三级联动:省.市.区: 图: 说一下思路: (1)当用户选择省份的时候触发事件,把当前的省份的id通过 ...
- SEO-友情链接注意事项
为什么要专门给友链一个区域呢?由此就可以想象到友情链接对一个网站有多重要前期,网站没有权重的时候,跟别人换友链,人家基本是不会换的因为你网站没权重,加了友链他也获取不到权重,对网站没有多少好处一般我们 ...
- Lambda&Java多核编程-6-方法与构造器引用
在Lambda&Java多核编程-2-并行与组合行为一文中,我们对Stream<Contact>里的每一位联系人调用call()方法,并根据能否打通的返回结果过滤掉已经失效的项. ...
- BFS-基础简单的算法
前言 有时候,当你并不了解很多高级算法的时候,搜索不失为一种解决问题的好方法,而且很多高级算法有或多或少的会用到搜索或者搜索的思想.可见,搜索是一个基础并且必须要掌握的算法. 在这篇文章中,会对BFS ...
- javascript继承--原型链的 继承
作者的话:原型链是JavaScript中相当重要的一个知识点,这里我使用了函数结构图,来帮助我更好的理解 /* 原型链继承方式: 通过改变一个对象的原型对象的指向来继承另一个对象 原理: 我们知道,一 ...
- QQ互联 redirect uri is illegal(100010)的解决办法,很简单
我的地址栏内容是:http://openapi.qzone.qq.com/oauth/show?which=ConfirmPage&display=pc&response_type=c ...