Problem Description
  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 .

输入样例

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

输出样例

Case #1: 20
Case #2: 24


子区间划分:由于栈的特点,区间的第一个元素在第k位出栈,则(1,k-1)(k+1,n)互相独立
注意dp数组的赋值
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=400,inf=0x3f3f3f3f;
int a[N],s[N],dp[N][N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int T;cin>>T;
for(int t=1;t<=T;++t)
{
int n;
cin>>n;
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;++i)
{cin>>a[i];
s[i]=a[i]+s[i-1];
}
for(int i = 0; i <= n; i++)
for(int j = i+1; j <= n; j++)
dp[i][j] = inf;//注意初始化只有成立区间赋值为inf
//dp[i][j]表示第i个人以i为起点
//第k个出场,k从i到j的相对焦虑最小值
for(int len=2;len<=n;++len)
for(int i=1;i<=n;++i)
{
int j=i+len-1;
if(j>n) break;
for(int k=i;k<=j;++k)//第i个人在第k个出场
{
dp[i][j]=min(dp[i][j],(k-i)*a[i]+dp[i+1][k]+dp[k+1][j]
+(k-i+1)*(s[j]-s[k]));
//i在第k个出场,则子区间为(i+1,k) (k+1,j)
}
}
cout<<"Case #"<<t<<": "<<dp[1][n]<<'\n';
}
return 0;
}

最新文章

  1. 自己动手写ORM框架
  2. css体验优化之图片容器设置宽高比
  3. ssh框架整合---- spring 4.0 + struts 2.3.16 + maven ss整合超简单实例
  4. 修复AWS上EC2损坏的sshd_config文件
  5. ubuntu同步系统时间命令
  6. Win7 登入提示临时漫游档案
  7. 使用 cURL 获取站点的各类响应时间 – dns解析时间,响应时间,传输时间
  8. 跨域解决方案CORS使用方法
  9. HDU-2521 反素数
  10. 每天一点css3聚沙成塔(一):transition
  11. 简单的CSS网页布局--三列布局
  12. POJ2239 Selecting Courses【二部图最大匹配】
  13. MFC 自定义控件
  14. OC中自定义构造方法
  15. (四)surging 微服务框架使用系列之网关
  16. Good Time 冲刺 二
  17. Java的注解总结
  18. c strlen和sizeof详解
  19. vim粘贴和保存
  20. 熟悉JSON

热门文章

  1. 从零搭建hadoop集群之系统管理操作
  2. shell语句记录
  3. COM 对象的利用与挖掘4
  4. vscode 开发Vue项目
  5. ref、reactive、toRef、toRefs使用与区别
  6. chrome浏览器通知与语音播放
  7. 华三防火墙主备ACL
  8. stream 链式结构
  9. Echarts中国地图下钻
  10. 【awk】找出两个文件中的不同值