HDU2795

来源:互联网 发布:淘宝几块钱的硅脂 编辑:程序博客网 时间:2024/05/08 10:44

HDU2795维护区间剩余长度的最大值

Billboard

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

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

 

这个题的翻译:

每张广告都是高度为1宽度为wi的细长的矩形纸条。贴广告的人总是会优先选择最上面的位置来帖,而且在所有最上面的可能位置中,他会选择最左面的位置,而且不能把已经贴好的广告盖住。 如果没有合适的位置了,那么这张广告就不会被贴了。 现在已知广告牌的尺寸和每张广告的尺寸,求每张广告被贴在的行编号。

Input

多组样例,不超过40个。对每组样例,第一行包含3个整数h,w,n(1 <= h,w <= 10^9; 1 <= n <= 200,000) -广告牌的尺寸和广告的个数。 下面n行每行一个整数 wi (1 <= wi <= 10^9) -  第i张广告的宽度.

Output

对每张广告,输出它被贴在的行编号(是1到h之间的数),顶部是第一行。如果某广告不能被贴上,则输出-1。

 

这道题和上面两个题目的做法稍微发生了一些变化,这道题要维护的内容是每个区间剩余的长度的最大值,而操作时只需要输入对应区间输出第几行,所以本题先建树,在后续输入区间操作时选择边输入边更新区间,并且返回所在的最高行数。

#include<iostream>#include<stdio.h>#include<algorithm>#include<string.h>using namespace std;#define Max 200005int w;int tree[Max<<2];void build(int p,int l,int r){tree[p]=w;if(l==r)return;int mid=(l+r)>>1;build(p<<1,l,mid);build(p<<1|1,mid+1,r);//本题建树只需要把每个节点先都赋值为每行广告位的最大长度}int update(int p,int l,int r,int board){if(l==r){tree[p]-=board;return l;}int mid=(l+r)>>1;int ret;//ret代表需要返回的行数if(tree[p<<1]>=board) ret=update(p<<1,l,mid,board);    //如果左子树的最大剩余值大于需要插入的广告的长度,则在左子树中继续查找更新else ret=update(p<<1|1,mid+1,r,board);//否则就在右子树中查找更新tree[p]=max(tree[p<<1],tree[p<<1|1]);//父亲节点的值为左右儿子中剩余区间的最大值return ret;//函数最后返回需要查找的行数}int main(){int h,n,a;while(scanf("%d%d%d",&h,&w,&n)!=EOF){if(h>n) h=n;build(1,1,n);while(n--){scanf("%d",&a);if(tree[1]<a)printf("-1\n");elseprintf("%d\n",update(1,1,h,a));}}return 0;}


0 0
原创粉丝点击