hdu 2795 Billboard

2018-06-17 21:53:00来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

Billboard

Time Limit : 20000/8000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 47   Accepted Submission(s) : 18
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 5 2 4 3 3 3
 

 

Sample Output
1 2 1 3 -1
 

 

Author
hhanger@zju
 

 

Source
HDOJ 2009 Summer Exercise(5)
 
这道题,首先说建树,建树不太好建。我想到几种方案,因为贴上去的都是高度为1的。1.按面积建, 可很显然这是错误的。2.按高度建。
按高度建有一个需要注意的问题就是,他的高度上限是1e9。所以必须要加以判断他和n的大小,一开始并没有注意这一点,所以一直在RE,还以为是数组开小了。
还有可以直接判断根节点的最大值。倘若放进updata中,不免多了许多错误的情况。
很多问题都是经不住思考的,想明白之后发现之前离决解问题仅仅隔了一层窗户纸。
 
 
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
using namespace std;
int h,w,o;
struct node
{
    int l,r,v;  //l表高度,v表可加入的最大值
}tr[200100<<2];
void build(int n,int l,int r )
{
    tr[n].l=l;
    tr[n].r=r;
    tr[n].v=w;
    if(tr[n].l==tr[n].r)
        return;
    int mid =(l+r)>>1;
    build(n<<1,l,mid);
    build(n<<1|1,mid+1,r);
}
int updata(int v,int n)
{
        int res;
    if(tr[n].l==tr[n].r)
    {
        tr[n].v-=v;
        return tr[n].l;
    }
    if(tr[n<<1].v>=v)
        res=updata(v,n<<1);
    else
        res=updata(v,n<<1|1);
    tr[n].v=max(tr[n<<1].v,tr[n<<1|1].v);
    return res;
}
int main()
{
    while(~scanf("%d%d%d",&h,&w,&o))
    {
      
        build(1,1,min(h,o));
        for(int i =0;i<o;i++)
        {
            int x;
            scanf("%d",&x);
            if(tr[1].v<x)
                printf("-1\n");
            else
                printf("%d\n",updata(x,1));
        }
    }

}

  

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:c++练习题1

下一篇:9.28 作业 6