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. 项目架构开发:数据访问层之Cache

    数据访问层简单介绍 数据访问层,提供整个项目的数据访问与持久化功能.在分层系统中所有有关数据访问.检索.持久化的任务,最终都将在这一层完成. 来看一个比较经典的数据访问层结构图 大概可以看出如下信息 ...

  2. 30分钟掌握 C#6

    1. 只读自动属性(Read-only auto-properties) C# 6之前我们构建只读自动属性: public string FirstName { get; private set; } ...

  3. 简单却又复杂的FizzBuzz面试编程问题

    写这篇文章主要是因为偶然看到一篇关于stackoverflow公司的面经中提到了一个有趣的面试编程问题,如题所述:FizzBuzz问题.原文引用如下: “在一些公平的考验之后,我发现那些因为代码而抓狂 ...

  4. 免费的ERP真的那么可怕?请少一些偏见!

    为什么说这个话题,因为看到了太多关于对免费ERP的偏见,也许这也只是那些卖软件的营销策略:从事这个行业的人,应该能看出其中的端倪,但是对于吃瓜群众来说,那就真是误导了... 所以今天发表下自己的看法, ...

  5. 前端布局常见IE6 bug的解决方法,清除浮动的几种方法以及各自的优缺点

    相信有很多前端的朋友再布局的时候经常面对IE6咬牙切齿,尤其是刚刚入行的朋友,在这里给大家一点常见问题的解决方案,希望对大家有所帮助 1)png24位的图片在iE6浏览器上出现背景,解决方案是做成PN ...

  6. Java I/O之NIO Socket

    PS:本文简单介绍下旧I/O和NIO下的Socket通讯,仅以UDP来示例. TCP/IP协议 首先简单回顾下TCP/IP协议 Application:应用程序:Socket:套接字:Host:主机: ...

  7. Oracle Developer Data Modeler项目实践 (转)

    http://www.Oracle.com/webfolder/technetwork/tutorials/obe/db/sqldevdm/r30/datamodel2moddm/datamodel2 ...

  8. ajax 实现页面加载和内容的删除

    ajax最大的好处就在于加载和删除的时候不会跳转页面,现在的网页大多都会选择用ajax来写,相比嵌入PHP代码来说减少了代码量,同时加载页面也会比较快,  下面是用ajax以数据库fruit表为例写的 ...

  9. Solr field alias

    Field alias Any field, function, or transformer can be displayed with a different name in the output ...

  10. wx模块小实例

    功能介绍: 查询数据库表数据,提取数据并显示 main.py(执行文件) #coding:gbk __author__ = 'Hito' import querySmscode import wx c ...