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. 小米手机安装https证书报错:无法安装该证书 因为无法读取该证书文件

    Fiddler]手机安装https证书报错:无法安装该证书 因为无法读取该证书文件   之前在手机上使用 “ip:端口号” 的方法就能直接在手机上自动下载安装fiddler证书,但是现在有些手机并不能 ...

  2. PHP教程-反序列化的方法

    序列化是将变量转换为可保存或传输的字符串的过程:反序列化就是在适当的时候把这个字符串再转化成原来的变量使用.这两个过程结合起来,可以轻松地存储和传输数据,使程序更具维护性.兄弟连PHP培训() 1. ...

  3. C# 桌面截屏 添加鼠标

    #region 第一种方法 [DllImport("user32.dll")] static extern bool GetCursorInfo(out CURSORINFO pc ...

  4. Nowcoder 北师校赛 B 外挂使用拒绝 ( k次前缀和、矩阵快速幂打表找规律、组合数 )

    题目链接 题意 : 中文题.点链接 分析 : 有道题是问你不断求前缀和后的结果 Click here 这道题问的是逆过程 分析方法雷同.可参考 Click here ----------------- ...

  5. the nearest point/vertex point of linestring

    引用https://github.com/Toblerity/Shapely/issues/190 snorfalorpagus commented on 18 Oct 2014 The point ...

  6. 深度学习笔记(十)Augmentation for small object detection(翻译)

    一. abstract 这些年来,目标检测取得了令人瞩目的成就.尽管改进很大,但对于小目标和大目标的检测性能差异还是蛮大的.我们在 MS COCO 数据集上分析了如今一个比较先进的算法,Mask-RC ...

  7. Linux压缩工具

    一.gzip/gunzip/zcat gzip, gunzip, zcat - compress or expand files gzip [ option .... ] [ filenames .. ...

  8. 在spring官网上下载历史版本的spring插件,springsource-tool-suite

    目前spring官网(https://spring.io/tools3/sts/all)上可下载的spring插件只有: ECLIPSE ARCHIVE SIZE 4.9.0 springsource ...

  9. 14.Python bytes类型及用法

    Python 3 新增了 bytes 类型,用于代表字节串(这是本教程创造的一个词,用来和字符串对应).字符串(str)由多个字符组成,以字符为单位进行操作:字节串(bytes)由多个字节组成,以字节 ...

  10. MDK Keil 5软件小技巧

    几乎所有玩ARM Cortex M单片机的坛友都是通过MDK Keil 5或者IAR环境进行单片机的程序开发的,俗话说工欲善其事必先利其器,我们天天都在用这个开发环境,那么,有些在MDK Keil 5 ...