HDU2795 billboard【转化为线段树。】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795

hhanger大神的题目,水题都得有点思维。可怜

 

 

题意:h*w的木板,放进一些1*L的物品,求每次放空间能容纳且最上边的位子
思路:每次找到最大值的位子,然后减去L
线段树功能:query:区间求最大值的位子(直接把update的操作在query里做了)

 

 

技巧挺好,一开始自己思路建个超大二维数组,显然内存不够。

然后。  线段树的话其实就是深搜,if(l==r) 返回的肯定是最左边的结点,哈~

判断的时候直接用Max[1]与p比较就能判断是否输出-1,赞一个!!

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;

#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r

int h,w,n;

const int maxn=200005;// 是200000还是20000要搞清楚
int Max[maxn<<2];     //开maxn的多少倍结合最底层的结点数加以注意

void Pushup(int rt)
{
    Max[rt]=max(Max[rt<<1],Max[rt<<1|1]);
}
void build(int rt,int l,int r)
{
    Max[rt]=w;
    if(l==r) return;
    int m=(l+r)>>1;
    build(lson);
    build(rson);
}
int query(int p,int rt,int l,int r)//技巧5 . update放在query里面了
{
    if(l==r)
    {
        Max[rt]-=p;
        return l;// 2. 返回行数
    }

    int m=(l+r)>>1;
    int ret;
    ret=(Max[rt<<1]>=p)?query(p,lson):query(p,rson);// 技巧1.这地方深搜返回最左边的结点即符合的。
    Pushup(rt);// 技巧4 . 自己写这个地方有可能会忘
    return ret;
}

int main()
{
    while(scanf("%d%d%d",&h,&w,&n)!=EOF)
    {
        if(h>n) {h=n;}
        build(1,1,h);

        int p;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&p);
            if(Max[1]<p) printf("-1\n"); // 技巧3.Max[1]最根结点一定是最大值
            else
            {
                int ans=query(p,1,1,h);
                printf("%d\n",ans);
            }
        }
    }
    return 0;
}


 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值