题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4283
-------------------------------------------------------------------------------------
这题由于是区域赛的题目 所以比较有名 然而原理实际上还是和 LightOJ1422一样
http://www.cnblogs.com/sagitta/p/5164883.html
要利用到栈的FILO的性质 对于任意一个人 在他之后入栈的那一堆是连续的
并且从集合上来讲是和在他之前入栈的是一样的
弄清楚这个性质后这题就比较水了
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 using namespace std; 6 const int N = 110; 7 int d[N], f[N][N], sum[N]; 8 int t, n; 9 int main() 10 { 11 scanf("%d", &t); 12 for(int ca = 1; ca <= t; ++ca) 13 { 14 scanf("%d", &n); 15 for(int i = 1; i <= n; ++i) 16 { 17 scanf("%d", &d[i]); 18 sum[i] = sum[i - 1] + d[i]; 19 } 20 memset(f, 0, sizeof f); 21 for(int len = 2; len <= n; ++len) 22 for(int i = 1; i + len - 1 <= n; ++i) 23 { 24 int j = i + len - 1; 25 f[i][j] = 1e9; 26 for(int k = i; k <= j; ++k) 27 f[i][j] = min(f[i][j], f[i + 1][k] + d[i] * (k - i) + f[k + 1][j] + (sum[j] - sum[k]) * (k - i + 1)); 28 } 29 printf("Case #%d: %d ", ca, f[1][n]); 30 } 31 return 0; 32 }