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. yum 源的搭建

    repos和epel的关系 https://blog.csdn.net/fantaxy025025/article/details/84918199 配置阿里云的yum源 https://www.cn ...

  2. CF293E Close Vertices 点分治+树状数组

    开始zz写了一个主席树,后来发现写个树状数组就行~ #include <cstdio> #include <vector> #include <algorithm> ...

  3. luogu 2993 [FJOI2014]最短路径树问题 Dijkstra+点分治

    挺简单的,但是给人一种把两个问题强行弄到一起的感觉. 十分不好写. Code: #include <queue> #include <cstdio> #include < ...

  4. Codeforces 1213F Unstable String Sort

    cf题面 中文题意 求一个由最多26个.最少k个小写字母构成的,长度为n的字符串,这个字符串要满足的要求是--当其中字母按照p和q两个\(1\)~\(n\)的全排列重新排序时,新的字符串是按照升序排好 ...

  5. 论文阅读:Offloading Distributed Applications onto SmartNICs using iPipe

    摘要: 包含丰富计算资源的新兴多核SoC SmartNIC具有卸载通用数据中心服务器任务的潜力,但是目前尚不清楚如何有效地使用SmartNIC并最大程度地减少卸载收益,尤其是对于分布式应用程序. 为此 ...

  6. apply,call,bind函数作用与用法

    作用 可以把方法借给其它对象使用,并且改变this的指向 a.apply(b,[3,2]);//this指向由a变为b, a的方法借给b使用 实例: function add(a,b){       ...

  7. MySQ彻底删除与安装配置

    彻底删除 1.查看 MySQL 安装了哪些东西 rpm -qa |grep -i mysql 2.卸载 -.el7.x86_64 -.el7.x86_64 .noarch -.el7.x86_64 - ...

  8. 并发编程--Concurrent-工具类介绍

    并发编程--Concurrent-工具类介绍 并发编程--Concurrent-工具类介绍 CountDownLatch CylicBarrier Semaphore Condition 对象监视器下 ...

  9. 使用宝塔面板 配置nginx 访问ftp服务器下面的图片

    如果 你在服务器上 运行war项目 可以在tomcat 配置访问的: tomcat 也贴出来吧! 一.tomca配置访问,需要更改配置文件server.xml ,如果找不到,自己好好找一下  一般在 ...

  10. openssl生成秘钥对

    openssl genrsa -out pri.pem openssl rsa -in pri.pem -out pub.pem -pubout 这样就生成秘钥对了,其中pri.pem是私钥,pub. ...