hdu 2795

来源:互联网 发布:小志传奇翅膀进阶数据 编辑:程序博客网 时间:2024/04/28 18:29

ProblemDescription

At theentrance 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 possibleannouncements are posted: nearest programming competitions, changes in thedining room menu, and other important information.
On September 1, the billboard was empty. One by one, the announcements startedbeing put on the billboard.
Each announcement is a stripe of paper of unit height. More specifically, thei-th announcement is a rectangle of size 1 * wi.
When someone puts a new announcement on the billboard, she would always choosethe topmost possible position for the announcement. Among all possible topmostpositions she would always choose the leftmost one.
If there is no valid location for a new announcement, it is not put on thebillboard (that's why some programming contests have no participants from thisuniversity).
Given the sizes of the billboard and the announcements, your task is to findthe numbers of rows in which the announcements are placed.

Input

There aremultiple 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 thebillboard 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 eachannouncement (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 numberedfrom 1 to h, starting with the top row. If an announcement can't be put on thebillboard, output "-1" for this announcement.

题目大意:

有一张h*w的公告墙,往墙上贴公告,每张公告高度为1,宽度不定,贴公告时,按着尽量往高处贴,同一层尽量往左贴的原则贴,输入每张公告的宽度,输出公告贴在哪一层。

代码如下:

#include<iostream>

#define N200000

usingnamespace std ;

int st[N],k ;

structline

{

    int L, R ;

    int mx ;

}a[4*N];

intmax(int a, int b)

{

       return (a>b?a:b) ;

}

voidBuildTree(int L, int R, int root, int w)

{

    a[root].L = L ;

    a[root].R = R ;

    a[root].mx = w ;

    if(L!=R)

    {

        int mid=(L+R)/2 ;

        BuildTree(L, mid, 2*root, w) ;

        BuildTree(mid+1, R, 2*root+1, w) ;

    }

}

voidinsert(int root, int v)

{

    if(a[root].L == a[root].R)

    {

        a[root].mx -= v ;

        st[k] = a[root].L ;

        return ;

    }

    if(v <= a[2*root].mx)

        insert(2*root, v) ;

    else

        insert(2*root+1, v) ;

    a[root].mx = max(a[2*root].mx,a[2*root+1].mx) ;

}

int main()

{

    int i, m, h, w, y ;

    while(scanf("%d%d%d", &h,&w, &m)!=EOF)

    {

        if(h>m)

            h = m ;

        BuildTree(1, h, 1, w) ;

        memset(st, -1, sizeof(st)) ;

        k=0 ;

        for(i=0; i<m; i++)

        {

            scanf("%d", &y) ;

            if(a[1].mx>=y)

                insert(1, y) ;

            k++ ;

        }

        for(i=0; i<m; i++)

            printf("%d\n", st[i]) ;

    }

    return 0 ;

}

voidBuildTree(int L, int R, int root, int w)函数建立线段树求解,线段树中,一个叶子节点表示公告牌上的一行,其父节点保存叶子节点上最大的剩余空间。编号为1的节点表示,此公告牌上的最大的可贴公告的剩余空间。

voidinsert(int root, int v)函数将输入的公告宽度存入线段树中,并将各节点相应的剩余空间更新;因为每次更新都是先更新叶子节点,所以,a[root].mx= max(a[2*root].mx, a[2*root+1].mx) ;  在进行完子节点的操作后,更新父节点。

Main()函数中,if(a[1].mx>=y),因为a[1]就表示公告牌的可贴公告的最大剩余空间,当mx<y时,就说明,公告牌不能再贴了。

0 0
原创粉丝点击