题目链接:http://poj.org/problem?id=3186

Treats for the Cows
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6548   Accepted: 3446

Description

FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a given period time.

The treats are interesting for many reasons:

  • The treats are numbered 1..N and stored sequentially in single file in a long box that is open at both ends. On any day, FJ can retrieve one treat from either end of his stash of treats.
  • Like fine wines and delicious cheeses, the treats improve with age and command greater prices.
  • The treats are not uniform: some are better and have higher intrinsic value. Treat i has value v(i) (1 <= v(i) <= 1000).
  • Cows pay more for treats that have aged longer: a cow will pay v(i)*a for a treat of age a.

Given the values v(i) of each of the treats lined up in order of the index i in their box, what is the greatest value FJ can receive for them if he orders their sale optimally?

The first treat is sold on day 1 and has age a=1. Each subsequent day increases the age by 1.

Input

Line 1: A single integer, N

Lines 2..N+1: Line i+1 contains the value of treat v(i)

Output

Line 1: The maximum revenue FJ can achieve by selling the treats

Sample Input

  1. 5
  2. 1
  3. 3
  4. 1
  5. 5
  6. 2

Sample Output

  1. 43

Hint

Explanation of the sample:

Five treats. On the first day FJ can sell either treat #1 (value 1) or treat #5 (value 2).

FJ sells the treats (values 1, 3, 1, 5, 2) in the following order of indices: 1, 5, 2, 3, 4, making 1x1 + 2x2 + 3x3 + 4x1 + 5x5 = 43.

Source

 
 
 
一.正向思维:
1.dp[l][r]表示:左边取了l个, 右边取了r个的最大值。
2.枚举左边取了多少个, 再枚举右边取了多少个。
3.对于当前的 dp[l][r],它可以是在dp[l-1][r]的基础上取了a[l];也可以是在dp[l][r-1]的基础上取了a[n+1-r]。所以:
  1. dp[l][r] = max(dp[l-1][r]+a[l], dp[l][r-1]+a[n+1-r])

当然,还需要注意边界条件:l-1>=0,r-1>=0

 
代码如下:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <vector>
  7. #include <queue>
  8. #include <stack>
  9. #include <map>
  10. #include <string>
  11. #include <set>
  12. #define ms(a,b) memset((a),(b),sizeof((a)))
  13. using namespace std;
  14. typedef long long LL;
  15. const double EPS = 1e-;
  16. const int INF = 2e9;
  17. const LL LNF = 2e18;
  18. const int MAXN = 2e3+;
  19.  
  20. int n;
  21. int a[MAXN], dp[MAXN][MAXN];
  22.  
  23. int main()
  24. {
  25. while(scanf("%d", &n)!=EOF)
  26. {
  27. for(int i = ; i<=n; i++)
  28. scanf("%d", &a[i]);
  29.  
  30. memset(dp, , sizeof(dp));
  31. for(int l = ; l<=n; l++)
  32. for(int r = ; l+r<=n; r++)
  33. {
  34. if(l!=) dp[l][r] = max(dp[l-][r]+(l+r)*a[l], dp[l][r]);
  35. if(r!=) dp[l][r] = max(dp[l][r], dp[l][r-]+(l+r)*a[n+-r]);
  36. }
  37.  
  38. int ans = -INF;
  39. for(int l = ; l<=n; l++)
  40. ans = max(ans, dp[l][n-l]);
  41. printf("%d\n", ans);
  42. }
  43. }
 
二.逆向思维:
1.逆向推导, 即把过程逆过来,然后就变成了:从中间开始往外取,这样就变成了连续的一段。
2.dp[i][j]表示:区间[i, j]的最大值。
3.枚举区间长度, 然后再枚举起点(终点就确定了)。对于dp[i][j],它可以是在dp[i+1][j]的基础上取了a[i],也可以是在dp[i][j-1]的基础上取了a[j]。两者取其大。
 
 
代码如下:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <vector>
  7. #include <queue>
  8. #include <stack>
  9. #include <map>
  10. #include <string>
  11. #include <set>
  12. #define ms(a,b) memset((a),(b),sizeof((a)))
  13. using namespace std;
  14. typedef long long LL;
  15. const double EPS = 1e-;
  16. const int INF = 2e9;
  17. const LL LNF = 2e18;
  18. const int MAXN = 2e3+;
  19.  
  20. int n;
  21. int a[MAXN], dp[MAXN][MAXN];
  22.  
  23. int main()
  24. {
  25. while(scanf("%d", &n)!=EOF)
  26. {
  27. for(int i = ; i<=n; i++)
  28. scanf("%d", &a[i]);
  29.  
  30. for(int i = ; i<=n; i++)
  31. dp[i][i] = a[i]*n;
  32.  
  33. for(int len = ; len<=n; len++)
  34. for(int i = ; i+len-<=n; i++)
  35. {
  36. int j = i+len-;
  37. dp[i][j] = max(dp[i+][j]+(n-len+)*a[i], dp[i][j-]+(n-len+)*a[j]);
  38. }
  39.  
  40. printf("%d\n", dp[][n]);
  41. }
  42. }

