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

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的序列,每次只能从队首或队尾取一个数,第几次取 * f[i] 就是利润,求最大利润。

看到题目果断贪心,只能局部最优,因为是dp专题,但是丝毫不会dp,看了题解发现是区间dp,然后看着理解了一下,

dp[i][j] 表示 从第i个数到第j个数的最大利润,由于只能从dp[i+1][j] 和 dp[i][j-1]到达dp[i][j],所以状态转移方程可以表示为 dp[i][j] = max(dp[i+1][j] + f[i] * (n-j+i), dp[i][j-1] + f[j] * (n-j+i),

这里是由里向外递推的,逆向遍历i。用n-j+i表示第几次取(可以模拟一个简单的看看)

初始化条件要注意一下,dp[i][i] = f[i] * n 只有一个数时,它就是最后取的。

 #include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;
#define mem(a,b) memset((a),(b),sizeof(a))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF =0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-;
const ll mod = 1e9+;
//head
const int maxn = + ;
int dp[maxn][maxn], f[maxn];//dp[i][j] 表示从i到j的 最大值 int main() {
int n;
while(~scanf("%d", &n)) {
for(int i = ; i <= n; i++)
scanf("%d", &f[i]);
mem(dp, );
for(int i = ; i <= n; i++)//初始化 每一个都是最后取的
dp[i][i] = f[i] * n;
for(int i = n - ; i >= ; i--) {//从里往外推
for(int j = i + ; j <= n; j++) {// n - j + i 很巧妙
dp[i][j] = max(dp[i+][j] + f[i] * (n - j + i), dp[i][j-] + f[j] * (n - j + i));//转移方程
}
}
printf("%d\n", dp[][n]);
}
}

kuangbin专题十二 POJ3186 Treats for the Cows (区间dp)的更多相关文章

  1. kuangbin专题十二 HDU1074 Doing Homework (状压dp)

    Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

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

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

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

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

  4. 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 ...

  5. kuangbin专题十二 POJ1661 Help Jimmy (dp)

    Help Jimmy Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14214   Accepted: 4729 Descr ...

  6. kuangbin专题十二 HDU1176 免费馅饼 (dp)

    免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  7. kuangbin专题十二 HDU1029 Ignatius and the Princess IV (水题)

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

  8. kuangbin专题十二 HDU1078 FatMouse and Cheese )(dp + dfs 记忆化搜索)

    FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  9. kuangbin专题十二 HDU1260 Tickets (dp)

    Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

随机推荐

  1. hibernate 事务的隔离级别

    脏读不可重复读幻读可序列化(符合事务的四个特性的正常情况 ) 解释: 脏读:事务A对数据1做了更新,但是还没有来得及提交 此时事务B对数据1进行了查询获得了事务A更新后的数据, 但是事务A因为一些原因 ...

  2. Solaris与Windows Active Directory集成

    通过Solaris与Active Directory的集成,Solaris可以使用Windows 2003 R2/ 2008 Active Directory来进行用户登录验证.以下是简要配置过程. ...

  3. Firefox切换页面默认显示语言

    重新安装了下Firefox,发现页面语言变为中文,而我的有些脚本是在英文界面录的,因此想把默认语言改为英文. 方法如下: 工具 - 选项 - 语言(选择...),将英文上移到顶部 在做上面的修改之前, ...

  4. HTML以及DOM选择属性

    http://www.cnblogs.com/season-huang/p/3360869.html

  5. ubuntu16搭建docker私库

    测试环境如下: 一.docker的安装 安装方法请查看这里的 安装教程 二.设置普通用户 1. centos的设置方法 $ sudo gpasswd -a docker ${USER} 2. ubun ...

  6. Replace Pioneer注册方法

    Replace Pioneer注册方法 Replace Pioneer过期后,会弹出一个注册(Registration)窗口,其中有一个试用选项(Trial License),点击Trial Lice ...

  7. transient关键字的理解

    谈到这个transient这个关键字,我们应该会立马想到序列化这个过程:什么是序列化?什么又是反序列化呢?序列化就是将对象转化内成二进制,而反序列化就是就二进制文件转换成对象的过程.一旦变量使用了tr ...

  8. CentOS7:ifconfig command not found解决和netstat -an

    没有 ifconfig 和netstat -an 的话安装 net-tools package yum install net-tools

  9. ES02 变量、数组、对象、方法

    1 变量 1.1 变量的声明 利用var关键字来声明变量,例如: var a = 100; <!DOCTYPE html> <html> <head> <me ...

  10. HDOJ 1023 Train Problem II 卡特兰数

    火车进站出站的问题满足卡特兰数...卡特兰数的相关知识如下: 卡特兰数又称卡塔兰数,是组合数学中一个常出现在各种计数问题中出现的数列.由以比利时的数学家欧仁·查理·卡塔兰 (1814–1894)命名. ...