HDU 2795 Billboard (线段树+贪心)

探讨了一个大型海报在无限大木板上的放置问题,使用贪心算法结合线段树进行优化,解决了海报放置的行号计算问题,通过二分思想减少时间复杂度。

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

手动博客搬家:本文发表于20170822 21:30:17, 原地址https://blog.youkuaiyun.com/suncongbo/article/details/77488127

URL: http://acm.hdu.edu.cn/showproblem.php?pid=2795题目大意:有一个h*w的木板 (h, w<=1e9), 现在有n (n<=2e5)张1*xi的海报要贴在木板上,按1~n的顺序每次贴海报时会选择最上的一排的最左边贴 (海报不能互相覆盖), 求每张海报会被贴在哪一行。最上方的一行编号为1, 以此类推。
样例解析:
如下

column 12345
row 1: 11333
row 2: 2222X
row 3: 444XXX: 未摆放;

对应行列上的数是摆放在此的海报的编号思路分析:本题实际上是一个贪心的思想。
每次可以从最上一行向下枚举剩余的空间,找到第一个能够贴上第i个海报的行,然后输出,更新即可。
时间复杂度O(n^2), 无法AC.
于是我们可以考虑二分的思想:
用线段树维护每一行剩余的空间的大小的最大值。
每次查询时,采用类似于二分答案的方法,每到达线段树的一个节点,若它的左子树最大值>=xi, 则左子树中必存在合法的最大答案,查询左子树;
假如左子树中的行无法装下第i张海报,但右子树最大值<=xi, 此时说明右子树中存在合法的最大答案,于是“退而求其次”,查询右子树。
如果两棵子树最大值都>xi, 则无法张贴此海报,于是输出-1.
查询完毕后,做一个单点修改,将答案所在的行剩余空间减去xi.
其实,最后一种情况不需要考虑。
假如整个[1, h]区间的最大值<xi, 则直接输出-1, 无需查询。
但是我们无法开4e9 (4h)如此大的数组,怎么办呢?
其实假如h>n, 那么下面的(h-n)行全部浪费了,不影响结果。
因此,h=min(h,n);
只开8e5 (4
n)的数组即可。
代码呈现:(Time: 3151 MS; 11256 KB; Code: 1576 B)

#include<cstdio>
#include<algorithm>
using namespace std;

const int MAXN = 2e5;
struct Node
{
    int left,right;
    int maxi;
}; 
struct SegmentTree
{
    Node nd[MAXN*4+8];
    
    void init()
    {
        for(int i=1; i<=MAXN*4; i++)
        {
            nd[i].left = nd[i].right = nd[i].maxi = 0;
        }
    }
    
    void build(int lbound,int rbound,int pos,int tot)
    {
        nd[pos].left = lbound;
        nd[pos].right = rbound;
        if(lbound==rbound)
        {
            nd[pos].maxi = tot;
            return;
        }
        int mid = (lbound+rbound)/2;
        build(lbound,mid,2*pos,tot);
        build(mid+1,rbound,2*pos+1,tot);
        nd[pos].maxi = tot;
    }
    
    void modify_subs(int bound,int val,int pos)
    {
        int mid = (nd[pos].left+nd[pos].right)/2;
        if(nd[pos].left==nd[pos].right)
        {
            nd[pos].maxi-=val;
            return;
        }
        if(bound<=mid) modify_subs(bound,val,2*pos);
        else modify_subs(bound,val,2*pos+1); 
        nd[pos].maxi = max(nd[2*pos].maxi,nd[2*pos+1].maxi);
    }
     
    int query(int pos,int val)
    {
        int mid = (nd[pos].left+nd[pos].right)/2;
        if(nd[pos].left==nd[pos].right) return nd[pos].left;
        int ans;
        if(val<=nd[2*pos].maxi) return query(2*pos,val);
        else return query(2*pos+1,val);
    }
};
SegmentTree sgt;
int n,m,p;

int main()
{
    while(scanf("%d%d%d",&n,&m,&p)!=EOF)
    {
        if(n>p) n = p;
        sgt.init();
        sgt.build(1,n,1,m);
        for(int i=1; i<=p; i++)
        {
            int x;
            scanf("%d",&x);
            if(sgt.nd[1].maxi < x)
            {
                printf("-1\n");
                continue;
            }
            int ans = sgt.query(1,x);
            printf("%d\n",ans);
            if(ans>0) sgt.modify_subs(ans,x,1);
        }
    }
    
    return 0;
}

转载于:https://www.cnblogs.com/suncongbo/p/10182378.html

### 线段树Billboard场景中的应用 #### 背景描述 HDU 2795 BillBoard 是一道经典的题目,涉及如何高效管理广告牌上的海报张贴情况。该问题的核心在于模拟一个二维平面上的覆盖过程,并计算未被覆盖的部分的高度总和。 #### 线段树的作用 线段树是一种高效的区间查询与修改数据结构,在处理此类动态更新和平面区域覆盖的问题上具有显著优势。通过维护区间的最大高度以及剩余空白部分的信息,能够快速完成对广告牌状态的管理和统计[^2]。 #### 实现思路 以下是基于线段树解决此问题的主要逻辑: 1. **节点定义** 定义线段树的每个节点存储当前区间的最高高度 `max_height` 和剩余空白部分的高度总和 `sum_blank`。 2. **初始化** 初始化整个线段树表示完整的广告牌高度范围 `[0, H)`,其中 `H` 表示广告牌的最大高度。 3. **更新操作** 当新贴一张海报时,找到其覆盖范围内尚未完全填充的位置并减少相应的空白高度。如果某一段已经被填满,则无需进一步深入子节点进行调整。 4. **查询操作** 查询当前广告牌中仍为空白的部分高度总和即可得出结果。 #### 示例代码 下面是一个简单的 Python 版本实现: ```python class SegmentTreeNode: def __init__(self, start, end): self.start = start self.end = end self.max_height = 0 self.sum_blank = (end - start) self.left = None self.right = None def build(start, end): if start >= end: return None node = SegmentTreeNode(start, end) mid = (start + end) // 2 node.left = build(start, mid) node.right = build(mid, end) return node def update(node, l, r, h): if not node or node.start > r or node.end <= l: return if l <= node.start and node.end <= r: if node.max_height < h: delta = min(h - node.max_height, node.sum_blank) node.sum_blank -= delta node.max_height += delta return update(node.left, l, r, h) update(node.right, l, r, h) # 合并左右子树的结果 node.sum_blank = node.left.sum_blank + node.right.sum_blank if node.left and node.right else \ node.left.sum_blank if node.left else \ node.right.sum_blank if node.right else 0 def query_sum_blank(root): return root.sum_blank if root else 0 # 主函数调用 if __name__ == "__main__": W, H, N = map(int, input().split()) # 广告牌宽度W、高度H、N张海报 root = build(0, H) for _ in range(N): x, y, w, h = map(int, input().split()) update(root, y, y+h, w) result = query_sum_blank(root) print(result) ``` 上述代码实现了构建线段树、执行更新操作以及查询剩余空白高度的功能。 #### 性能分析 由于每张贴海报的操作最多只会访问 O(log H) 层次的节点,因此整体时间复杂度为 O(N log H),非常适合大规模输入的数据集。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值