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

5
1
3
1
5
2

Sample Output

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,最后将和加起来,求最大和
思路:由里向外逆推区间
 
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int a[2005],dp[2005][2005]; int main()
{
int n,i,j,k,l,ans;
while(~scanf("%d",&n))
{
for(i = 1; i<=n; i++)
scanf("%d",&a[i]);
memset(dp,0,sizeof(dp));
for(i = 1; i<=n; i++)
dp[i][i] = a[i]*n;//将对角线初始化
for(l = 1; l<n; l++)
{
for(i = 1; i+l<=n; i++)
{
j = i+l;
dp[i][j] = max(dp[i+1][j]+(n-l)*a[i],dp[i][j-1]+(n-l)*a[j]);//这里是从最后出队的开始往前推,之前的初始化也是为了这里,因为只有最后出队的,i+1才会等于j。
}
}
printf("%d\n",dp[1][n]);
} return 0;
}

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. C#动态二维数组输出

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  2. js日期格式,日期对象

    以对象为基准去使用方法, 围绕Date对象 var a = new Date() 返回当前的时间对象,可以使用内置的日期对象的方法 a.getFullYear(), a.getMonth(), a.g ...

  3. C#如何释放已经加载的图片 (转)

    使用Image.FromFile取磁盘上的图片时,这个方法会锁定图片文件,而且会导致内存占用增大, 有几种方法解决: 一:将Image类转换成Bitmap类 System.Drawing.Image ...

  4. 当 IDENTITY_INSERT 设置为 OFF 时,不能向表 中的标识列插入显式值错误的解决方法

    一个主键.两个外键,把两个外键改为非空就行了. CREATE TABLE [dbo].[User_Compare]( ,) NOT NULL, [UserId] [int] NOT NULL, [Pa ...

  5. 鸟哥的linux私房菜——第20章 启动流程、模块管理与loader

    20.1 Linux启动流程分析 Linux启动过程: 按下开机电源后计算机硬件主动读取BIOS来加载硬件信息以及硬件系统的自我测试,之后系统会主动读取第一个可启动的设备(由BIOS设置),此时就可以 ...

  6. WPF扩展标记

    标记扩展和 WPF XAML,标记扩展是 XAML 语言以及 XAML 服务的 .NET 实现的常规功能 XAML 处理器和标记扩展 XAML 分析器可将特性值解释为可转换成基元的文本字符串,或可通过 ...

  7. 学习Swift--下标脚本

    下标脚本 下标脚本可以定义在类(Class).结构体(structure)和枚举(enumeration)这些目标中,可以认为是访问集合(collection),列表(list)或序列(sequenc ...

  8. iptables 配置需要保存

    iptables-save > /root/myiptables 将iptables规则导入到文件/root/myiptablse iptables-restore < /root/myi ...

  9. bzoj 1305: [CQOI2009]dance 二分+網絡流判定

    1305: [CQOI2009]dance跳舞 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 1340  Solved: 581[Submit][Sta ...

  10. 浅谈JS DDoS攻击原理与防御

    分布式拒绝服务攻击(DDoS)攻击是一种针对网站发起的最古老最普遍的攻击.Nick Sullivan是网站加速和安全服务提供商CloudFlare的一名系统工程师.近日,他撰文介绍了攻击者如何利用恶意 ...