Treats for the Cows
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5753   Accepted: 2972

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.

Source

题意:
n个数排成一串,每次可以从头取一个数或者从尾取一个数,取出数后该数的值乘以取他的时间(每取一个数时间加一)是价值,问能够得到的最大价值。
代码:
//dp,要从内向外推,dp[i][j]表示串中只剩下i~j数的时候最大价值
//dp[i][j]=max(dp[i+1][j]+a[i]*cnt,dp[i][j-1]+a[j]*cnt),因为dp[i][j]时要用到
//后面的数,要从近距离到远距离dp。当然也可以从前向后。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,a[];
int f[][];
int main()
{
while(scanf("%d",&n)==){
for(int i=;i<n;i++) scanf("%d",&a[i]);
for(int i=n-;i>=;i--){
for(int j=i;j<n;j++){
f[i][j]=max(f[i+][j]+a[i]*(n+i-j),f[i][j-]+a[j]*(n+i-j));
}
}
printf("%d\n",f[][n-]);
}
return ;
}

POJ3186 DP的更多相关文章

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

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

  2. POJ3186(KB12-O DP)

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

  3. poj3186(区间DP)

    题目链接:http://poj.org/problem?id=3186 思路: 区间DP,给treat编号为1..n,状态很明显是上界i和下界j,dp[i][j]表示从下标i到下标j之间数据的最大价值 ...

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

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

  5. poj3186(区间dp)

    题目链接:http://poj.org/problem?id=3186 题意:给一行n个数,每次可以取出行首或者行末的数,如果第ai是第i次取出的,可以得到ai*i的收益,求最大的总收益: 思路:区间 ...

  6. POJ3186【区间DP】

    题意: 每次只能取两端,然后第 i 次取要val[ i ]*i,求一个最大值 一切都是错觉[读者省略此段] 这道题目一开始想的就是记忆化搜索,然后太天真了?好像是,一开始用一维dp[ i ]直接代表一 ...

  7. POJ3186 Treats for the Cows —— DP

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

  8. 「kuangbin带你飞」专题十二 基础DP

    layout: post title: 「kuangbin带你飞」专题十二 基础DP author: "luowentaoaa" catalog: true tags: mathj ...

  9. 各种DP总结

    一.数位DP 1.含有或不含某个数“xx”: HDU3555 Bomb HDU2089 不要62 2.满足某些条件,如能整除某个数,或者数位上保持某种特性: HDU3652 B-number Code ...

随机推荐

  1. OpenMPI 集群配置

    现在有2台机器,希望可以尝试一下在多台机器上跑MPI的感觉,所以跑之前就得配置,先参考网址: https://www.cnblogs.com/awy-blog/p/3402949.html: 1. 配 ...

  2. "Generative Adversarial Nets" Notes

    - Ian J.Goodfellow 中文翻译:https://blog.csdn.net/wspba/article/details/54577236 代码实现:https://github.com ...

  3. 在线求助man page

    一.举例——输入“man date” 图1 图2 图3 二.man之概述 用于:命令的使用说明 用法:man 命令 man page:执行“man 命令”后,出现的屏幕界面 补:man是manual( ...

  4. Thunder团队第六周 - Scrum会议1

    Scrum会议1 小组名称:Thunder 项目名称:i阅app Scrum Master:王航 工作照片: 参会成员: 王航(Master):http://www.cnblogs.com/wangh ...

  5. 20162328蔡文琛week03

    学号 2006-2007-2 <程序设计与数据结构>第X周学习总结 教材学习内容总结 在第三章,我学习到了更多有关于java.util包的知识.了解了多个引用变量可以指向同一个对象.而且J ...

  6. C++读取文件统计单词个数及频率

    1.Github链接 GitHub链接地址https://github.com/Zzwenm/PersonProject-C2 2.PSP表格 PSP2.1 Personal Software Pro ...

  7. 福大软工1816:Alpha(9/10)

    Alpha 冲刺 (9/10) 队名:第三视角 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务: 文字/口头描述: 1.完善通过父子进程调用wxp ...

  8. Java微笔记(4)

    Java 中的内部类 内部类( Inner Class )就是定义在另外一个类里面的类.与之对应,包含内部类的类被称为外部类 内部类的主要作用如下: 内部类提供了更好的封装,可以把内部类隐藏在外部类之 ...

  9. Java常用类之StringBuffer

    StringBuffer 类: 1. java.lang.StringBuffer 代表可变的字符序列: 2. StringBuffer 和 String 类似,但是 StringBuffer 可以对 ...

  10. python学习笔记04:安装pip

    如果是从python官网下载的python版本(2.7.9或3.4)的安装包,其中已经内置了pip工具.那么只需要升级pip即可. 检测是否已安装pip: python -m pip --versio ...