ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Billboard(线段树) HDU - 2795

2020-07-24 21:33:42  阅读:309  来源: 互联网

标签:announcement HDU 广告牌 billboard 广告 Billboard announcements include 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);
        }
    }
}

 


 

标签:announcement,HDU,广告牌,billboard,广告,Billboard,announcements,include,2795
来源: https://www.cnblogs.com/whhh/p/13374650.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有