Billboard
Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13179 Accepted Submission(s): 5695
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
Sample Output
Author
hhanger@zju
Source
HDOJ 2009 Summer Exercise(5)
Recommend
lcy | We have carefully selected several similar problems for you: 1698 1542 1828 1540 1255
一个宽和高分别为w,h信息板,向上添加信息,要求每条信息的位置尽量靠上,并且从左到右排列,问每一条信息如果能添加上,它会处于第几行。
用线段树模拟,线段树叶子节点的标号表示行号,从左往右行号依次增加,对于一个左右区间分别为l,r的节点,它所存储的信息是从l行到r行中的最大剩余宽度。
我们将插入信息和查询行号的工作合并一下,在插入的时候,因为要尽量选择行号较小的,所以优先选择左子树,看左子树的最大值是否大于等于所要插入的信息的宽度,如果不满足就查询右子树,找到对应的叶子节点后,要将该叶子节点的宽度减去所插入信息的宽度,并且返回节点的标号,即行号,然后向上更新每个节点的最大值。
这里有一个需要优化的地方,因为信息板的高度可能非常大,但我们建树的数组不可能设那么大。可以这样想,如果信息板的宽度大于等于信息的数目,那么最差的情况就是每条信息占用一行,对于无法插入的信息,一定是因为它的宽度超过了信息板的宽度,所以,就算增加行数也无济于事,那么就可以直接将h设为n,从而将空间复杂度降低。
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <iomanip>
#include <algorithm>
#include <memory.h>
#define MAX 800000
using namespace std;
int tree[MAX];//每个区间记录某些行中的最大剩余宽度
void PushUp(int rt)
{
tree[rt]=max(tree[rt<<1],tree[rt<<1|1]);
}
void Build(int l,int r,int rt,int w)
{
tree[rt]=w;//初始的时候所有行的最大宽度都是w
if(l==r)
{
return;
}
int m=(l+r)>>1;
Build(l,m,rt<<1,w);
Build(m+1,r,rt<<1|1,w);
}
int Query(int wi,int l,int r,int rt)
{
if(l==r)
{
tree[rt]-=wi;
return l;
}
int m=(l+r)>>1;
int ans=(tree[rt<<1]>=wi)?Query(wi,l,m,rt<<1):Query(wi,m+1,r,rt<<1|1);//优先找左子树,因为较小的行号意味着更靠上
PushUp(rt);//向上更新最大值
return ans;
}
int main()
{
int h,w,n,wi;
while(~scanf("%d%d%d",&h,&w,&n))
{
if(h>n)//空间优化,当h>n的时候,意味着最差也就是每个信息占用一行,那么对于一条信息来说,如果宽度小于w
h=n;//肯定能放开,否则肯定放不开,所以这种情况最多使用n行
Build(1,h,1,w);//每个叶子节点表示行号,从左到右依次增大
while(n--)
{
scanf("%d",&wi);
if(tree[1]<wi)//如果所有行中的最大宽度都小于这条信息的宽度,肯定放不下
{
printf("-1\n");
continue;
}
printf("%d\n",Query(wi,1,h,1));//查询,顺便更新
}
}
return 0;
}