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),在给你 N 个广告,规格为 1 * x;要求吧广告贴在广告牌上,如下图所示。

       我是按照线段树的区间查询单点更新,先把原函数写出来,很显然这里是求区间最大值,而且是在全体区间上求,但是这里要求的不是最大值,而是最大值所在的位置,所以在加上自己的一些改动,求出位置来之后单点更新。

代码如下:

#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAX=200010;
int H, W, N;
struct Tree
{
    int l, r;
    int maxx;
}tree[MAX*4];
int a[MAX];
int ansPos;
void build(int k, int left, int right)
{
    tree[k].l=left;
    tree[k].r=right;
    if(tree[k].l==tree[k].r)
    {
        tree[k].maxx=W;
        return ;
    }
    int mid=(left+right)/2;
    build(2*k, left, mid);
    build(2*k+1, mid+1, right);
    tree[k].maxx=max(tree[2*k].maxx, tree[2*k+1].maxx);
    return ;
}
void updatePoint(int k, int pos, int d)
{
    if(tree[k].l==tree[k].r)//子点
    {
        tree[k].maxx+=d;
        return ;
    }
    int mid=(tree[k].l+tree[k].r)/2;
    if(pos<=mid)//更新左子树
    {
        updatePoint(2*k, pos, d);
    }
    else//右子树
    {
        updatePoint(2*k+1, pos, d);
    }
    tree[k].maxx=max(tree[2*k].maxx,tree[2*k+1].maxx);//回溯
}
void queryPoint(int k, int x)
{
    if(tree[k].l>H)//超过H了
    {
        ansPos=-1;
        return ;
    }
    if(tree[k].maxx<x)//如果maxx小于x了   -1
    {
        ansPos=-1;
        return ;
    }
    if(tree[k].l==tree[k].r)//如果到单点,  得到位置
    {
        ansPos=tree[k].l;//求的是位置
        return ;
    }
    if(tree[2*k].maxx>=x)//首先考虑左子树
        queryPoint(k*2, x);
    else
        queryPoint(k*2+1, x);
}
int main()
{
    int x;
    while(~scanf("%d%d%d", &H, &W, &N))
    {
        memset(tree, 0, sizeof(tree));
        build(1, 1, N);
        for(int i=1; i<=N; i++)
        {
            scanf("%d", &x);
            queryPoint(1, x);//查找可以放 x 宽度的位置
            if(ansPos!=-1)
                updatePoint(1, ansPos, -x);//更新该位置
            printf("%d\n",ansPos);
        }
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值