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

 
题意:给出的一系列的数字,可以看成一个双向队列,每次只能从队首或者队尾出队,第n个出队就拿这个数乘以n,最后将和加起来,求最大和
思路:由里向外逆推区间
 
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. int a[2005],dp[2005][2005];
  7.  
  8. int main()
  9. {
  10. int n,i,j,k,l,ans;
  11. while(~scanf("%d",&n))
  12. {
  13. for(i = 1; i<=n; i++)
  14. scanf("%d",&a[i]);
  15. memset(dp,0,sizeof(dp));
  16. for(i = 1; i<=n; i++)
  17. dp[i][i] = a[i]*n;//将对角线初始化
  18. for(l = 1; l<n; l++)
  19. {
  20. for(i = 1; i+l<=n; i++)
  21. {
  22. j = i+l;
  23. dp[i][j] = max(dp[i+1][j]+(n-l)*a[i],dp[i][j-1]+(n-l)*a[j]);//这里是从最后出队的开始往前推,之前的初始化也是为了这里,因为只有最后出队的,i+1才会等于j。
  24. }
  25. }
  26. printf("%d\n",dp[1][n]);
  27. }
  28.  
  29. return 0;
  30. }

POJ3186:Treats for the Cows(区间DP)的更多相关文章

  1. POJ3086 Treats for the Cows(区间DP)

    题目链接  Treats for the Cows 直接区间DP就好了,用记忆化搜索是很方便的. #include <cstdio> #include <cstring> #i ...

  2. O - Treats for the Cows 区间DP

    FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast am ...

  3. poj3186 Treats for the Cows(区间)

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

  4. Treats for the Cows 区间DP POJ 3186

    题目来源:http://poj.org/problem?id=3186 (http://www.fjutacm.com/Problem.jsp?pid=1389) /** 题目意思: 约翰经常给产奶量 ...

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

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

  6. POJ3186 Treats for the Cows —— DP

    题目链接:http://poj.org/problem?id=3186 Treats for the Cows Time Limit: 1000MS   Memory Limit: 65536K To ...

  7. poj3186 Treats for the Cows

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

  8. 【BZOJ】1652: [Usaco2006 Feb]Treats for the Cows(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1652 dp.. 我们按间隔的时间分状态k,分别为1-n天 那么每对间隔为k的i和j.而我们假设i或者 ...

  9. POJ 3186Treats for the Cows(区间DP)

    题目链接:http://poj.org/problem?id=3186 题目大意:给出的一系列的数字,可以看成一个双向队列,每次只能从队首或者队尾出队,第n个出队就拿这个数乘以n,最后将和加起来,求最 ...

随机推荐

  1. Constant is not finite! That's illegal. constant:inf'

    原本使用正常的情况, 切换为测试库突然出现这个错误, 网上搜索并排查后得出导致这个问题的原因: (1)就是你的除数为0(2)除数或者被除数为null 找出你出错的界面, 并打断点, 看看是否出现上面两 ...

  2. 关于Java中的选择排序法和冒泡排序法

    一,这种方法是直接传入一个数组进行排序(选择排序法) public static void selectSort(int arr[]){ for (int i = 0; i < arr.leng ...

  3. Windows Phone 之播放视频

    在Windows Phone 7中播放视频有两种方式, (1)使用MediaElement 控件来播放:用MediaElement 控件来播放视频比较灵活,你需要自己去实现播放暂停进度条等等的功能,播 ...

  4. jquery text--val--html

    .html()用为读取和修改元素的HTML标签    对应js中的innerHTML .html()是用来读取元素的HTML内容(包括其Html标签),.html()方法使用在多个元素上时,只读取第一 ...

  5. django基本命令备忘录

    1. 新建一个 django project django-admin.py startproject project-name 新建 app python manage.py startapp ap ...

  6. ART模式和Dalvik模式的异同

    Dalvik模式 如果要解释清楚什么是ART模式,我们就需要从Android系统的应用编译模式说起,我们都知道Android系统是以Linux系统为底层构建的,Android系统是开源(源代码公开)的 ...

  7. jdk各个版本

    http://www.cnblogs.com/langtianya/p/3757993.html

  8. 对rsync进行封装的shell脚本

    抓取 #!/bin/bash . push.sh # 错误处理:尝试查找备份文件 function onError() { local errFile="err" local se ...

  9. Java实现希尔排序

            华杰让我看了一道面试题:现有一段程序S,可以对任意n个数进行排序.如果现在需要对n^2个数进行排序,最少需要调用S多少次?(只允许调用S,不可以做别的操作).         看到了这 ...

  10. Java 字节码

    Java作为业界应用最为广泛的语言之一,深得众多软件厂商和开发者的推崇,更是被包括Oracle在内的众多JCP成员积极地推动发展.但是对于Java语言的深度理解和运用,毕竟是很少会有人涉及的话题.In ...