Billboard
Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15595 Accepted Submission(s): 6571
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.
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.
3 5 5 2 4 3 3 3
1 2 1 3 -1
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795
题目大意:每张广告都是高度为1宽度为w的细长的矩形纸条。贴广告的人总是会优先选择最上面的位置来帖,而且在所有最上面的可能位置中,他会选择最左面的位置,而且不能把已经贴好的广告盖住。 如果没有合适的位置了,那么这张广告就不会被贴了。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
int const maxn=800005;
int tr[maxn];
int h,w,p;
void Query(int l,int r,int x,int root)
{
if(l==r) //线段树的结构特点就是单点从前往后的,即高度是从上往下的
{
if(tr[root]-x>=0) //此行可以贴纸条,更新单点
{
tr[root]-=x;
printf("%d\n",l);
p=1; //标记已贴过纸条
}
return ;
}
int mid=(l+r)/2;
if(!p&&x<=tr[root*2])
Query(l,mid,x,root*2);
if(!p&&x<=tr[root*2+1])
Query(mid+1,r,x,root*2+1);
tr[root]=max(tr[root*2+1],tr[root*2]);
}
int main(void)
{
int x,n;
while(scanf("%d%d%d",&h,&w,&n)!=EOF)
{
h=h>n?n:h; //不加这句会RE
for(int i=0;i<maxn;i++)
tr[i]=w;
for(int i=0;i<n;i++)
{
p=0;
scanf("%d",&x);
if(tr[1]<x)
printf("-1\n");
else
Query(1,h,x,1);
}
}
}
479



被折叠的 条评论
为什么被折叠?



