标签:des style blog http io color ar os for
Description
Input
Output
Sample Input
Sample Output
1 #include<set> 2 #include<queue> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<cstring> 6 #include<iostream> 7 #include<algorithm> 8 using namespace std; 9 const int N = 1010; 10 #define For(i,n) for(int i=1;i<=n;i++) 11 #define Rep(i,l,r) for(int i=l;i<=r;i++) 12 #define Down(i,r,l) for(int i=r;i>=l;i--) 13 int num[N],sum[N]; 14 int dp[N][N]; 15 int T,n,cases; 16 int main(){ 17 scanf("%d",&T); 18 while(T--){ 19 cases++; 20 scanf("%d",&n); 21 sum[0]=0; 22 For(i,n){ 23 scanf("%d",&num[i]); 24 sum[i]=sum[i-1]+num[i]; 25 } 26 For(i,n){ 27 Rep(j,i,n) 28 dp[i][j]=1<<30; 29 dp[i][i]=0; 30 } 31 For(len,n-1) 32 For(i,n-len){ 33 int j=i+len; 34 dp[i][j]=min(dp[i][j],dp[i+1][j]+sum[j]-sum[i]); 35 Rep(k,i+1,j) 36 dp[i][j]=min(dp[i][j],dp[i+1][k]+num[i]*(k-i)+dp[k+1][j]+(sum[j]-sum[k])*(k-i+1)); 37 } 38 printf("Case #%d: %d\n",cases,dp[1][n]); 39 } 40 return 0; 41 }
标签:des style blog http io color ar os for
原文地址:http://www.cnblogs.com/kingnight/p/4069927.html