题目链接:https://vjudge.net/problem/HDU-2795
转自:https://blog.csdn.net/weixin_43693379/article/details/98726378
题意:有一块h*w的广告牌,现有多个广告,每个广告都是1 * x的大小,总是往最上方放置,并且如果可能就往最左放置,输出放置的行.
思路:注意到h最大1e9,但是因为一个广告最多占一行,即h如果比n大,那么h=n。在查询之前首先判断能否放置。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
const int maxn=8e5+5;
int h,w,n;
struct node
{
int l,r,sum,lazy;
node()
{
l=r=sum=lazy=0;
}
} a[maxn];
void update(int k)
{
a[k].sum=max(a[2*k].sum,a[2*k+1].sum);
}
void build(int k,int l,int r)
{
a[k].l=l,a[k].r=r;
if(l==r)
{
a[k].sum=w;
return ;
}
int mid=(l+r)/2;
build(k*2,l,mid);
build(k*2+1,mid+1,r);
update(k);
}
void change(int k,int x,int tep)
{
if(a[k].l==a[k].r)
{
a[k].sum-=x;
return ;
}
int mid=(a[k].l+a[k].r)/2;
if(tep<=mid)
change(k*2,x,tep);
else
change(k*2+1,x,tep);
update(k);
}
int query(int k,int x)
{
if(a[k].l==a[k].r)
return a[k].l;
int mid=(a[k].l+a[k].r)/2;
if(a[k*2].sum>=x)
{
return query(k*2,x);
}
else
{
return query(k*2+1,x);
}
}
int main()
{
while(~ scanf("%d%d%d",&h,&w,&n))
{
if(h>n)
h=n;
build(1,1,h);
for(int i=0; i<n; i++)
{
int x;
scanf("%d",&x);
if(a[1].sum<x)
{
printf("-1\n");
continue;
}
int tep= query(1,x);
printf("%d\n",tep);
change(1,x,tep);
}
}
return 0;
}
来源:CSDN
作者:EHWWFFT.
链接:https://blog.csdn.net/weixin_43983247/article/details/104201174