洛谷3467 [POI2008]PLA-Postering 单调栈

本文介绍了一种使用单调栈解决海报覆盖问题的方法。该问题要求用最少数量的矩形海报覆盖一系列连续排列的矩形建筑的北侧墙面,且各海报不能重叠。通过维护单调递增栈来找到可以共用海报的相邻相同高度矩形。

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

题目描述
All the buildings in the east district of Byteburg were built in accordance with the old arbitecture:
they stand next to each other with no spacing inbetween.
Together they form a very long chain of buildings of diverse height, extending from east to west.
The mayor of Byteburg, Byteasar, has decided to have the north face of the chain covered with posters.
Byteasar ponders over the minimum number of posters sufficient to cover the whole north face.
The posters have rectangular shape with vertical and horizontal sides.
They cannot overlap, but may touch each other, i.e. have common points on the sides.
Every poster has to entirely adjoin the walls of certain buildings and the whole surface of the north face has to be covered.
Task Write a programme that:
reads the description of buildings from the standard input, determines the minimum number of posters needed to entirely cover their north faces, writes out the outcome to the standard output.
N个矩形,排成一排. 现在希望用尽量少的矩形海报Cover住它们.

Solution


这道题好眼熟a

通俗地讲,具有相同高度的矩形是可以用一张海报cover住的,那么我们先假定每个矩形cover一张海报,通过维护一个单调递增的单调栈寻找最左边与当前举行同高的矩形,然后这两个矩形就能用一张海报cover了

还有宽度?不影响的,忽略就好了

Code


#include <stdio.h>
#define rep(i, st, ed) for (int i = st; i <= ed; i += 1)
#define N 1000001
int h[N], stack[N];
int main(void){
    int n;
    scanf("%d", &n);
    rep(i, 1, n){
        scanf("%*d%d", &h[i]);
    }
    int top = 0;
    int ans = n;
    rep(i, 1, n){
        while (h[i] < stack[top]){
            top -= 1;
        }
        if (h[i] == stack[top]){
            top -= 1;
            ans -= 1;
        }
        stack[++ top] = h[i];
    }
    printf("%d\n", ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值