bzoj 1628: [Usaco2007 Demo] City skyline [greedy + monotonous stack]

I thought it was dp.
First of all, the default answer is n
. For a shadow, if the shadow in front is higher than it, it can be classified into the shadow in front, and the high part is counted alone;
if it is as tall as him, it doesn't need to be counted alone, ans-- ;
else push on the stack

#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;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325320789&siteId=291194637