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

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

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
#include
#include
using namespace std;int h,w,n,m;struct node{ int l,r,the_rest; //保存这个区间的 最大剩余 }data[200000*4];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_)/2; built(l_,mid,i*2); built(mid+1,r_,i*2+1); data[i].the_rest=max(data[i*2].the_rest,data[i*2+1].the_rest); }}int insert(int len,int i) //开始自己想的是 用一个flag 来标记是否已经找到可以插入 来避免后面的多次插入 //但是后面看了下别人的代码发现 还是这种好 左子树没有找到再找右子树 { if(data[i].the_rest
=len) { data[i].the_rest-=len; return data[i].l; } int a=insert(len,i*2); if(a==-1){ a=insert(len,i*2+1); } data[i].the_rest=max(data[i*2].the_rest,data[i*2+1].the_rest); return a;}int main(){ while(~scanf("%d%d%d",&h,&w,&n)) { built(1,min(h,n),1); while(n--) { scanf("%d",&m); cout<
<

 

 

转载于:https://www.cnblogs.com/Swust-lyon/p/6702303.html

你可能感兴趣的文章
8 款为 WordPress 文章生成缩略图的插件
查看>>
bzoj1396 识别子串
查看>>
Android开发六:常用控件3--ListView(一)
查看>>
Wormholes (bellman)
查看>>
[ 产品经理 ] 产品经理的一天工作内容
查看>>
依赖倒转原则
查看>>
Dubbo分布式服务框架入门(附project)
查看>>
A2-02-14.DML- MySQL LEFT JOIN
查看>>
char与CString相互转换
查看>>
jQuery Ajax总结
查看>>
制作Visual Studio 2019 (VS 2019) 离线安装包
查看>>
JavaScript高级程序设计学习笔记--变量、作用域和内存问题
查看>>
Smack 结合 Openfire服务器,建立IM通信,发送聊天消息
查看>>
所闻所获4:下拉刷新控件2
查看>>
程序猿,你为什么须要一台mac?
查看>>
Cygwin下vim按方向键出现ABCD;
查看>>
android用shape画虚线,怎么也不显示
查看>>
javascript小白学习指南0---1
查看>>
【求助】怎样实如今并肩看中增加代码啊
查看>>
创业路(VC Pipeline),创业需要融资的阅读
查看>>