Billboard

本文介绍了一个经典的广告牌问题,通过线段树的数据结构解决公告如何在有限空间内有序张贴的问题。文章详细解析了问题背景、核心算法思想及其实现细节。

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

Billboard

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


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  1394  1828  1540 
/*
题意:有一块h*w的空广告牌,现在n个1*w的广告要贴上去,现在问

初步思路:实际上就是,一块一块的广告向上贴,但是贴之前查询一下,前面的已经贴过的位置是不是能盛下这个广告牌,如果能盛下的话
    就放上去,如果不能放下的话,就重新向下找位置贴,实际的操作:从1开始到n找第一个大于wi的位置,然后返回位置,然后将wi更新到
    到这个位置,这样的话每个几点就得保存区间最大值了,不能保存和了
*/
#include<bits/stdc++.h>
using namespace std;
int w,h,n;
int a;
int now=0;//表示当前开扩的广告范围
/****************************线段树基础模板*********************************/
const int maxn=200000+10;

int sum[maxn*4];
#define lson i*2, l, m
#define rson i*2+1, m+1, r

void PushUp(int i)
{
    sum[i]=max(sum[i*2],sum[i*2+1]);
}

void build(int i,int l,int r)
{
    if(l==r)
    {
        sum[i]=w;//初始化这个位置没有广告,所以空位置为w
        return ;
    }
    int m=(l+r)/2;
    build(lson);
    build(rson);
    PushUp(i);
}
//查询改为,查询从1开始的第一个比val大的数返回坐标
int query(int val,int i,int l,int r)
{
    // cout<<l<<" "<<r<<endl;
    if(l==r)//如果遍历到根节点的话,就可以输出了
        return l;
    int m=(l+r)/2;
    //这句话才能体现从1开始找,肯定是优先遍历坐左儿子
    if(sum[i*2]>=val) query(val,lson);
    else if(sum[i*2+1]>=val) query(val,rson);
}

void update(int id,int val,int i,int l,int r)
{
    if(l==r)
    {
        sum[i]+=val;
        return ;
    }

    int m=(l+r)/2;
    if(id<=m) update(id,val,lson);
    else update(id,val,rson);
    PushUp(i);
}

/****************************线段树基础模板*********************************/
int main(){
    // freopen("in.txt","r",stdin);
    while(scanf("%d%d%d",&h,&w,&n)!=EOF){
        now=min(h,n);
        build(1,1,now);//初始化建树
        for(int i=0;i<n;i++){
            scanf("%d",&a);
            // cout<<"sum[1]="<<sum[1]<<endl;
            if(sum[1]<a){
                puts("-1");
                continue;
            }
            int res=query(a,1,1,now);
            printf("%d\n",res);
            update(res,-a,1,1,now);
        }
    }
    return 0;
}

 

 

转载于:https://www.cnblogs.com/wuwangchuxin0924/p/6009431.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值