题目链接: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.求值除了一步求得之外,还可以分步求值,一个阶段只求一部分。

记忆化搜索:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <cmath>
  7. #include <queue>
  8. #include <stack>
  9. #include <map>
  10. #include <string>
  11. #include <set>
  12. using namespace std;
  13. typedef long long LL;
  14. const int INF = 2e9;
  15. const LL LNF = 9e18;
  16. const int MOD = 1e9+;
  17. const int MAXN = +;
  18.  
  19. int a[MAXN];
  20. int sum[MAXN], dp[MAXN][MAXN];
  21.  
  22. int dfs(int l, int r)
  23. {
  24. if(l>=r) return ;
  25. if(dp[l][r]!=-) return dp[l][r];
  26.  
  27. dp[l][r] = INF;
  28. for(int k = ; k<=r-l+; k++)
  29. {
  30. int tmp = dfs(l+, l+k-)+dfs(l+k,r)+(k-)*a[l]+k*(sum[r]-sum[l+k-]);
  31. dp[l][r] = min( dp[l][r], tmp );
  32. }
  33. return dp[l][r];
  34. }
  35.  
  36. int main()
  37. {
  38. int T, n, kase = ;
  39. scanf("%d", &T);
  40. while(T--)
  41. {
  42. scanf("%d", &n);
  43. sum[] = ;
  44. for(int i = ; i<=n; i++)
  45. scanf("%d", &a[i]), sum[i] = sum[i-]+a[i];
  46.  
  47. memset(dp, -, sizeof(dp));
  48. dfs(, n);
  49. printf("Case #%d: %d\n", ++kase, dp[][n]);
  50. }
  51. }

递推:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <cmath>
  7. #include <queue>
  8. #include <stack>
  9. #include <map>
  10. #include <string>
  11. #include <set>
  12. using namespace std;
  13. typedef long long LL;
  14. const int INF = 2e9;
  15. const LL LNF = 9e18;
  16. const int MOD = 1e9+;
  17. const int MAXN = +;
  18.  
  19. int a[MAXN];
  20. int sum[MAXN], dp[MAXN][MAXN];
  21.  
  22. int main()
  23. {
  24. int T, n, kase = ;
  25. scanf("%d", &T);
  26. while(T--)
  27. {
  28. scanf("%d", &n);
  29. sum[] = ;
  30. for(int i = ; i<=n; i++)
  31. scanf("%d", &a[i]), sum[i] = sum[i-]+a[i];
  32.  
  33. memset(dp, , sizeof(dp));
  34. for(int len = ; len<=n; len++)
  35. {
  36. for(int l = ; l<=n-len+; l++)
  37. {
  38. int r = l+len-;
  39. dp[l][r] = INF;
  40. for(int k = ; k<=r-l+; k++)
  41. {
  42. int tmp = dp[l+][l+k-] + dp[l+k][r] + (k-)*a[l] + k*(sum[r]-sum[l+k-]);
  43. dp[l][r] = min(dp[l][r], tmp);
  44. }
  45. }
  46. }
  47. printf("Case #%d: %d\n", ++kase, dp[][n]);
  48. }
  49. }

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. git提交之后没有push,代码被覆盖之后恢复

    git  reflog  通过这个看commit id git reset [commit id] --hard   有时候要删除一个index.lock文件.

  2. Nova 组件详解

    本节开始,我们将详细讲解 Nova 的各个子服务. 前面架构概览一节知道 Nova 有若干 nova-* 的子服务,下面我们将依次学习最重要的几个.今天先讨论 nova-api 和 nova-cond ...

  3. C#.net磁盘管理以及文件操作

    原文发布时间为:2008-08-08 -- 来源于本人的百度文章 [由搬家工具导入]    需要引入的命名空间: using System.IO;using System.Text; private ...

  4. Hdu5921 Binary Indexed Tree

    Hdu5921 Binary Indexed Tree 思路 计数问题,题目重点在于二进制下1的次数的统计,很多题解用了数位DP来辅助计算,定义g(i)表示i的二进制中1的个数, $ans = \su ...

  5. ELK之Kibana部署、收集系统日志、一个文件收集多个日志

    1.安装及配置Kibana cd /usr/local/src yum -y install kibana-5.4.0-x86_64.rpm grep "^[a-Z]" /etc/ ...

  6. Spring Boot使用Schedule实现定时任务

    适用的工具是:Schedule 集成步骤: 1.开启Schedule支持 package com.jsoft.springboottest.springboottest1; import org.sp ...

  7. UItableView 所有内容保存为图片

    将所有的UITableView保存为图片,因为UITableView只能保存显示当前,所以,就单个保存后,合并为一张图片 代码如下: -(IBAction)savePic:(id)sender { / ...

  8. ASP.NETCore使用AutoFac依赖注入

    原文:ASP.NETCore使用AutoFac依赖注入 实现代码 1.新建接口类:IRepository.cs,规范各个操作类的都有那些方法,方便管理. using System; using Sys ...

  9. SolidEdge 如何由装配图快速生成爆炸视图

    建立图纸精灵,组态中选择explode(没有下一步可选了)   点击完成即可绘制爆炸图

  10. Solidworks做镜像 导致厚度为零的几何体怎么办

    如下图所示,我想把1,2,3,4架子做一个镜像,但是提示错误   貌似只能用镜像实体,并且取消勾选"合并实体"