不充钱,你怎么AC?
题目:http://codevs.cn/problem/1048/
区间DP题,设 f[i][j] 为在区间 [i,j] 中合并的最小代价
目标状态是 f[1][n],末尾的求和公式可以用前缀和来优化
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<cmath> 5 #include<iostream> 6 #include<algorithm> 7 #define N 101 8 using namespace std; 9 10 const int oo=(1<<31)-1; 11 int a[N],f[N][N],n,sum[N]; 12 int main() 13 { 14 int i,j,k; 15 scanf("%d",&n); 16 for (i=1;i<=n;i++) 17 { 18 scanf("%d",&a[i]); 19 sum[i]=sum[i-1]+a[i]; 20 } 21 for (k=1;k<n;k++) 22 { 23 for (i=1;i<=n-k;i++) 24 { 25 f[i][i+k]=oo; 26 for (j=i;j<i+k;j++) f[i][i+k]=min(f[i][i+k],f[i][j]+f[j+1][i+k]+sum[i+k]-sum[i-1]); 27 } 28 } 29 printf("%d ",f[1][n]); 30 return 0; 31 }