HDU-2795 Billboard(线段树)

来源:互联网 发布:数据分析展示界面 编辑:程序博客网 时间:2024/06/05 16:06

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 5
2
4
3
3
3
Sample Output
1
2
1
3
-1


题目大意:在一块长为w宽为h的广告牌上贴广告,n个人去贴。每个广告长为wi宽为1 ,每个人去贴时候都会尽量选择可行方案的左上角去贴,问每张广告所在的行数。不能贴的话输出-1.


这道题真的是,网上的代码也是支支吾吾说不清楚。但是大致的思路都表述出来了。即(l==r )时 用Tree[o].l保存广告所在的行数。
tree[o].num保存每行还剩余有多大的长度可以贴。
tree[o].num表示的是左右子树中剩余的最大长度。
思路大概就是这样。WA了5遍的心路历程。


附上AC 代码:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAX = 1e7+10;int h,w,n;struct Node{    int l,r;    int num;}Tree[MAX*4];void Build(int o,int l,int r)//建树{    Tree[o].l=l;    Tree[o].r=r;    Tree[o].num=w;    int mid=(l+r)/2;    if(l==r)        return;    Build(o*2,l,mid);    Build(o*2+1,mid+1,r);}int Query(int o,int l,int r,int high){    if(Tree[o].l==Tree[o].r)    {        Tree[o].num-=high;        return Tree[o].l;    }    else    {        int sum1=0;        int mid=(l+r)/2;        if(high<=Tree[o*2].num)    //如果左子树的长度足够贴                   sum1=Query(o*2,l,mid,high);        else              sum1=Query(o*2+1,mid+1,r,high);        Tree[o].num=max(Tree[o*2].num,Tree[o*2+1].num);        return sum1;     }}int main(){    while(~scanf("%d%d%d",&h,&w,&n))    {        if(h>n)//这里!!!!一定要初始化这个。不然根据题目数据范围。h过大。会超时。            h=n;        Build(1,1,h);        int high;        for(int i=1;i<=n;i++)        {            scanf("%d",&high);            if(Tree[1].num>=high)                printf("%d\n",Query(1,1,h,high));            else                printf("-1\n");         }    }       return 0; }