hdu 2795


题意:给出一个广告板,然后要将n个广告贴在广告板上,然后要求每次贴广告的时候要越靠上越好,求每个广告贴的位置,找不到的话输出-1;

思路:以高 为线段构造线段树,然后上面的值为区间的能放的最大值,每次查询的时候优先往左子树走;

           当高很大的时候没有必要以高为线段进行构造线段树,直接以n来构造,因为最多有那么多的线段;


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 200000 + 5;
#define INF 0x3f3f3f3f
#define clr(x,y) memset(x,y,sizeof x )
typedef long long ll;

#define eps 10e-10
const ll Mod = 1000000007;
struct Tree
{
    int l;
    int r;
    int maxs;
}tree[maxn << 2];;
int h,w,n;
void build(int rt,int l,int r)
{
    tree[rt].l = l;
    tree[rt].r = r;
    if(l == r)
    {
        tree[rt].maxs = w;
        return ;
    }
    int mid = (l + r) >> 1;
    build(rt << 1,l,mid);
    build(rt<<1 | 1,mid+1,r);
    tree[rt].maxs = w;
}
int query(int rt,int x)
{
    if(tree[rt].l == tree[rt].r)
    {
        tree[rt].maxs -= x;
        return tree[rt].l;
    }
    int mid = (tree[rt].l + tree[rt].r) >> 1;
    int ret = -1;
    if(tree[rt << 1].maxs >= x)
    {
        ret = query(rt<<1,x);
    }
    else if(tree[rt<<1 |1].maxs >= x)ret = query(rt<<1|1,x);
    tree[rt].maxs = max(tree[rt << 1].maxs,tree[rt<<1|1].maxs);
    return ret;
}
int main()
{
    while( ~ scanf("%d%d%d",&h,&w,&n))
    {
        build(1,1,n > h ? h : n);
        for(int i = 1; i <= n; i ++)
        {
            int x;
            scanf("%d",&x);
            if(x > w)
            {
                cout << -1 << endl;
                continue;
            }
            cout << query(1,x) << endl;
        }
    }
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值