hdu 2795 Billboard

来源:互联网 发布:通关宝典软件下载 编辑:程序博客网 时间:2024/06/03 20:36

Billboard

Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 21411    Accepted Submission(s): 8825


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
 


题意:
h*w的板子上贴海报。海报都是1*x宽度的。,每次贴的时候都会往左上角贴,第一行没位置再还第二行,有位置则尽可能往左贴。给你一些海报的信息,问能贴在哪一行上,不能贴输出-1;

解题思路:
线段树维护从 l ~ r 行中,空白位置最大的位置是多大。 如果当前节点 最大空位 < 海报宽度,那么当前 从1~r 行都不满足了,否则一直进入符合要求的子树,直到确定某一行为止。


代码

#include <iostream>#include <cstdio>#include <map>#include <cmath>#include <string.h>#include <algorithm>#include <set>#include <sstream>#include <vector>#include <queue>#include <stack>using namespace std;const int maxn = 200010*4;///顶多每个海报贴一行,一共贴n行const int INF = 0x3f3f3f3f;struct node{    int l,r;    int MAX;///第l行~第r行,最多能贴多宽的海报}tree[maxn];int h,w,n;void buildtree(int node,int b,int e){    int mid = (b+e)/2;    tree[node].l = b;    tree[node].r  = e;    tree[node].MAX = w;    if(b==e) return;    if(b <= mid) buildtree(node*2,b,mid);    if(e > mid) buildtree(node*2+1,mid+1,e);}int update(int node,int t){    if(tree[node].l == tree[node].r){///找到了这一行        tree[node].MAX -= t;        return tree[node].l;    }    int ans;    if(tree[node*2].MAX >= t) ans = update(node*2,t);    else ans = update(node*2+1,t);    tree[node].MAX = max(tree[node*2].MAX,tree[node*2+1].MAX);    return ans;}int main(){    while(scanf("%d%d%d",&h,&w,&n) != EOF){        if(h>n) h = n;///开n和h中小的那个数,如果只有n张海报最多贴n行        buildtree(1,1,h);        int t = 0;        for(int i = 0; i < n; ++i){            scanf("%d",&t);            if(tree[1].MAX < t) printf("-1\n");            else            printf("%d\n",update(1,t));        }    }    return 0;}


1 0
原创粉丝点击