[POI2008]PLA-Postering(洛谷P3467)

[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.

输入格式

The first line of the standard input contains one integer n (1≤n≤250 000),denoting the number of buildings the chain comprises of.
Each of the following n lines contains two integers di and wi (1≤di,wi≤1 000 000 000), separated by a single space, denoting respectively the length and height of the i^th building in the row.

输出格式

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

5
1 2
1 3
2 2
2 5
1 4

输出 #1

4

题目大意为给你n个矩形,让你用最少海报把他们覆盖掉,海报不能重叠,也不可以高出被覆盖的矩形;
虽然知道是单调栈的题目,但是还是没做出来;这道题首先令sum等于矩形数量,然后维护一个单调递增的栈,当有元素入栈时,如果这个元素等于当前栈顶元素,说明这几个元素可以用一块海报,sum–;这个结论真的比较巧妙;
代码:

#include<bits/stdc++.h>
using namespace std;
int sta[300000];
int main(){
	int n;
	scanf("%d",&n);
	int w,h;
	int ans=0;//栈顶位置 
	int sum=n;
	for(int i=1;i<=n;i++){
		scanf("%d%d",&w,&h);
		while(ans&&sta[ans]>=h){
			if(sta[ans]==h) sum--;
			ans--;
		}
		sta[++ans]=h;
	}
	printf("%d\n",sum);
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值