线段树(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 ...
随机推荐
- cura-engine学习(3)
目前,我们还在函数prepareModel中徘徊,因为这函数实在是太长了,近乎包含了整个数据处理过程.通过前面两篇,几何图形已经被导入到内存中,并且由一个simplemodel变成了一个optimiz ...
- C++ 11和C++98相比有哪些新特性
此文是如下博文的翻译: https://herbsutter.com/elements-of-modern-c-style/ C++11标准提供了许多有用的新特性.这篇文章特别针对使C++11和C++ ...
- Spring框架(4)---AOP讲解铺垫
AOP讲解铺垫 不得不说,刚开始去理解这个Aop是有点难理解的,主要还是新的概念比较多,对于初学者一下子不一定马上能够快速吸收,所以我先对什么事Aop做一个解释: 首先说明:本文不是自己所写 ...
- Struts2环境的搭建
1. Struts2 获取http://struts.apache.org/download.cgiStruts-2.3.16.3-all.zip 了解主要目录 apps :该文件夹下包含了基于Str ...
- 好公司、行业、领导?应届生应根据什么选offer?
两个年轻人大学毕业了,一个去了收入更高的大企业工作,一个去了收入较低的小作坊式工厂工作.你们说他们谁的青春时光最能升值呢?表面上看应该是大企业,可是大企业是做马车制造的,小作坊是做汽车的.现在人们都知 ...
- How to build mscorlib.dll with visual studio
Recently, Microsoft Corportation has released a new look for .NET Reference Source. And you may find ...
- Spring总结_04_容器和bean
一.概念理解 1.容器 IoC容器负责容纳并管理bean,在Spring中,BeanFactory是IoC容器的核心接口. 它的职责包括:实例化.定位.配置应用程序中的对象及建立这些对象间的依赖. ...
- 1751: [Usaco2005 qua]Lake Counting
1751: [Usaco2005 qua]Lake Counting Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 190 Solved: 150[Su ...
- 动态规划略有所得 数字三角形(POJ1163)
在上面的数字三角形中寻找一条从顶部到底边的路径,使得路径上所经过的数字之和最大.路径上的每一步都只能往左下或 右下走.只需要求出这个最大和即可,不必给出具体路径. 三角形的行数大于1小于等于100,数 ...
- VMware10虚拟机安装mac os x 10.9
身边越来越多盆友们购进mac本子,不断地诱惑着小编,近日一直想着买一台mac本子装逼,后来想想用来开发(非ios)可能会遇着各种问题,就先在虚拟机上玩玩看,毕竟1,2万RMB不是小数. 在mac os ...