bzoj 1113 海报pla

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

【解析】

单调增栈

显然只需要看矩形的高就可以了

当存在两个矩形的高相同且这两个矩形之间没有比他们更矮的矩形

这两个矩形可以用一个海报来覆盖

就需要维护一个单增序列找左边第一个小于它的值

【code】

#include<iostream>
#include<cstdio>
using namespace std;
#define N 250005
int n,top,ans,l,h;
int sta[N];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&l,&h);
        while(top&&sta[top]>h)top--;
        sta[++top]=h;
        if(sta[top-1]!=sta[top])ans++;
    }
    printf("%d",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/zzyh/p/6852569.html