You Are the One

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6711    Accepted Submission(s): 3341

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 .
 
Sample Input
2
  
5
1
2
3
4
5

5
5
4
3
2
2

 
Sample Output
Case #1: 20
Case #2: 24
 
Source
 
Recommend
liuyiding

本题大意:队列中有n个人,要求从1到n依次上台,每个人上台有一个unhappy值val,假设第i个人

第k个上台那么他所带来的unhappy值为(k - 1) * val[ i ],为了使得unhappy变得足够小,现在允许轮到一个人上台的时候让他进入一个窄巷,窄巷满足先进后出原则,问你通过这个窄巷调整上台顺序,让这n个人都上台后的最小unhappy值为多少。

本题思路:分析容易知道对于第i个人,他是否入栈,何时出栈都会影响到最后的分数,所以我们肯定是要枚举每个人的是否入栈和出入栈状况,我们肯定会枚举每个i,容易想到区间dp,我们用dp[ i ][ j ]表示第 i 到 j 的人已经上台所花费的最小值(仅是i -> j,不考虑其他人),那么选择断点k,我们让i第k个上台,很容易知道它前面的 k - 1个人已经上台了,i第k各上台的花费为(k - 1) * val[ i ],他前面的人肯定是已经上台了的,那就是dp[i + 1][i + k - 1],第k + i ~ j 个人肯定在 i 之后,所以就有了子问题dp[k + i ][ j ],但是在计算这个子问题时肯定是把i + k当作第1个人的,所以在原问题上我们共把每个数多算了k 次,所以就可以得到状态转移方程dp[ i ][ j ] = (k - 1) * val[ i ] + dp[i + 1][i + k - 1] + dp[i + k][ j ] + (sum[ j ] - sum[i + k - 1]) * k。

 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + , inf = 0x3f3f3f3f; int val[maxn], sum[maxn], dp[maxn][maxn];
int n; int main() {
int t, _case = ;
scanf("%d", &t);
while(t --) {
memset(dp, , sizeof dp);
scanf("%d", &n);
for(int i = ; i <= n; i ++) {
scanf("%d", &val[i]);
sum[i] = sum[i - ] + val[i];
}
for(int len = ; len < n; len ++) {
for(int i = ; i + len <= n; i ++) {
int j = i + len;
dp[i][j] = inf;
for(int k = ; k <= len + ; k ++) {
dp[i][j] = min(dp[i][j], (k - ) * val[i] + dp[i + ][i + k - ] + dp[i + k][j] + (sum[j] - sum[i + k - ] ) * k);
}
}
}
printf("Case #%d: %d\n", ++ _case, dp[][n]);
}
return ;
}

you_are_the_one(区间dp)的更多相关文章

  1. 【BZOJ-4380】Myjnie 区间DP

    4380: [POI2015]Myjnie Time Limit: 40 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 162  Solved: ...

  2. 【POJ-1390】Blocks 区间DP

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5252   Accepted: 2165 Descriptio ...

  3. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  4. BZOJ1055: [HAOI2008]玩具取名[区间DP]

    1055: [HAOI2008]玩具取名 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1588  Solved: 925[Submit][Statu ...

  5. poj2955 Brackets (区间dp)

    题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...

  6. HDU5900 QSC and Master(区间DP + 最小费用最大流)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...

  7. BZOJ 1260&UVa 4394 区间DP

    题意: 给一段字符串成段染色,问染成目标串最少次数. SOL: 区间DP... DP[i][j]表示从i染到j最小代价 转移:dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k ...

  8. 区间dp总结篇

    前言:这两天没有写什么题目,把前两周做的有些意思的背包题和最长递增.公共子序列写了个总结.反过去写总结,总能让自己有一番收获......就区间dp来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...

  9. Uva 10891 经典博弈区间DP

    经典博弈区间DP 题目链接:https://uva.onlinejudge.org/external/108/p10891.pdf 题意: 给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能 ...

随机推荐

  1. [人物存档]【AI少女】【捏脸数据】1224今日份的推荐

    点击下载(城通网盘):AISChaF_20191111222714074.png 点击下载(城通网盘):AISChaF_20191108141610951.png

  2. conda退出base 环境

    安装conda后取消命令行前出现的base,取消每次启动自动激活conda的基础环境   方法一: 每次在命令行通过conda deactivate退出base环境回到系统自动的环境 方法二 1,通过 ...

  3. Java多线程和并发(七),synchronized

    目录 1.线程安全的主要原因 2.互斥锁的特性 3.锁的类别 4.类锁和对象锁的总结 七.synchronized 1.线程安全的主要原因 2.互斥锁的特性 Java中synchronized锁的不是 ...

  4. Python3学习笔记(十一):函数参数详解

    一.位置参数 根据参数的位置来传递参数,调用函数时,传递的参数顺序和个数必须和定义时完全一致 # 定义函数 def man(name, age): print("My name is %s, ...

  5. git远程相关

    git remote add origin git仓库地址 // 添加了远程仓库 git remote remove origin // 移除远程仓库 git push -u origin maste ...

  6. 用JavaScript实现div的鼠标拖拽效果

    实现原理鼠标按下时根据onmousemove事件来动态获取鼠标坐标位置以此来更新div的位置,实现的前提时div要有一个定位效果,不然的话是移动不了它的. HTML <div class=&qu ...

  7. 分布式-信息方式-ActiveMQ示例

    实战 代码如下: 信息生产者 package test.mq.helloword; import javax.jms.Connection; import javax.jms.ConnectionFa ...

  8. webpack3升级webpack4

    cnpm i webpck@4 webpack-cli -D cnpm i webpack-cli -D cnpm update npm WARN deprecated extract-text-we ...

  9. Java并发编程的艺术笔记(九)——FutureTask详解

    FutureTask是一种可以取消的异步的计算任务.它的计算是通过Callable实现的,多用于耗时的计算. 一.FutureTask的三种状态 二.get()和cancel()执行示意 三.使用 一 ...

  10. C++入门经典-例6.2-将二维数组进行行列对换

    1:一维数组的初始化有两种,一种是单个逐一赋值,一种是使用聚合方式赋值.聚合方式的例子如下: int a[3]={1,2,3}; int a[]={1,2,3};//编译器能够获得数组元素的个数 in ...