题目翻译:有一个h*w长度的木板,现在要向上贴n张广告,广告之间不能覆盖,且每一张广告的高度都为1,第i张广告的宽度为w[i]。并且贴广告的规则是
每次都铁道最左上方能够贴得下的位置。并输出该行行号。
解题方法:
用高度来构建线段树,记录left行到right行中的最大留白空间,然后每贴一个广告,就在线段树中插,如果左子树的留白长度大于当前要贴的广告长度,就先
查找左子树,因为题目要求贴最左上方,当找到贴的位置,则该行的新的剩余长度就在原值上减去当前长度。并更新整科线段树。
AC代码:
#include <iostream>
#include <stdio.h>
#define lchild left,mid,root<<1
#define rchild mid+1,right,root<<1|1
using namespace std;
const int maxn=1e8;
int Max[maxn<<2]; ///用来存放某些行区间所能存放的最大值
int height,width,n;
///构建线段树
void push_up(int root)
{
Max[root] = max(Max[root<<1],Max[root<<1|1]);
}
void build(int left,int right,int root)
{
Max[root] = width; ///初始,每个区间所剩余的最大长度都为width
if(left == right) return;
int mid = (left+right)>>1;
///递归构建左右子树
build(lchild);
build(rchild);
}
///查询并插入
int query(int w,int left,int right,int root)
{
if(left == right) ///则将该板插入到这一行,这一节点所剩余的宽度减去w
{
Max[root] -= w;
return left; ///返回木板所插入的行
}
int mid = (left+right)>>1;
int ans=0;
if(w<=Max[root<<1]) ///如果广告的宽度小于左区间的最大宽度,题目要求往左区间靠
ans = query(w,lchild);
else
ans = query(w,rchild);
push_up(root);
return ans;
}
int main()
{
while(~scanf("%d%d%d",&height,&width,&n))
{
if(n<height) ///所要贴的广告数小于高度,则线段树按n建,否则按height建立
{
height = n;
}
build(1,height,1); ///构建线段树
int w;
while(n--)
{
scanf("%d",&w);
if(Max[1]<w)
printf("-1\n");
else
{
int ans = query(w,1,height,1);
printf("%d\n",ans);
}
}
}
return 0;
}