$bzoj1113-POI2008$ 海报$PLA$ 单调栈

  • 题面描述
    • \(N\)个矩形,排成一排. 现在希望用尽量少的矩形海报\(Cover\)住它们.
  • 输入格式
    • 第一行给出数字\(N\),代表有\(N\)个矩形.\(N\)\([1,25*10^4]\)
    • 下面\(N\)行,每行给出矩形的长与宽.其值在\([1,10^9]\)
  • 输出格式
    • 最少数量的海报数.
  • 题解
    • 维护单调减的单调栈,如果存在两个柱子高度相等,且他们中间的所有柱子都大于他们的高度,那么这两个柱子就可以用同一个海报覆盖
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=5e5+5;
int n,cnt;
int st[MAXN],top;
int main(){
    scanf("%d",&n);
    for (int i=1;i<=n;i++){
        int tmp,x; scanf("%d%d",&tmp,&x);
        while (top&&st[top-1]>=x){
            if (st[top-1]==x) cnt++;
            top--;
        }
        st[top++]=x;
    }
    printf("%d\n",n-cnt);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/shjrd-dlb/p/10844073.html