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;
}

hdu: You Are the One(区间DP)的更多相关文章

  1. hdu 4597 + uva 10891(一类区间dp)

    题目链接:http://vjudge.net/problem/viewProblem.action?id=19461 思路:一类经典的博弈类区间dp,我们令dp[l][r]表示玩家A从区间[l, r] ...

  2. HDU 2476 String painter (区间DP)

    题意:给出两个串a和b,一次只能将一个区间刷一次,问最少几次能让a=b 思路:首先考虑最坏的情况,就是先将一个空白字符串刷成b需要的次数,直接区间DP[i][j]表示i到j的最小次数. 再考虑把a变成 ...

  3. HDU 4597 Play Game(区间DP(记忆化搜索))

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4597 题目大意: 有两行卡片,每个卡片都有各自的权值. 两个人轮流取卡片,每次只能从任一行的左端或右端 ...

  4. HDU 5151 Sit sit sit 区间DP + 排列组合

    Sit sit sit 问题描述 在一个XX大学中有NN张椅子排成一排,椅子上都没有人,每张椅子都有颜色,分别为蓝色或者红色. 接下来依次来了NN个学生,标号依次为1,2,3,...,N. 对于每个学 ...

  5. hdu 5115 Dire Wolf(区间dp)

    Problem Description Dire wolves, also known as Dark wolves, are extraordinarily large and powerful w ...

  6. HDU 2476 String painter(区间DP)

    String painter Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

  7. HDU 4745 Two Rabbits(区间DP,最长非连续回文子串)

    Two Rabbits Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total ...

  8. HDU 5115 Dire Wolf ——(区间DP)

    比赛的时候以为很难,其实就是一个区间DP= =..思路见:点我. 区间DP一定要记住先枚举区间长度啊= =~!因为区间dp都是由短的区间更新长的区间的,所以先把短的区间更新完.. 代码如下: #inc ...

  9. hdu 3632 A Captivating Match(区间dp)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3632 题意:n个人进行比赛,每个人有一个价值a[i],最后冠军只有一个,只能相邻两个人进行 ...

  10. HDU 5151 Sit sit sit 区间dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5151 题解: 有n个椅子,编号为1到n. 现在有n个同学,编号为1到n,从第一个同学开始选择要坐的位 ...

随机推荐

  1. ES得分

    一.概念 1.ES主要用于搜索 2.搜索要把更有相关性的结果展示出来 3.对一个文档评分,相关性越大,评分越高 4.打分的本质是排序 二.评分规则 1.ES5之前,默认评分规则是TF-IDF,这是信息 ...

  2. Linux系统配置安装jdk

    菜鸟新人搭建服务器,目前不深入原理,只配置 1.官方自行下载Linux版本jdk http://www.oracle.com/technetwork/java/javase/downloads/jdk ...

  3. Mosquitto安装与部署

    版本说明: Mosquitto版本:v2.0.10     libwebsockets版本:v3.0.1(用于支持websockets)     mosquitto-go-auth(Mosquitto ...

  4. Echarts实现不均匀刻度的方法,自定义刻度(转)

    原文地址 今天突然有个我们的咨询公司找我问一个echarts问题,这个问题确实值得一解决,很有意思. 问题是这样的.数据中有很多低于100的数值,但是最高值却能达到14000. data = [93. ...

  5. 一些test短代码

    #include <stdio.h> int main(){ char ray_tx_pwr[2][3]={{1,2,3},{4,5,6}}; int i = 0; printf(&quo ...

  6. java第七周学习情况

    这个星期 主要是在搞学校在暑期安排的实验报告b 怎么说来着 才知道这个消息几天  这是对学习不上心的体现啊 题目也有点多 慢慢做呗 而Java这边还是看些相关知识呗 说实话 已经学不太懂了 不过没关系 ...

  7. [AGC013B] Hamiltonish Path

    个人思路: 随便从一个节点开始搜索,只要当前节点不满足条件,随便找一个与它有边相连,不在序列里的节点加入序列.因为要么中途停止,要么把所有节点遍历一遍,一定能找到一个端点. 我们直接从节点 \(1\) ...

  8. Pr视频软件主要知识点

    1,选中某一个面板,点击"Tab键上的 '波浪号' 键"即可将这个面板全屏展示  . 2,新建序列项目:自定义,25帧/s,方形像素,无场(逐行扫描). 3,序列面板素材自动缩放适 ...

  9. vue 收藏

    html: //收藏 <el-table-column prop="isOpen" label="">                 <te ...

  10. picture --攻防世界

    题目描述: 下载得到附件 解题思路: (1)将图片放入winnhex中查看 发现这是一张jpeg图片,修改文件后缀尾.jpg 或者使用binwalk分析 (2)binwalk分离图片 foremost ...