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. 【Template】template中如果包含post方法的form, 要在<form>之后添加{% csrf_token %}标签

    template模板标签{% csrf_token %} 和CSRF middleware提供了易于使用的防“跨站点伪造攻击”的保护, 详情请阅读官方文档https://docs.djangoproj ...

  2. ajax补充FormData

    一.回顾上节知识点 1.什么是json字符串? 轻量级的数据交换格式 2.定时器:关于setTimeout setTimeout(foo,3000)  # 3000表示3秒,foo表示一个函数,3秒后 ...

  3. 类型:NodeJs;问题:默认IE的编码为utf8;结果:IE不能自动选择UTF-8编码解决办法

    在Windows操作系统上使用IE作为浏览器时.常常会发生这样的问题:在浏览使用UTF-8编码的网页时,浏览器无法自动侦测(即没有设定“自动选 择”编码格式时)该页面所用的编码.即使网页已经声明过编码 ...

  4. Delphi IOS (二)

    1.Mac 中 simulator模拟器Home快捷键:command(Win键盘,Ctrl与Alt之间的键)+shift+h来代替,也可以点击菜单>HardWare>Home 2.iPh ...

  5. JSP 按钮点击,onclick事件中的AJAX不执行可能的原因

    <button onclick="deleteAccount()"  >删除</button> 缺少了 type="button" &l ...

  6. 文本框控件JTextField和JTextArea的使用

    -----------------siwuxie095                             工程名:TestUI 包名:com.siwuxie095.ui 类名:TestTextF ...

  7. 使用foreach获取数据列表的全部信息

    先把代码列出来:(在admin/listAdmin.php中) <?php foreach($rows as $row):?> //注意,这里的foreach($rows as $row) ...

  8. 【摘自张宴的"实战:Nginx"】使用nginx的proxy_cache模块替代squid,缓存静态文件

    #user nobody;worker_processes 1; error_log logs/static_source.error.log;#error_log logs/error.log no ...

  9. 在Oracle 12C中使用scott账号

    在Oracle11g中默认是有scott账号的,但在Oracle 12C中则不能直接使用. 我的机器环境: 操作系统:Windows Server 2008 R2 64位 Oracle版本:Oracl ...

  10. 【Boost】boost库获取格式化时间

    获取时间方式 格式一:YYYYMMDD #include<iostream> #include<string> #include<boost/date_time/greg ...