You Are the One

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

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

dp[i][j]表示区间[i,j]的最小总不开心值

把区间[i,j]单独来看,则第i个人可以是第一个出场,也可以是最后一个出场(j-i+1),也可以是在中间出场(1   ~  j-i+1)

不妨设他是第k个出场的(1<=k<=j-i+1),那么根据栈后进先出的特点,以及题目要求原先男的是排好序的,那么::

第  i+1  到 i+k-1  总共有k-1个人要比i先出栈,

第 i+k   到j 总共j-i-k+1个人在i后面出栈

举个例子吧:

有5个人事先排好顺序  1,2,3,4,5

入栈的时候,1入完2入,2入完3入,如果我要第1个人第3个出场,那么入栈出栈顺序是这样的:

1入,2入,3入,3出,2出,1出(到此第一个人就是第3个出场啦,很明显第2,3号人要在1先出,而4,5要在1后出)

这样子, 动态转移方程 就出来了,根据第i个人是第k个出场的,将区间[i,j]分成3个部分

dp[i][j]=min(dp[i][j],dp[i+1,i+k-1]+dp[i+k,j]+(k-1)*a[i]+(sum[j]-sum[i+k-1])*k);

(sum[j]-sum[i+k-1])*k 表示 后面的 j-i-k+1个人是在i后面才出场的,那么每个人的不开心值都会加个 unhappy,sum[i]用来记录前面i个人的总不开心值,根据题目,每个人的unhappy是个 累加的过程 ,多等一个人,就多累加一次

 //2017-05-23
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = ;
const int inf = 0x3f3f3f3f;
int D[N], dp[N][N], sum[N]; int main()
{
int T, n;
scanf("%d", &T);
for(int kase = ; kase <= T; kase++){
scanf("%d", &n);
for(int i = ; i < n; i++){
scanf("%d", &D[i]);
if(i == )sum[i] = D[i];
else sum[i] = sum[i-] + D[i];
}
memset(dp, , sizeof(dp));
for(int i = ; i < n; i++)
for(int j = i+; j < n; j++)
dp[i][j] = inf;
for(int len = ; len <= n; len++){
for(int l = ; l+len <= n; l++){
int r = l+len-;
for(int k = ; k <= len; k++){
dp[l][r] = min(dp[l][r], dp[l+][l+k-]+dp[l+k][r]+(k-)*D[l]+(sum[r]-sum[l+k-])*k);
}
}
}
printf("Case #%d: %d\n", kase, dp[][n-]);
} return ;
}

HDU4283(KB22-G)的更多相关文章

  1. Storyboards Tutorial 03

    这一节主要介绍segues,static table view cells 和 Add Player screen 以及 a game picker screen. Introducing Segue ...

  2. 文件图标SVG

    ​<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink ...

  3. [转]Linux下g++编译与使用静态库(.a)和动态库(.os) (+修正与解释)

    在windows环境下,我们通常在IDE如VS的工程中开发C++项目,对于生成和使用静态库(*.lib)与动态库(*.dll)可能都已经比较熟悉,但是,在linux环境下,则是另一套模式,对应的静态库 ...

  4. CentOS 6.6 升级GCC G++ (当前最新版本为v6.1.0) (完整)

    ---恢复内容开始--- CentOS 6.6 升级GCC G++ (当前最新GCC/G++版本为v6.1.0) 没有便捷方式, yum update....   yum install 或者 添加y ...

  5. Linux deepin 下sublimes配置g++ openGL

    参考 :http://blog.csdn.net/u010129448/article/details/47754623 ubuntu 下gnome只要将代码中deepin-terminal改为gno ...

  6. [翻译svg教程]svg 中的g元素

    svg 中的<g>元素用来组织svg元素.如果一组svg元素被g元素包裹了,你可以通过对g元素进行变换(transform),被g元素包裹的元素也将被变换,就好这些被svg包裹的元素是一个 ...

  7. 软件工程:黄金G点小游戏1.0

    我们要做的是黄金G点小游戏: N个同学(N通常大于10),每人写一个0~100之间的有理数 (不包括0或100),交给裁判,裁判算出所有数字的平均值,然后乘以0.618(所谓黄金分割常数),得到G值. ...

  8. 2016huasacm暑假集训训练五 G - 湫湫系列故事——减肥记I

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/G 这是一个01背包的模板题 AC代码: #include<stdio.h&g ...

  9. 毫秒级的时间处理上G的图片(生成缩略图)

    测试环境: 测试图片(30M): 测试计时方法: Stopwatch sw1 = new Stopwatch(); sw1.Start(); //TODO...... sw1.Stop(); stri ...

  10. g++编译流程

    测试程序test.cpp如下所示: #include <iostream> using namespace std; #define MAX 9 int main() { //just f ...

随机推荐

  1. C#6.0语言规范(十八) 不安全代码

    前面章节中定义的核心C#语言与C和C ++的区别在于它省略了作为数据类型的指针.相反,C#提供了引用和创建由垃圾收集器管理的对象的能力.这种设计与其他功能相结合,使C#成为比C或C ++更安全的语言. ...

  2. python packaging

    python packaging 一.困惑 作为一个 Python 初学者,我在包管理上感到相当疑惑(嗯,是困惑).主要表现在下面几个方面: 这几个包管理工具有什么不同? * distutils * ...

  3. Windows server2012 IIs 8 自定义日志记录

    问题: 通过CDN加速的网站,记录日志时无法追踪源IP,日志的IP都为CDN节点ip. 分析: 1.在解析记录header时,CDN实际会把源IP以其它header的形式回传,如网宿为[Cdn-Src ...

  4. 整理几个 RPC 框架

    gRPChttp://www.grpc.io/https://github.com/grpcMotan支撑微博千亿调用的轻量级RPC框架:Motanhttp://h2ex.com/820WangleW ...

  5. linux和docker的capabilities介绍

    验证环境:centos7 x86/64 内核版本4.19.9 在linux 2.2版本之前,当内核对进程进行权限验证的时候,可以将进程划分为两类:privileged(UID=0)和unprivile ...

  6. java中this$0 this$1 this$2

    import java.lang.reflect.Field; public class Outer {//this$0 public class FirstInner {//this$1 publi ...

  7. postgresql进程及内存结

    一.进程和内存架构图 postgresql数据库启动时,先启动一个postmaster的主进程,然后fork出一些辅助子进程. 二.主进程postmaster -bash-4.2$ which pos ...

  8. postgresql逻辑结构--索引(六)

    一.索引简介 二.索引分类 三.创建索引 四.修改索引 五.删除索引

  9. k8s之安装docker-ce17.06

    1.下载rpm包 https://download.docker.com/linux/centos/7/x86_64/stable/Packages/ https://download.docker. ...

  10. Css相关用法个人总结

    Css相关用法个人总结