三.记忆化搜索:

1.上面的两种方法都要考虑枚举顺序的问题,有时比较不好处理。那么可以用记忆化搜索。

2. 思维与方法二一样,只是写法不同。

代码如下:

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

POJ3186 Treats for the Cows —— DP的更多相关文章

  1. kuangbin专题十二 POJ3186 Treats for the Cows (区间dp)

    Treats for the Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7949   Accepted: 42 ...

  2. poj3186 Treats for the Cows

    http://poj.org/problem?id=3186 Treats for the Cows Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  3. BZOJ 1652: [Usaco2006 Feb]Treats for the Cows( dp )

    dp( L , R ) = max( dp( L + 1 , R ) + V_L * ( n - R + L ) , dp( L , R - 1 ) + V_R * ( n - R + L ) ) 边 ...

  4. poj 3186 Treats for the Cows(dp)

    Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for gi ...

  5. poj3186 Treats for the Cows(区间)

    题目链接:http://poj.org/problem?id=3186 题意:第一个数是N,接下来N个数,每次只能从队列的首或者尾取出元素. ans=每次取出的值*出列的序号.求ans的最大值. 样例 ...

  6. POJ3186:Treats for the Cows(区间DP)

    Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for gi ...

  7. 【POJ - 3186】Treats for the Cows (区间dp)

    Treats for the Cows 先搬中文 Descriptions: 给你n个数字v(1),v(2),...,v(n-1),v(n),每次你可以取出最左端的数字或者取出最右端的数字,一共取n次 ...

  8. poj 3186 Treats for the Cows(区间dp)

    Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for gi ...

  9. (区间dp + 记忆化搜索)Treats for the Cows (POJ 3186)

    http://poj.org/problem?id=3186   Description FJ has purchased N (1 <= N <= 2000) yummy treats ...

随机推荐

  1. jenkins 提示No emails were triggered

    发送邮件 Jenkins->系统管理->系统设置,在“邮件通知”里设置smtp服务器地址,以及发送者邮箱地址,在具体的任务构建完成以后,可以设置发送邮件,在某一个任务的"Add ...

  2. 【java基础 3】树形结构数据呈现的递归算法实现

    一.基本概况 在我的项目中,常常会用到树形结构的数据,最为明显的就是左边菜单栏,类似于window folder一样的东西. 而我之前一直是借助前端封装好的ZTree等工具实现展示,而后台则通常使用递 ...

  3. [luoguP2831] 愤怒的小鸟(状压DP)

    传送门 感觉这题不是很难,但是很恶心. 说一下几点. 1.预处理出来每两个点所构成的抛物线能消除的猪的集合. 2.如果两个点横坐标相同,则不能构成抛物线 3.a >= 0 continue 4. ...

  4. 使用WaveOut API播放WAV音频文件(解决卡顿)

    虽然waveout已经过时,但是其api简单,有些时候也还是需要用到. 其实还是自己上msdn查阅相应api最靠谱,waveout也有提供暂停.设置音量等接口的,这里给个链接,需要的可以自己查找: h ...

  5. android开发里跳过的坑-电源锁WakeLock不起作用

    由于个别需求,需要做一个不让系统屏常亮不休眠的APP,于是使用了电源锁,在oncreate里加了如下代码: PowerManager pm = (PowerManager) context .getS ...

  6. 《TCP/IP详解卷1:协议》——第5章 RARP:逆地址解析协议(转载)

    1.引言 具有本地磁盘的系统引导时,一般是从磁盘上的配置文件中读取IP地址.但是无盘机,如X终端或无盘工作站,则需要采用其他方法来获得IP地址. 网络上的每个系统都具有唯一的硬件地址,它是由网络接口生 ...

  7. php 基础复习 2018-06-19

    (1)date()函数 如果从代码返回的不是正确的时间,有可能是因为您的服务器位于其他国家或者被设置为不同时区. 因此,如果您需要基于具体位置的准确时间,您可以设置要用的时区. date_defaul ...

  8. 库操作&表操作

    系统数据库 ps:系统数据库: mysql 授权库,主要存储系统用户的 权限信息 test MySQL数据库系统自动创建的 测试数据库 ination_schema 虚拟库,不占用磁盘空间,存储的是数 ...

  9. Animation显示ListView的每一条记录

    activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...

  10. HDU 1558

    输入线段的两个短点,如果线段相交那么他们属于一个集合,查看第i条线段所在的集合有几条线段. 好久没码码了,总是各种蠢. 首先找出两条直线的方程,求解相交点的横坐标,然后看是不是在线段内部. 没有注意题 ...