HDU-2795-Billboard(线段树 点更新求最值)

本文介绍了一个经典的广告牌问题,通过使用线段树来高效解决广告位分配的问题。文章详细阐述了如何构建线段树以跟踪可用空间,并通过更新操作实现广告的放置过程。此外,还提供了一种不使用线段树的简化解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Billboard

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

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

题意:多组数据,每组数据第一行给出H,W,N,接着是N行Wi。代表有个高H宽W的广告牌,可以往上面贴高度为1,宽度为Wi的广告,尽量往上面一行贴,并且尽量往左边贴,输出广告贴在第几行,贴不下就输出-1.

思路:以H建立线段树,每层结点记录对应区间所能容纳的最大值,H,W都是1e9的级别,但是并不用离散化,因为N只有200000,所以if(H>N) H=N即可。

代码

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<queue>
using namespace std;
//维护H剩余的W
const int maxn=200100;//确实可以开这么小,if(H>N) H=N;
int Tree[maxn<<2];
int H;
int W;
int N;
void Push_Up(int root)
{
    Tree[root]=max(Tree[root<<1],Tree[root<<1|1]);
}
void Build(int root,int left,int right)
{
    if(left==right)
    {
        Tree[root]=W;
        return;
    }
    int mid=(left+right)>>1;
    Build(root<<1,left,mid);
    Build(root<<1|1,mid+1,right);
    Push_Up(root);
}
int Query(int root,int left,int right,int find_num)
{
    if(left==right&&Tree[root]>=find_num)
        return left;
    int mid=(left+right)>>1;
    if(Tree[root<<1]>=find_num)
        return Query(root<<1,left,mid,find_num);
    else if(Tree[root<<1|1]>=find_num)
        return Query(root<<1|1,mid+1,right,find_num);
    else
        return -1;
}
void Update(int root,int left,int right,int find_point,int num)
{
    if(left==right&&right==find_point)
    {
        Tree[root]-=num;
        return;
    }
    int mid=(left+right)>>1;
    if(find_point<=mid)
        Update(root<<1,left,mid,find_point,num);
    else
        Update(root<<1|1,mid+1,right,find_point,num);
    Push_Up(root);
}
int main()
{
    while(~scanf("%d%d%d",&H,&W,&N))
    {
        if(H>N)
            H=N;//坑点在此
        Build(1,1,H);
        int num;
        while(N--)
        {
            scanf("%d",&num);
            int find_point=Query(1,1,H,num);
            if(find_point!=-1)
                Update(1,1,H,find_point,num);
            printf("%d\n",find_point);
        }
    }
    return 0;
}

同时附上讨论区的另一份代码,并没有建树,代码量很小

代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXM 200005
#define MAXN 1000005
using namespace std;
const int INF=0x3f3f3f3f;
int n,H,W,val;
int Tree[MAXN];
int GetTree(int l,int r,int k)
{
    if(l==r)
    {
        Tree[k]+=val;
        return l;
    }
    int mid=(l+r)>>1,num;
    if(Tree[k<<1]+val<=W) num=GetTree(l,mid,k<<1);
    else if(Tree[k<<1|1]+val<=W) num=GetTree(mid+1,r,k<<1|1);
    Tree[k]=min(Tree[k<<1],Tree[k<<1|1]);
    return num;
}

int main()
{
    int i;
//  freopen("input.txt","r",stdin);
    while(~scanf("%d%d%d",&H,&W,&n))
    {
        memset(Tree,0,sizeof(Tree));
        for(H=min(H,n),i=1;i<=n;i++)
        {
            scanf("%d",&val);
            if(Tree[1]+val<=W) printf("%d\n",GetTree(1,H,1));
            else printf("-1\n");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值