题意: 有一块h*w的矩形广告板,要往上面贴广告; 然后给n个1*wi的广告,要求把广告贴上去; 而且要求广告要尽量往上贴并且尽量靠左; 求第n个广告的所在的位置,不能贴则为-1;
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=200010;
int a[maxn];
int h,w,n;
struct IntevalTree
{
int sum[maxn<<2];
void build(int o,int l,int r)
{
sum[o]=w;
if(l==r)return;
int mid=(l+r)/2;
build(o*2,l,mid);
build(o*2+1,mid+1,r);
}
int query(int o,int l,int r,int x)
{
if(l==r)
{
sum[o]-=x;
return l;
}
int mid=(l+r)/2;
int ans=sum[o*2]>=x?query(o*2,l,mid,x):query(o*2+1,mid+1,r,x);
sum[o]=max(sum[o*2],sum[o*2+1]);
return ans;
}
}tree ;
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d%d%d",&h,&w,&n)!=EOF)
{
int x;
if(h>n)h=n;
tree.build(1,1,h);
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
if(tree.sum[1]<x){printf("-1\n");}
else printf("%d\n",tree.query(1,1,h,x));
}
}
return 0;
}