标签:
3 1 2 3 7 13 7 8 16 21 4 18
9 239
解题思路:
刚刚学会区间dp,感觉区间dp要把握的重点就是要明确dp[i][j] = dp[i][k]+dp[k+1][j]+sum(i,j) 得到的,就是说,
任意一个区间[i,j]都可以由[i,k]和[k+1,j]得到。。。区间dp用的最多的就是记忆化搜索了。。
代码:
1 # include<cstdio> 2 # include<iostream> 3 # include<cstring> 4 5 using namespace std; 6 7 # define MAX 233 8 # define inf 99999999 9 10 int a[MAX]; 11 int sum[MAX]; 12 int dp[MAX][MAX]; 13 14 int cal ( int i,int j ) 15 { 16 if ( dp[i][j] ) 17 return dp[i][j]; 18 else if ( i==j-1 ) 19 dp[i][j] = a[i]+a[j]; 20 else if ( i==j ) 21 return 0; 22 else 23 { 24 dp[i][j] = inf; 25 for ( int k = i;k <= j;k++ ) 26 { 27 int temp = cal(i,k)+cal(k+1,j)+sum[j]-sum[i-1]; 28 if ( temp < dp[i][j] ) 29 { 30 dp[i][j] = temp; 31 } 32 } 33 } 34 return dp[i][j]; 35 } 36 37 int main(void) 38 { 39 int n; 40 while ( scanf("%d",&n)==1 ) 41 { 42 for ( int i = 1;i <= n;i++ ) 43 { 44 scanf("%d",&a[i]); 45 sum[i] = sum[i-1]+a[i]; 46 } 47 memset(dp,0,sizeof(dp)); 48 int res = cal(1,n); 49 printf("%d\n",res); 50 51 } 52 53 54 return 0; 55 }
标签:
原文地址:http://www.cnblogs.com/wikioibai/p/4502262.html