bzoj 1628: [Usaco2007 Demo]City skyline【贪心+单调栈】

还以为是dp呢
首先默认答案是n
对于一个影子,如果前边的影子比它高则可以归进前面的影子,高处的一段单算;
和他一样高的话就不用单算了,ans--;
否则入栈

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=50005;
int n,m,ans,top,h[N],s[N];
int read()
{
    int r=0,f=1;
    char p=getchar();
    while(p>'9'||p<'0')
    {
        if(p=='-')
            f=-1;
        p=getchar();
    }
    while(p>='0'&&p<='9')
    {
        r=r*10+p-48;
        p=getchar();
    }
    return r*f;
}
int main()
{
    n=read(),m=read();
    for(int i=1;i<=n;i++)
        h[i]=read(),h[i]=read();
    ans=n;
    for(int i=1;i<=n;i++)
    {
        while(s[top]>h[i])
            top--;
        if(s[top]==h[i])
            ans--;
        else 
            s[++top]=h[i];
    }
    printf("%d",ans);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/lokiii/p/8992703.html
今日推荐