bzoj 1113(单调栈)

1113: [Poi2008]海报PLA

Time Limit: 10 Sec   Memory Limit: 162 MB
Submit: 888   Solved: 567
[ Submit][ Status][ Discuss]

Description

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

Input

第一行给出数字N,代表有N个矩形.N在[1,250000] 下面N行,每行给出矩形的长与宽.其值在[1,1000000000]2 1/2 Postering

Output

最少数量的海报数.

Sample Input

5
1 2
1 3
2 2
2 5
1 4

Sample Output

4


解题思路:首先考虑如果中间凸出来的那块要单独处理,然后用单调栈处理,最后得到单调递增的

栈,然后结果+最后剩余的就可以了


#include<cstring>
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
int q[250001];


inline int read()
{
char y; int x=0,f=1; y=getchar();
while (y<'0' || y>'9') {if (y=='-') f=-1; y=getchar();}
while (y>='0' && y<='9') {x=x*10+int(y)-48 ;y=getchar();}
return x*f;
}


int main()
{
n=read(); int tail=0; int ans=0;
for (int i=1;i<=n;++i)
{
int x,y; x=read(); y=read();
if (tail==0)
{
++tail; q[tail]=y;
}else
 {
  while (y<q[tail] && tail>0) {--tail; ++ans;}
if (y==q[tail])continue;
++tail; q[tail]=y;
 }
}
ans+=tail;
printf("%d",ans);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值