BZOJ1628: [Usaco2007 Demo]City skyline

单调栈。给你的数据是一组矩阵,以为我们是求最少覆盖,只跟矩阵高度有关,所有我们可以不考虑宽度,大大优化代码长度。

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
const int maxn=5010,maxm=1e6+10;
int sta[maxm],h[maxn];
int main(){
    int n,m,ans,top=0;
    scanf("%d%d",&n,&m);
    ans=n;
    for(int i=1;i<=n;++i){
        int t;
        scanf("%d%d",&t,&h[i]);
        if(!h[i])ans--;
    }
    for(int i=1;i<=n;++i){
        while(top&&h[sta[top]]>=h[i]){
            if(h[sta[top]]==h[i])--ans;
            --top;
        }
        sta[++top]=i;
        if(!h[sta[top]])--top;
    }
    printf("%d",ans);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Dream-Runner/p/9403266.html
今日推荐