题目链接:https://vjudge.net/problem/HDU-4283

You Are the One

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

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个人站成一排准备上台表演,由于每个人都想是第一个站到台上,所以如果需要等待就会不开心。每个人都有一个值a[i],表示每等待一个人,不开心度就会+a[i]。工作人员为了使得所有人的不开心总值最小,就设置了一个小黑屋(一个栈),可以把队列的人先安排到小黑屋里,以此调节出场顺序。

题解:

1.可知:在一个区间 [l, r] 内, 如果l是第 k (1<=k<=r-l+1)个出场的,那么 [l+1, l+k-1] 必定是先与l出场的(根据栈的性质,如果要得到栈底元素,那么必须把栈底元素上面的所有元素出栈), 所以剩下的 [l+k, r] 是在l之后出场的。

2.根据第一条结论,如果确定了l在区间 [l, r] 是第k个出场的(不需要考虑区间之外的人),那么就可以把区间分为 [l+1, l+k-1] 和 [l+k, r],且这两个区间是互不影响的,所以又可以根据上述结论分别对这两个区间单独求值。

3.那怎么计算区间的不开心总值呢?

由于我们知道l在当前区间内是第k个出场的,所以我们可以先得到 a[l]*(k-1)。对于区间[l+1, l+k-1]的人,由于他们是先于l出场的,所以对于这个总体来说,他们是没有延迟的,所以直接加上dp[l+1, l+k-1]。对于区间[l+k, r]的人,由于他们是在l之后出场的,而l在这个区间内又是第k个出场的,所以对于 [l+k, r]这个总体来说,他们是整体延迟了k个人的,所以先加上 k*(sum[r]-sum[l+k-1]),加上了延迟所带来的不开心值之后,我们就可以把 区间[l+k, r]的出场是没有延迟的,所以情况跟区间[l+1, l+k-1]一样,再加上dp[l+k, r]就行了。

学习之处

1.栈的性质: 如果要得到栈底元素,那么必须把栈底元素上面的所有元素出栈(这个结论虽然显而易见,但是由这个结论再推出另外的结论却并非易事)。

2.求值除了一步求得之外,还可以分步求值,一个阶段只求一部分。

记忆化搜索:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; int a[MAXN];
int sum[MAXN], dp[MAXN][MAXN]; int dfs(int l, int r)
{
if(l>=r) return ;
if(dp[l][r]!=-) return dp[l][r]; dp[l][r] = INF;
for(int k = ; k<=r-l+; k++)
{
int tmp = dfs(l+, l+k-)+dfs(l+k,r)+(k-)*a[l]+k*(sum[r]-sum[l+k-]);
dp[l][r] = min( dp[l][r], tmp );
}
return dp[l][r];
} int main()
{
int T, n, kase = ;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
sum[] = ;
for(int i = ; i<=n; i++)
scanf("%d", &a[i]), sum[i] = sum[i-]+a[i]; memset(dp, -, sizeof(dp));
dfs(, n);
printf("Case #%d: %d\n", ++kase, dp[][n]);
}
}

递推:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; int a[MAXN];
int sum[MAXN], dp[MAXN][MAXN]; int main()
{
int T, n, kase = ;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
sum[] = ;
for(int i = ; i<=n; i++)
scanf("%d", &a[i]), sum[i] = sum[i-]+a[i]; memset(dp, , sizeof(dp));
for(int len = ; len<=n; len++)
{
for(int l = ; l<=n-len+; l++)
{
int r = l+len-;
dp[l][r] = INF;
for(int k = ; k<=r-l+; k++)
{
int tmp = dp[l+][l+k-] + dp[l+k][r] + (k-)*a[l] + k*(sum[r]-sum[l+k-]);
dp[l][r] = min(dp[l][r], tmp);
}
}
}
printf("Case #%d: %d\n", ++kase, dp[][n]);
}
}

HDU4283 You Are the One —— 区间DP的更多相关文章

  1. hdu4283 You Are the One 区间DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4283 自己想了很久还是不会,参考了别人的思路才写的,区间DP还是很弱,继续努力!! 思路: 转载: 题 ...

  2. hdu-4283 You Are the One 区间dp,

    题意:n个人排队上台,每个人有一屌丝值D,他的不满意值=D*(k-1)(k为他前面的总人数). 求整个队列不满意值之和的最小值.你只有一个操作,就是把队首的人塞进小黑屋,也就是压入栈中,后面的人就被提 ...

  3. hdu4283 区间dp

    //Accepted 300 KB 0 ms //区间dp //dp[i][j] 表示i到j第一个出场的最小diaosizhi //对于i到j考虑元素i //(1)i第一个出场,diaosizhi为 ...

  4. HDU4283:You Are the One(区间DP)

    Problem Description The TV shows such as You Are the One has been very popular. In order to meet the ...

  5. 【BZOJ-4380】Myjnie 区间DP

    4380: [POI2015]Myjnie Time Limit: 40 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 162  Solved: ...

  6. 【POJ-1390】Blocks 区间DP

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5252   Accepted: 2165 Descriptio ...

  7. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  8. BZOJ1055: [HAOI2008]玩具取名[区间DP]

    1055: [HAOI2008]玩具取名 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1588  Solved: 925[Submit][Statu ...

  9. poj2955 Brackets (区间dp)

    题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...

随机推荐

  1. bzoj [Scoi2016]美味

    [Scoi2016]美味 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 721  Solved: 391[Submit][Status][Discus ...

  2. 空指针问题(java.lang.NullPointerException)

    在Java中对值为null的指针调用任何方法,就会引发空指针异常(java.lang.NullPointerException).空指针异常绝对是Java中最难查找和调试的一种异常,你永远无法得到任何 ...

  3. rest-assured 将log()中的信息打印到log日志中去的方法

    rest-assured 将log()中的信息打印到log日志中去的方法: ============方法1============== PrintStream fileOutPutStream = n ...

  4. 降雨量 BZOJ 1067

    降雨量 [问题描述] 我们常常会说这样的话:“X年是自Y年以来降雨量最多的”.它的含义是X年的降雨量不超过Y年,且对于任意Y<Z<X,Z年的降雨量严格小于X年.例如2002,2003,20 ...

  5. Docker-PostgresSQL

    Postgresql  Docker安装运行 mac环境: 1.拉取官方镜像,并创建容器 zhoumatoMacBook-Pro:~ zhou$ docker search postgresql NA ...

  6. 洛谷P2058 仪仗队

    P2058 仪仗队 24通过 34提交 题目提供者shengmingkexue 标签数论(数学相关) 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 暂时没有讨论 题目描述 作为体育委员,C君 ...

  7. Java到底是值传递还是引用传递

    什么是按值传递,什么是按引用传递 按值调用(call by value) : 在参数传递过程中,形参和实参占用了两个完全不同的内存空间.形参所存储的内容是实参存储内容的一份拷贝. 按引用调用:在参数传 ...

  8. Java 读写文件大全

    原文:http://www.open-open.com/code/view/1423281836529 java中多种方式读文件 一.多种方式读文件内容. 1.按字节读取文件内容 2.按字符读取文件内 ...

  9. 修复OS X的Finder中文档 打开方式中重复程序的问题

    如上图,OS X在使用一段时间后,有些软件就会重复注册打开方式,对于有洁癖的人,这是难以接受的事. 不过有个命令可以很简单的把重复项给去掉. /System/Library/Frameworks/Co ...

  10. gdb生成的core文件位置

    gdb可以生成core文件,记录堆栈信息,core文件名字是下面这种格式 :core.9488,其中9488是PID 文件位置是当前目录