Billboard
Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13382 Accepted Submission(s): 5770
On September 1, the billboard was empty. One by one, the announcements started being put on the billboard.
Each announcement is a stripe of paper of unit height. More specifically, the i-th announcement is a rectangle of size 1 * wi.
When someone puts a new announcement on the billboard, she would always choose the topmost possible position for the announcement. Among all possible topmost positions she would always choose the leftmost one.
If there is no valid location for a new announcement, it is not put on the billboard (that's why some programming contests have no participants from this university).
Given the sizes of the billboard and the announcements, your task is to find the numbers of rows in which the announcements are placed.
The first line of the input file contains three integer numbers, h, w, and n (1 <= h,w <= 10^9; 1 <= n <= 200,000) - the dimensions of the billboard and the number of announcements.
Each of the next n lines contains an integer number wi (1 <= wi <= 10^9) - the width of i-th announcement.
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 200005
#define ll root<<1
#define rr root<<1|1
#define mid (a[root].l+a[root].r)/2 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} int n;
int b[N];
int h, w;
int ans[N]; struct node{
int l, r, minw;
}a[N*]; void build(int l,int r,int root){
a[root].l=l;
a[root].r=r;
a[root].minw=;
if(l==r) return;
build(l,mid,ll);
build(mid+,r,rr);
}
int flag; void solve(int id,int root){
if(b[id]+a[root].minw>w) return; //若这个区间最小的高度+要贴报纸的高度大于木板的高度则不能贴
if(a[root].l==a[root].r){
ans[id]=a[root].l;
a[root].minw+=b[id];
flag=;
return ;
}
if(b[id]+a[ll].minw<=w) solve(id,ll); //优先贴最左边
else solve(id,rr);
a[root].minw=min(a[ll].minw,a[rr].minw);//向上更新
} main()
{
int i, j, k;
while(scanf("%d %d %d",&h,&w,&n)==){
for(i=;i<=n;i++) scanf("%d",&b[i]);
build(,min(n,h),);
for(i=;i<=n;i++){
if(b[i]>w){ //若b[i]>w,显然不能贴
ans[i]=-;continue;
}
flag=;
solve(i,);
if(!flag) ans[i]=-; //若不能贴
}
for(i=;i<=n;i++) printf("%d\n",ans[i]);
}
}