codeforce 448C Painting Fence (贪心)

原题地址:http://codeforces.com/problemset/problem/448/C

 

题意:

 

题解

 

#include<bits/stdc++.h>

#define clr(x,y) memset((x),(y),sizeof(x))

using namespace std;
typedef long long LL;

const int maxn=5000;
int A[maxn+5];
int d[maxn+5][20];

void rmq_init(int A[],int n)
{
    for (int i=1;i<=n;++i)
        d[i][0]=A[i];

    for (int j=1;(1<<j)<=n;++j)
    {
        for (int i=0;i+(1<<j)-1<=n;++i)
        {
            d[i][j]=min(d[i][j-1],d[i+(1<<(j-1))][j-1]);
        }
    }
}

int rmq(int l,int r)
{
    int k=0;
    while ((1<<(k+1))<=r-l+1) ++k;
    return min(d[l][k],d[r-(1<<k)+1][k]);
}

LL solve(int l,int r,int cnt)
{
    if (l>r) return 0LL;

    LL minA=rmq(l,r);
    LL res=minA-cnt;

    int last=l-1;
    for (int i=l;i<=r+1;++i)
    {
        if (i!=r+1 && A[i]!=minA) continue;
        res+=solve(last+1,i-1,minA);
        last=i;
    }

    return min((LL)r-l+1,res); /***/
}

int main(void)
{
    #ifdef ex
    freopen ("../in.txt","r",stdin);
    //freopen ("../out.txt","w",stdout);
    #endif

    int n;
    scanf("%d",&n);
    for (int i=1;i<=n;++i)
    {
        scanf("%d",&A[i]);
    }
    rmq_init(A,n);

    LL ans=solve(1,n,0);
    printf("%I64d\n",ans);
}

 

posted on 2016-08-22 19:35  zhong_wang  阅读(265)  评论(0编辑  收藏  举报

导航