首页 > 代码库 > codeforces 448C Painting Fence

codeforces 448C Painting Fence

    刷篱笆,只有横和竖, 竖着肯定最多是 n , 另一种 那么每一次先把最下面的那个刷掉,  刷掉之后 ,  继续把上面的  刷掉,, 每一次把  剩下的   再按横着或竖着  刷

   就是分治了

   

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define inf 0x7f7f7f7f
int a[6000];
int n;
int ans;
int len;
int dfs(int l,int r)
{
    int i,mina = inf;
    int sum = 0;
    for(i = l;i <= r; i++)
        mina = min(mina,a[i]);
    for(i = l;i <= r; i++)
        a[i] -= mina;
    sum = mina;
    int ll = l;
    for(i = l;i <= r; i++)
    {
        if(a[i] == 0)
        {
            sum += dfs(ll,i-1);
            ll = i+1;
        }
    }
    if(ll <= r) sum += dfs(ll,r);
    return min(sum,r-l+1);
}

int main()
{
    int n,i;
    while(~scanf("%d",&n))
    {
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        printf("%d\n",dfs(0,n-1));
    }
    return 0;
}

codeforces 448C Painting Fence