Billboard
Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16318 Accepted Submission(s): 6914
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.
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.
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 的公告板,这里认为每个公告所占空间为1*wi ,然后一个新的公告张贴时首先考虑往最高处贴,在同一高度,优先考虑往左边贴,若没有可用的空间,则不贴。输出输入的每个宽度的公告所张贴位置的高度,没有贴的输出-1.
#include <iostream>
#include<cstdio>
#include<cstring>
#define maxn 200000
using namespace std;
int h,w,n;
struct lnode
{
int left,right,val;//left和right为高度的上下值,val为相应高度区间内最大的剩余宽度
};
lnode node[maxn<<2];
int maxi(int a,int b)
{
return a>b?a:b;
}
void pushup(int o)
{
node[o].val=maxi(node[o<<1].val,node[o<<1|1].val);
}
void build(int o,int l,int r)
{
node[o].left=l;
node[o].right=r;
node[o].val=w;//开始时所用高度点的宽度都为w
if(l==r)
return ;
int mid=(l+r)>>1;
build(o<<1,l,mid);
build(o<<1|1,mid+1,r);
}
int query(int o,int aim)
{
if(node[o].left==node[o].right)
{
node[o].val-=aim;
return node[o].left;//返回值为板的高度值
}
int mid=(node[o].left+node[o].right)>>1;
int ans=0;
if(aim<=node[o<<1].val)
ans=query(o<<1,aim);
else ans=query(o<<1|1,aim);
pushup(o);
return ans;
}
int main()
{
int q;
while(~scanf("%d%d%d",&h,&w,&n))
{
if(h>n)
h=n;//当h小于n时,所放的公告由板高决定,当大于时,最多使用板的n 高
build(1,1,h);
while(n--)
{
scanf("%d",&q);
if(q>node[1].val)//若公告的宽度大于当前所剩下的最大宽度则不可放
printf("-1\n");
else
printf("%d\n",query(1,q));
}
}
return 0;
}