codeforces 448C
给出n个杆子,每个杆子有一个长度,每次可以刷一行或一列,问最少刷多少次可以将整个墙刷成黄色。
#include
#include
#include
#define MAX 5007
using namespace std;
typedef long long LL;
int n;
LL a[MAX];
LL dp[MAX][MAX];
void solve ( int l , int r , LL h )
{
dp[l][r] = r-l+1;
if ( l == r ) return;
LL hh = 1LL<<48;
for ( int i = l ; i <= r ; i++ )
hh = min ( hh , a[i] );
LL ans = hh-h;
for ( int i = l ; i <= r ; i++ )
{
if ( a[i] == hh ) continue;
int j;
for ( j = i; j <= r ; j++ )
{
if ( j == r ) break;
if ( a[j+1] == hh ) break;
}
solve ( i , j , hh );
ans += dp[i][j];
i = j+1;
}
dp[l][r] = min ( dp[l][r] , ans );
}
int main ( )
{
while ( ~scanf ( "%d" , &n ))
{
for ( int i = 1 ; i <= n ; i++ )
scanf ( "%I64d" , &a[i] );
solve ( 1 , n , 0 );
printf ( "%I64d\n" , dp[1][n] );
}
}