hdu2795 Billboard 線段樹模擬


Billboard

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


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
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   1698  1542  1828  1540  1255 


一個寬和高分別為w,h信息板,向上添加信息,要求每條信息的位置盡量靠上,並且從左到右排列,問每一條信息如果能添加上,它會處於第幾行。

用線段樹模擬,線段樹葉子節點的標號表示行號,從左往右行號依次增加,對於一個左右區間分別為l,r的節點,它所存儲的信息是從l行到r行中的最大剩余寬度。

我們將插入信息和查詢行號的工作合並一下,在插入的時候,因為要盡量選擇行號較小的,所以優先選擇左子樹,看左子樹的最大值是否大於等於所要插入的信息的寬度,如果不滿足就查詢右子樹,找到對應的葉子節點后,要將該葉子節點的寬度減去所插入信息的寬度,並且返回節點的標號,即行號,然后向上更新每個節點的最大值。

這里有一個需要優化的地方,因為信息板的高度可能非常大,但我們建樹的數組不可能設那么大。可以這樣想,如果信息板的寬度大於等於信息的數目,那么最差的情況就是每條信息占用一行,對於無法插入的信息,一定是因為它的寬度超過了信息板的寬度,所以,就算增加行數也無濟於事,那么就可以直接將h設為n,從而將空間復雜度降低。

#include <iostream>
#include <cmath>
#include <stdio.h>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <iomanip>
#include <algorithm>
#include <memory.h>
#define MAX 800000
using namespace std;

int tree[MAX];//每個區間記錄某些行中的最大剩余寬度

void PushUp(int rt)
{
    tree[rt]=max(tree[rt<<1],tree[rt<<1|1]);
}

void Build(int l,int r,int rt,int w)
{
    tree[rt]=w;//初始的時候所有行的最大寬度都是w
    if(l==r)
    {
        return;
    }
    int m=(l+r)>>1;
    Build(l,m,rt<<1,w);
    Build(m+1,r,rt<<1|1,w);
}

int Query(int wi,int l,int r,int rt)
{
    if(l==r)
    {
        tree[rt]-=wi;
        return l;
    }
    int m=(l+r)>>1;
    int ans=(tree[rt<<1]>=wi)?Query(wi,l,m,rt<<1):Query(wi,m+1,r,rt<<1|1);//優先找左子樹,因為較小的行號意味着更靠上
    PushUp(rt);//向上更新最大值
    return ans;
}


int main()
{
    int h,w,n,wi;
    while(~scanf("%d%d%d",&h,&w,&n))
    {
        if(h>n)//空間優化,當h>n的時候,意味着最差也就是每個信息占用一行,那么對於一條信息來說,如果寬度小於w
            h=n;//肯定能放開,否則肯定放不開,所以這種情況最多使用n行
        Build(1,h,1,w);//每個葉子節點表示行號,從左到右依次增大
        while(n--)
        {
            scanf("%d",&wi);
            if(tree[1]<wi)//如果所有行中的最大寬度都小於這條信息的寬度,肯定放不下
            {
                printf("-1\n");
                continue;
            }
            printf("%d\n",Query(wi,1,h,1));//查詢,順便更新
        }

    }
    return 0;
}





注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com