hdu2795 Billboard
题意:h*w的木板,放进一些1*L的物品,求每次放 空间能容纳且最上边 的位子
题意:h*w的木板,放进一些1*L的物品,求每次放 空间能容纳且最上边 的位子
思路:每次找到最大值的位子,然后减去L
思路从hh博客看得 ^-^ http://www.notonlysuccess.com/index.php/segment-tree-complete/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define lson l,m,rt<<1 //函数用左儿子
#define rson m+1,r,rt<<1|1 //函数用右儿子
#define havemid int m=(l+r)>>1 //取节点中点
#define left (rt<<1) //左儿子
#define right (rt<<1|1) //右儿子
const int maxn=200100;
int Max[maxn<<2];
int h,w,n;
void pushup(int rt){
Max[rt]=max(Max[left],Max[right]);
}
void build(int l,int r,int rt){
Max[rt]=w;
if(l==r)return ;
havemid;
build(lson);
build(rson);
}
void update(int p,int x,int l,int r,int rt){
if(l==r){
Max[rt]-=x;
return ;
}
havemid;
if(p<=m)update(p,x,lson);
else update(p,x,rson);
pushup(rt);
}
int query(int x,int l,int r,int rt){
if(l==r){
return l;
}
havemid;
if(Max[left]>=x)return query(x,lson);
else return query(x,rson);
}
int main(){
int x,i,res;
while(scanf("%d%d%d",&h,&w,&n)!=EOF){
if(h>n)h=n;
build(1,h,1);
while(n--){
scanf("%d",&x);
if(Max[1]<x)printf("-1\n");
else {
res=query(x,1,h,1);
update(res,x,1,h,1);
printf("%d\n",res);
}
}
}
return 0;
}