[P1970][NOIP2013]花匠

原题链接

听说正解是DP

#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

int n,i,cnt,h[100005],top,pos[100005],ans;

int main()
{
    freopen("flower.in","r",stdin);
    freopen("flower.out","w",stdout);       

    scanf("%d",&n);

    for(i=1;i<=n;i++)
    {
        cnt++;
        scanf("%d",&h[cnt]);

        if(h[cnt]==h[cnt-1])
        {
            h[cnt]=0;
            cnt--;      
        }   
    }

    for(i=1;i<=cnt;i++)
        if(h[i]>h[i-1]&&h[i]>h[i+1])
        {
            top++;
            pos[top]=i;
        }

    ans=top*2;

    if(pos[top]==cnt) ans--;
    if(pos[1]!=1) ans++;

    printf("%d\n",ans);

    return 0;
}

你可能感兴趣的:(刷题,模拟)