Billboard
Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7529 Accepted Submission(s): 3345
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
开始看到这道题。看到数据范围就凌乱了。就算离散化也搞不定。完全没思路了。去看了下别人的思路就顿悟了。这道题关键要转化。
思路:
因为每个announcement的高都是1,所以把一整块board 90度旋转,h为宽,w为高,线段树的叶子就是原来board的每一行。
注意一点,一开始看到board的长和宽有10^9这么多,还以为要离散化,其实是不用的,因为announcement贴上去以后不会被覆盖,所以最多就是有200,000张长度为10^9的announcement,即线段树最多只有200,000个叶子,所以建树的时候要在h和n之间取最小值。
树节点维护一个记录区间内最大空位长度的域,对于每个announcement,先通过query来判断可否贴上去贴的时候优先选择行数小及topmost possible position ,如果可以则update。
详细见代码:
#include <iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int maxn=200100;
int id[4*maxn],rest[4*maxn];//记录行号
int w,h,n;
void btree(int L,int R,int k)
{
int ls,rs,mid;
id[k]=L;//优先选最上面的行
rest[k]=w;
if(L==R)
return;
ls=k<<1;
rs=k<<1|1;
mid=(L+R)>>1;
btree(L,mid,ls);
btree(mid+1,R,rs);
}
void update(int L,int R,int l,int r,int k,int c)
{
int ls,rs,mid;
if(l==L&&r==R)
{
rest[k]-=c;
return;
}
ls=k<<1;
rs=k<<1|1;
mid=(L+R)>>1;
if(l>mid)
update(mid+1,R,l,r,rs,c);
else if(r<=mid)
update(L,mid,l,r,ls,c);
else
{
update(L,mid,l,mid,ls,c);
update(mid+1,R,mid+1,r,rs,c);
}
if(rest[ls]<rest[rs])//rest子结点剩余的最大空间
{
rest[k]=rest[rs];
id[k]=id[rs];
}
else//还是行号小的优先
{
rest[k]=rest[ls];
id[k]=id[ls];
}
}
int qu(int L,int R,int k,int cost)
{
int ls,rs,mid;
if(cost>rest[k])//如果没地方贴了
return -1;
if(L==R)
return id[k];
ls=k<<1;
rs=k<<1|1;
mid=(L+R)>>1;
if(cost<=rest[ls])//优先贴行号小的
return qu(L,mid,ls,cost);
else
return qu(mid+1,R,rs,cost);
}
int main()
{
int sz,i,ans;
while(~scanf("%d%d%d",&h,&w,&n))
{
if(n<h)
h=n;
btree(1,h,1);
for(i=1;i<=n;i++)
{
scanf("%d",&sz);
ans=qu(1,h,1,sz);
printf("%d\n",ans);
if(ans!=-1)
update(1,h,ans,ans,1,sz);
}
}
return 0;
}