很经典的线段树题目,只需要每次找尽量靠上并且能放下这块板的位置就行了。
p.s:注意(1 <= h,w <= 10^9),如果h>n,h等于n就行了,因为最多只有n块板。
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define For(x, y) for(int i=x; i<=y; i++)
#define Lson l,mid,k<<1
#define Rson mid+1,r,k<<1|1
#define Mid(x,y) (x+y)>>1
using namespace std;
const int M=200010;
struct Seb_tree
{
int val;
};
Seb_tree t[M*4];
int h, w, n;
int s;
void push_up(int k)
{
t[k].val=max(t[k*2+1].val, t[k*2].val);
}
void build(int l, int r,int k)
{
t[k].val=w;
if (l==r) return;
int mid=Mid(l,r);
build(Lson);
build(Rson);
}
void query(int x, int l, int r,int k)
{
if (l==r)
{
t[k].val-=x;
s=l;
return;
}
int mid=Mid(l,r);
if (x<=t[k*2].val) query(x,Lson);
else query(x, Rson);
push_up(k);
}
int main()
{
while (scanf("%d %d %d", &h, &w, &n)!=EOF)
{
if(h>n) h=n;
build(1,h,1);
For(1,n)
{
int x;
scanf("%d", &x);
if (x>t[1].val) printf("-1\n");
else
{
s=0;
query(x,1,h,1);
printf("%d\n", s);
}
}
}
return 0;
}