常见优化技巧——PLA-Postering

编程任务涉及利用栈解决城市建筑物链中覆盖问题,找到使用最少海报数量的方法,涉及高度比较和覆盖决策。
摘要由CSDN通过智能技术生成

[POI2008] PLA-Postering

题目描述

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.

Byteburg市东边的建筑都是以旧结构形式建造的:建筑互相紧挨着,之间没有空间.它们共同形成了一条长长的,从东向西延伸的建筑物链(建筑物的高度不一).Byteburg市的市长Byteasar,决定将这个建筑物链的一侧用海报覆盖住.并且想用最少的海报数量,海报是矩形的.海报与海报之间不能重叠,但是可以相互挨着(即它们具有公共边),每一个海报都必须贴近墙并且建筑物链的整个一侧必须被覆盖(意思是:海报需要将一侧全部覆盖,并且不能超出建筑物链)

输入格式

The first line of the standard input contains one integer n n n ( 1 ≤ n ≤ 250   000 1\le n\le 250\ 000 1n250 000), denoting the number of buildings the chain comprises of.

Each of the following n n n lines contains two integers d i d_i di and w i w_i wi ( 1 ≤ d i , w i ≤ 1   000   000   000 1\le d_i,w_i\le 1\ 000\ 000\ 000 1di,wi1 000 000 000), separated by a single space, denoting respectively the length and height of the i t h i^{th} ith building in the row.

第一行为一个整数n(1≤n≤250000),表示有n个建筑,接下来n行中,第i行表示第i个建筑物的宽di与高wi(1≤di,wi≤1 000 000 000),中间由一个空格隔开

输出格式

The first and only line of the standard output should contain one integer, the minimum number of rectangular posters that suffice to cover the north faces of the buildings.

第一个为一个整数,表示最少需要几张海报.

样例 #1

样例输入 #1

5
1 2
1 3
2 2
2 5
1 4

样例输出 #1

4

提示

题目简述:N个矩形,排成一排. 现在希望用尽量少的矩形海报Cover住它们.

感谢@__乱世魇华 提供翻译

思路分析

这道题如果按常理去想的话,我们可以想到,对于两个柱子,如果一个比另一个高的话,那么我们就得多拿一张纸来覆盖,因此,多举几个例子我们可以发现,我们可以开一个栈,存楼的高度,如果栈不为空并且能够覆盖下一栋楼时,就将栈里的元素弹出,直到不能再覆盖的楼的。如果可以新开的话,就答案加1,继续查询能否覆盖,别忘记无论如何都要存下这栋楼的高度。简而言之:当前高度不能覆盖前面的高度的时候,则开一张

代码

//栈

#include<iostream>
#include<stack>

using namespace std;

const int N = 250010;

stack<int> s;
int h[N],w[N];
int n;

int main(){
    cin>>n;
    for(int i=1;i<=n;i++)cin>>w[i]>>h[i];
    
    int res=0;
    for(int i=1;i<=n;i++){
        while(!s.empty()&&s.top()>h[i]){
            s.pop();
        }
        if(s.empty()||s.top()!=h[i]){
            res++;
        }
        s.push(h[i]);
    }
    
    cout<<res;
    
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

green qwq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值