hdu2795 Billboard


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的板,每次要尽量在满足先靠上再考左的空白处放下1*wi 的公告,如果能放下输出第几行,都放不下输出-1 。

大致思路:最近正在被线段树折磨,这题也是稍微转换一下(其实明明并不那么好想)可以发现,因为每次放公告是1*wi的相当于每一次只能在某一行上放公告,有单点更新的意思,而每个“单点”的容量都是w,而每一次询问也是询问到某一具体行数,因此类似于单点查询,故可以把所有行数构成一棵线段树,用sum[i]表示每个线段树结点上的剩余容量(从0开始保存已放容量亦可),每次优先往上放符合线段树更新时自上往下的更新顺序。优先左再右可以优先放左孩子(当然要剩余余额允许了)再考虑右孩子。在更新到叶子结点时同时记录一下行号即可省略查询的步骤(同时完成),还有一点在初始时候行数要取h和n的较小值,(因为可能会出现h很大每个wi也很大而只有n块板,下面冗余的情况?)


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long  
#define lson l,m,rt<<1  //左孩子
#define rson m+1,r,rt<<1|1  //右孩子
const int N=2e5+5;  
LL sum[N<<2];  //维护每个结点的剩余可放容量
int h,w,n,ans;  
void PushUP(int rt)  
{  
    sum[rt]=max(sum[rt<<1],sum[rt<<1|1]);   //因为不会在非叶结点上更新,所以父亲结点保存左右孩子较大的剩余容量。
}  
  
void Build(int l,int r,int rt)  
{  
    sum[rt]=w;    //每个叶子结点的剩余容量初始均为w
    if(l==r)  
    {  
        return;  
    }  
    int m=(l+r)>>1;  
    Build(lson);  
    Build(rson);  
    PushUP(rt);  
}  
  
void Update(int c,int l,int r,int rt)  
{  
    if(l==r)  
    {  
        sum[rt]-=c;     //更新到叶子结点,因为有下面的判断条件故容量一定够放。
        ans=l;          //直接记录行号即左区间端点。
        return;  
    }  
    int m=(l+r)>>1;  
    if(sum[rt<<1]>=c)  
        Update(c,lson);  
    else  
        Update(c,rson);  
    PushUP(rt);  
}  
int main(){
    while(~scanf("%d%d%d",&h,&w,&n))
    {
        memset(sum,0,sizeof(sum));
        h=min(h,n);        
        Build(1,h,1);
        while(n--){
            int x;
            scanf("%d",&x);
            if(sum[1]>=x) {     //sum[1]是线段树最顶的保存整个区间叶子节点中剩余最大值
                Update(x,1,h,1);
                printf("%d\n",ans);   //ans即为所求
            }
            else puts("-1");
        //    for(int i=1;i<=h*2;i++) cout<<sum[i]<<' ';cout<<endl;
        }
    }
    
    
    return 0;
}

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值