Billboard

Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18408    Accepted Submission(s): 7716

Problem Description
At the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and w is its width). The board is the place where all possible announcements are posted: nearest programming competitions, changes in the dining room menu, and other important information.

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.

 
Input
There are multiple cases (no more than 40 cases).

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.

 
Output
For each announcement (in the order they are given in the input file) output one number - the number of the row in which this announcement is placed. Rows are numbered from 1 to h, starting with the top row. If an announcement can't be put on the billboard, output "-1" for this announcement.
 
Sample Input
3 5 5
2
4
3
3
3
 
Sample Output
1
2
1
3

-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)的更多相关文章

  1. 主席树[可持久化线段树](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 ...

  2. 最大矩阵覆盖权值--(静态连续最大子段 (线段树) )-HDU(6638)Snowy Smile

    这题是杭电多校2019第六场的题目 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6638 题意:给你平面上n个点,每个点都有权值(有负权),让你计算一 ...

  3. 敌兵布阵(线段树HDU 1166)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submissi ...

  4. HDU 6464 权值线段树 && HDU 6468 思维题

    免费送气球 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  5. 区间第k大问题 权值线段树 hdu 5249

    先说下权值线段树的概念吧 权值平均树 就是指区间维护值为这个区间内点出现次数和的线段树 用这个加权线段树 解决第k大问题就很方便了 int query(int l,int r,int rt,int k ...

  6. 线段树 HDU 3397(真)

    5 种操作  0 1 然后 异或 似乎这种2个更新的先后每次都搞不清 覆盖有覆盖就可以不异或 也不知道为什么 #include<stdio.h> #include<string.h& ...

  7. 线段树 HDU 3397

    5种操作 具体看代码 #include<iostream> #include<stdio.h> #include<string.h> #include<alg ...

  8. 线段树 HDU 3308

    t 题目大意:给你n个数,m个操作.操作有两种:1.U x y 将数组第x位变为y   2. Q x y 问数组第x位到第y位连续最长子序列的长度.对于每次询问,输出一个答案 #include< ...

  9. 二维线段树 HDU 1823最简单的入门题

    xiaoz 征婚,首先输入M,表示有M个操作. 借下来M行,对每一行   Ih a l     I 表示有一个MM报名,H是高度, a是活泼度,L是缘分. 或   Q h1 h2 a1 a2    求 ...

  10. bzoj 3038: 上帝造题的七分钟2 线段树||hdu 4027

    3038: 上帝造题的七分钟2 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 1066  Solved: 476[Submit][Status][Dis ...

随机推荐

  1. 第35篇 IIS执行原理

    服务器的监听(IIS6.0+版本) 当请求到达服务器时,请求最终会到达TCPIP.SYS驱动程序,TCPIP.SYS将请求转发给HTTP.SYS网络驱动程序的请求队列中(可以理解为专门处理http请求 ...

  2. js substr和substring的区别

    在js中substring和substr都是用来截取字符串的,substr函数和substring函数都是用来从某个“母字符串”中提取“子字符串”的函数.但用法有些差别,下面分别介绍但是它们还是有区别 ...

  3. Gradle之恋-任务1

    任务作为Gradle的核心功能模块,而且Gradle的任务还可以具有自己的属性和方法,大大扩展了Ant任务的功能.由于任务相关内容比较多,分为两篇来探讨,本篇主要涉及到:任务的定义.任务的属性.任务的 ...

  4. Java日志工具之java.util.logging.Logger

    今天总结下JDK自带的日志工具Logger,虽然它一直默默无闻,但有时使用它却比较方便.更详细的信息可以查看JDK API手册,本文只是简单示例入门. 创建Logger 我们可以使用Logger的工厂 ...

  5. wemall app商城源码Android中ViewHolder详细解释

    1.ViewHolder的解释: (1).只是一个静态类,不是Android的API方法. (2).它的作用就在于减少不必要的调用findViewById,然后把对底下的控件引用存在ViewHolde ...

  6. app 评分

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #822e0e } p.p2 { margin: 0.0px 0. ...

  7. a中的类型转换

    自动类型转换 当然自动类型转换是需要满足特定的条件的: 1.  目标类型能与源类型兼容,如 double 型兼容 int 型,但是 char 型不能兼容 int 型. 2.  目标类型大于源类型,如 ...

  8. 多个Activity交互的生命周期:

    一.多个Activity交互的生命周期: A Activity打开B Activity的时候:        A Activity                    B Activity     ...

  9. 用tp框架来对数据库进行增删改

    先来看添加 使用tp框架,对数据库进行添加操作,都有哪些方法 先在Main控制器中,做个方法 运行一下,注意地址,就要输tianjia了 然后再看一下数据库,有没有添加上数据 添加成功 再来看一下这个 ...

  10. Git环境搭建以及上传到GitHub全文记录

    1.百度搜索下载git,官网可能很慢,我在百度软件里面下载的.当然可能不是最新版本.一路回车安装就好. 2.设置本机git的用户名和邮箱地址 查看当前电脑是否设置了用户名称 $ git config ...