BZOJ 1113: [Poi2008]海报PLA(单调栈)

Description

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

Input

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

Output

最少数量的海报数.


题解:

经典的单调栈题目,海报的向后延伸一定是以最低的边缘为上界的,所以扫到和栈顶元素相等的就可以节省一个海报


AC代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#include<ext/rope>
using namespace std;
using namespace __gnu_cxx;
#define LL long long
#define pii pair<int,int>
#define mp(a,b) make_pair(a,b)
const int MAXN = 3e5+10;
const int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
int a[MAXN],b[MAXN],n,m,res; stack<int> s;
signed main(){
#ifndef ONLINE_JUDGE
    freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
#endif // ONLINE_JUDGE
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d%d",&a[i],&b[i]);
    for(int i=1;i<=n;i++){
        while(!s.empty() && b[i]<=s.top()){
            if(b[i]==s.top()) res++;
            s.pop();
        }
        s.push(b[i]);
    }
    printf("%d\n",n-res);
    return 0;
}

发布了152 篇原创文章 · 获赞 1 · 访问量 2703

猜你喜欢

转载自blog.csdn.net/qq_43544481/article/details/103794442