首页 > 代码库 > Codeforces Round #256 (Div. 2) C (448C)Painting Fence

Codeforces Round #256 (Div. 2) C (448C)Painting Fence

分治!首先是一大块,贪行刷和竖刷的最小,再转化为小块。。。。。。。。。。。。




AC代码如下:



#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

int n;
int a[5005];

int solve(int l,int r)
{
    int i,j;
    int len=r-l+1;
    int height=a[l];
    for(i=l;i<=r;i++)
        if(a[i]<height)
        height=a[i];
    for(i=l;i<=r;i++)
        a[i]-=height;
    int ans=height;
    for(i=l;i<=r;i++)
    {
        if(a[i]!=0)
        {
            int ll,rr;
            ll=i;
            j=i+1;
            while(j<=r&&a[j]!=0)
                j++;
            rr=j-1;
            ans+=solve(ll,rr);
        }
    }
    if(len<ans)
        ans=len;
    return ans;

}

int main()
{
    int i,j;
    while(cin>>n)
    {
        for(i=1;i<=n;i++)
            cin>>a[i];
       int ans= solve(1,n);
       cout<<ans<<endl;
    }

    return 0;
}