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. hdu_3535 (AreYouBusy)

    http://acm.hdu.edu.cn/showproblem.php?pid=3535 题意:        给你n个工作集合,给你T的时间去做它们.给你m和s,说明这个工作集合有m件事可以做, ...

  2. scrapy项目5:爬取ajax形式加载的数据,并用ImagePipeline保存图片

    1.目标分析: 我们想要获取的数据为如下图: 1).每本书的名称 2).每本书的价格 3).每本书的简介 2.网页分析: 网站url:http://e.dangdang.com/list-WY1-dd ...

  3. oracle 获取时间

    1.获取当前时间的前24小时的各小时时间段 select to_char(to_date(to_char(sysdate ) ,'yyyy-mm-dd hh24') || ':00:00','yyyy ...

  4. python学习之路(18)

    返回函数 函数作为返回值 高阶函数除了可以接受函数作为参数外,还可以把函数作为结果值返回. 我们来实现一个可变参数的求和.通常情况下,求和的函数是这样定义的: >>> def a(* ...

  5. PyCharm将main.py解析成text文件的解决方法

    问题:PyCharm将main.py解释成文本文件,没有代码提示,也无法执行 解决方法:File->Settings->Editor->File Types ->选则Text ...

  6. 190707Python-MySQL

    一.Python连接MySQL import pymysql conn = pymysql.connect(host='192.168.100.4', port=3306, user='dongfei ...

  7. Ajax中Put和Delete请求传递参数无效的解决方法(Restful风格)

    本文装载自:http://blog.csdn.net/u012737182/article/details/52831008    感谢原文作者分享 开发环境:Tomcat9.0 在使用Ajax实现R ...

  8. 阶段3 3.SpringMVC·_01.SpringMVC概述及入门案例_05.入门程序之入门代码编写

    先把默认的index.jsp删掉.默认的index.jsp没有jsp的声明 ok webapp文件夹下new一个 起名叫做index.新建的页面有jsp的头 创建控制器类 java下新建一个class ...

  9. linux配置多个ip

    linux配置多个ip /sbin/ifconfig eth0:1 172.19.121.180 broadcast 172.19.121.255  netmask 255.255.255.0 up ...

  10. 微信小程序的配置详解

    1.配置详解: 使用app.json文件来对微信小程序进行全局配置,决定页面文件的路径.窗口表现.设置网络超时时间.设置多 tab 等. 1>pages 接受一个数组,每一项都是字符串,来指定小 ...