博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
you_are_the_one(区间dp)
阅读量:4477 次
发布时间:2019-06-08

本文共 3172 字,大约阅读时间需要 10 分钟。

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。

 

1 #include 
2 #include
3 #include
4 using namespace std; 5 6 const int maxn = 200 + 5, inf = 0x3f3f3f3f; 7 8 int val[maxn], sum[maxn], dp[maxn][maxn]; 9 int n;10 11 int main() {12 int t, _case = 0;13 scanf("%d", &t);14 while(t --) {15 memset(dp, 0, sizeof dp);16 scanf("%d", &n);17 for(int i = 1; i <= n; i ++) {18 scanf("%d", &val[i]);19 sum[i] = sum[i - 1] + val[i];20 }21 for(int len = 1; len < n; len ++) {22 for(int i = 1; i + len <= n; i ++) {23 int j = i + len;24 dp[i][j] = inf;25 for(int k = 1; k <= len + 1; k ++) {26 dp[i][j] = min(dp[i][j], (k - 1) * val[i] + dp[i + 1][i + k - 1] + dp[i + k][j] + (sum[j] - sum[i + k - 1] ) * k);27 }28 }29 }30 printf("Case #%d: %d\n", ++ _case, dp[1][n]);31 }32 return 0;33 }

 

转载于:https://www.cnblogs.com/bianjunting/p/11595949.html

你可能感兴趣的文章
yum安装mongodb
查看>>
PHP基础知识系列:拦截器方法
查看>>
confluece安装文档及破解
查看>>
解决SpringCloud使用Feign跨服调用时header请求头中的信息丢失
查看>>
《CoffeeScript应用开发》学习:第五章 CoffeeScript中的类
查看>>
centos 7.3 快速安装ceph
查看>>
redis
查看>>
POJ 1942 Paths on a Grid(组合数学)
查看>>
Android UI设计规范之常用单位
查看>>
计算机如何实现运算?
查看>>
js: 从setTimeout说事件循环模型
查看>>
IT外企那点儿事(7):做一个优秀的基层
查看>>
lsof作用
查看>>
串的模式匹配算法——“KMP算法”
查看>>
Rq-165-FBI序列
查看>>
Audactiy 和 Sox
查看>>
vue视频学习笔记07
查看>>
【题解】矩形相交面积
查看>>
mac下安装mysqlcient 报错
查看>>
用Python3发送邮件详解
查看>>