Billboard

来源:互联网 发布:win10 kaiwifi软件 编辑:程序博客网 时间:2024/04/30 11:15

Billboard




Problem Description
At the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and w is its width). The board is the place where all possible announcements are posted: nearest programming competitions, changes in the dining room menu, and other important information.

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.
 

Input
There are multiple cases (no more than 40 cases).

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.
 

Output
For each announcement (in the order they are given in the input file) output one number - the number of the row in which this announcement is placed. Rows are numbered from 1 to h, starting with the top row. If an announcement can't be put on the billboard, output "-1" for this announcement.
 

Sample Input
3 5 524333
 

Sample Output
1213-1
 

Author
hhanger@zju
 

Source
HDOJ 2009 Summer Exercise(5)
 

Recommend
lcy
 
/**线段树 单点更新 区间求最大值。我们可以把 h*w的木板,平均分成h份小木板,每份小木板宽为w高为1。这样对于 x[i]木板,从上往下比较, 找到第一个宽度大于等于x[i]的小木板, 放进去,使其长度-=x[i]。这样,我们可以维护一个线段树, 表示[l, r]区间中(l-r+1)个小木板中最长的长度。query()函数中 优先向左递归,就保证了靠左上方。**/#include <map>#include <set>#include <list>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <vector>#include <bitset>#include <cstdio>#include <string>#include <numeric>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>#include <functional>using namespace std;typedef long long  ll;typedef unsigned long long ull;int dx[4]={-1,1,0,0};int dy[4]={0,0,-1,1};//up down left rightbool inmap(int x,int y,int n,int m){if(x<1||x>n||y<1||y>m)return false;return true;}int hashmap(int x,int y,int m){return (x-1)*m+y;}#define eps 1e-8#define inf 0x7fffffff#define debug puts("BUG")#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define read freopen("in.txt","r",stdin)#define write freopen("out.txt","w",stdout)#define maxn 201000int Max[maxn<<2+1];int h,w,n;void build(int l,int r,int rt)//建树{    if(l==r)    {        Max[rt]=w;        return ;    }    int m=(l+r)>>1;    build(lson);    build(rson);    Max[rt]=max(Max[rt<<1],Max[rt<<1|1]);//更新}int update(int key,int l,int r,int rt)//查询并修改,线段树会按照当初插入的顺序从左到右依次进行比较,就能够实现每次插入时,用最合适的{    if(l==r)    {        Max[rt]-=key;        return l;    }    int m=(l+r)>>1;    int ans;    if(Max[rt<<1]>=key)//这里才是本题的关键,活用线段树,不仅仅是套模板,我还需要继续努力~~~~        ans=update(key,lson);    else        ans=update(key,rson);    Max[rt]=max(Max[rt<<1],Max[rt<<1|1]);//更新    return ans;}int main(){    while(~scanf("%d%d%d",&h,&w,&n))    {        int l;        if(h>=n) l=n;        else l=h;        build(1,l,1);        for(int i=0;i<n;i++)        {            int x;            scanf("%d",&x);            if(x>Max[1])//Max[1]是区间最大值,如果最大值都比x小,那就是没有地方可以插入                printf("-1\n");            else                printf("%d\n",update(x,1,l,1));        }    }    return 0;}


原创粉丝点击