标签:pre style bsp can int scanf main define efi
#define INF 9999999 int n,a[99999],dp[9999][9999],ans=9999999,s[9999]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]),a[n+i]=a[i],s[i]=s[i-1]+a[i]; for(int i=1;i<n;i++) for(int j=1;j<=n-i;j++) { dp[j][i+j]=INF; for (int k=j; k<i+j; k++) dp[j][i+j]=min(dp[j][i+j],dp[j][k]+dp[k+1][i+j]+s[i+j]-s[j-1]); } printf("%d",dp[1][n]); return 0; } int n,a[99999],dp[9999][9999],ans=9999999,s[9999]; int dfs(int l,int r) { if(l==r) {return 0;} else{ int ll,rr; for(int i=l;i<r;i++) { if(dp[l][i]) ll=dp[l][r]; else ll=dfs(l,i); if(dp[i+1][r]) rr=dp[i+1][r]; else rr=dfs(i+1,r); dp[l][r]=99999999; dp[l][r]=min(dp[l][r],dfs(l,i)+dfs(i+1,r)+s[r]-s[l-1]); } return dp[l][r]; } } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]),a[n+i]=a[i],s[i]=s[i-1]+a[i]; dfs(1,n); printf("%d %d",dfs(1,n),dp[1][n]); cout<<dp[1][n]; return 0; }
标签:pre style bsp can int scanf main define efi
原文地址:http://www.cnblogs.com/CLGYPYJ/p/6363973.html