Billboard(线段树) HDU - 2795

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.


题意:

在学校的入口处有一个巨大的矩形广告牌,高为h,宽为w。所有种类的广告都可以贴,比如ACM的广告啊,还有餐厅新出了哪些好吃的,等等。。
在9月1号这天,广告牌是空的,之后广告会被一条一条的依次贴上去。
每张广告都是高度为1宽度为wi的细长的矩形纸条。
贴广告的人总是会优先选择最上面的位置来帖,而且在所有最上面的可能位置中,他会选择最左面的位置,而且不能把已经贴好的广告盖住。
如果没有合适的位置了,那么这张广告就不会被贴了。
现在已知广告牌的尺寸和每张广告的尺寸,求每张广告被贴在的行编号。
思路:以广告牌的高度进行划分建树,记录每层的剩余宽度的最大值,
例如
tree[1].len是1-N层最大的剩余宽度,
tree[1*2].len是1-(1+N)/2层的最大剩余宽度
tree[1*2+1].len是(1+N)/2-N层的最大剩余宽度
 

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include <sstream>
#include<vector>
#include<cmath>    
#include<stack>
#include<time.h>
#include<ctime>
using namespace std;
#define inf 1<<30
#define eps 1e-7
#define LD long double
#define LL long long
#define maxn 1000005
int s[maxn] = {};    
int h, w, n, ans = -1;
struct node
{
    int L, R, len;
}tree[maxn];
void up(int step)
{
    tree[step].len = max(tree[step << 1].len, tree[step << 1 | 1].len);
}
void build(int step, int L, int R)//建树
{
    tree[step].L = L;
    tree[step].R = R;
    if (tree[step].L == tree[step].R)
    {
        tree[step].len = w;//L层的宽度为w
        return;
    }
    int mid = (L + R) >> 1;
    build(step << 1, L, mid);
    build(step << 1 | 1, mid + 1, R);
    up(step);//更新
}
void treeInsert(int step, int k)//插入
{
    if (tree[step].L == tree[step].R)
    {
        tree[step].len -= k;//贴完广告后,这一层要减去广告占用的空间
        ans = tree[step].L;//记录层数
        return;
    }
    if (k <= tree[step << 1].len)
        treeInsert(step << 1, k);
    else
        treeInsert(step << 1 | 1, k);
    up(step);//更新
}
int main()
{
    while (~scanf("%d%d%d", &h, &w, &n))
    {
        if (h > n)
        {
            h = n;
        }
        build(1, 1, h);
        int k;
        for (int i = 1; i <= n; i++)
        {
            scanf("%d", &k);
            ans = -1;//更新
            if (k <= tree[1].len)
            {        
                treeInsert(1, k);
            }
            printf("%d\n", ans);
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/whhh/p/13374650.html
今日推荐