The TV shows such as You Are the One has been very popular. In order to meet the need of boys who are still single, TJUT hold the show itself. The show is hold in the Small hall, so it attract a lot of boys and girls. Now there are n boys enrolling in. At the beginning, the n boys stand in a row and go to the stage one by one. However, the director suddenly knows that very boy has a value of diaosi D, if the boy is k-th one go to the stage, the unhappiness of him will be (k-1)*D, because he has to wait for (k-1) people. Luckily, there is a dark room in the Small hall, so the director can put the boy into the dark room temporarily and let the boys behind his go to stage before him. For the dark room is very narrow, the boy who first get into dark room has to leave last. The director wants to change the order of boys by the dark room, so the summary of unhappiness will be least. Can you help him?

Input  The first line contains a single integer T, the number of test cases.  For each case, the first line is n (0 < n <= 100)
  The next n line are n integer D1-Dn means the value of diaosi of boys (0 <= Di <= 100)
Output  For each test case, output the least summary of unhappiness .
Sample Input

2
  
5
1
2
3
4
5 5
5
4
3
2
2

Sample Output

Case #1: 20
Case #2: 24

代码
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std; const int INF = 0x3f3f3f;
int n, num[105], dp[105][105], sum[105]; int main()
{
int t, cnt;
cin >> t;
while (t--)
{
scanf("%d", &n);
memset(dp, 0, sizeof dp); for (int i = 1; i <= n; i++)
{
scanf("%d", &num[i]);
}
sum[0] = 0;
for (int i = 1; i <= n; i++)
{
sum[i] = sum[i - 1] + num[i];
}
for (int i = 1; i <= n; i++)
{
for (int j = i + 1; j <= n; j++)
dp[i][j] = INF;
}
for (int i = n; i >= 1; i--)
{
for (int j = i + 1; j <= n; j++)
{
for (int k = 1; k <= j - i + 1; k++)
{
dp[i][j] = min(dp[i][j], dp[i + 1][i + k - 1] + num[i] * (k - 1) + dp[i + k][j] + k*(sum[j] - sum[i + k - 1]));
}
}
}
printf("Case #%d: %d\n", cnt++, dp[1][n]);
}
system("pause");
return 0;
}

最新文章

  1. js 的一些知识 摘自http://img0.pconline.com.cn/Pc_intranet/1105/13/313647_7.pdf
  2. PHP:php知识小解
  3. 拿到腾讯实习offer的前后小事
  4. 九 spring和mybatis整合
  5. Python File I/O
  6. Swift对面向对象提供了良好的支持,下面介绍几个其独有的特性。
  7. Excel导入mysql数据库
  8. SVN 不能提交, 看不到日志, 出现乱码. 解决方案.
  9. 【leetcode】Merge Sorted Array(合并两个有序数组到其中一个数组中)
  10. 如何在程序中动态设置墙纸(使用IActiveDesktop接口)
  11. 关于数据库优化1——关于count(1),count(*),和count(列名)的区别,和关于表中字段顺序的问题
  12. 【LintCode&#183;容易】字符串置换
  13. 【Linux】Set CentOS no GUI default
  14. macOS 升级后重装命令行工具的问题
  15. winform 关于Messagebox自动定时关闭
  16. label 赋值 , 隐藏 , 显示
  17. Servlet基本用法(二)接口和类
  18. ubuntu 10.10 更新源
  19. hbase windows安装
  20. delphi---EHlib第三方插件----TDBGridEH,TDBNumberEditEh,TDBComboBoxEh

热门文章

  1. Vue视频 | 【Vue2 + Vue3 前端教程】完整版
  2. 使用nvm时报错:exit status 1: ļ Ѵ ʱ ޷ ļ 的解决办法
  3. Vue中组件和插件的区别
  4. JZOJ 2020.02.01【NOIP提高组】模拟A 组
  5. Connect-The-Dots
  6. 使用express设置静态目录,创建服务,响应get请求
  7. MATH026th: 《古今算学丛书》目录
  8. [网鼎杯2020]boom
  9. .Net 6.0:WebAPI配置跨域
  10. Android FragmentTabHost底部选项卡实现