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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
using namespace std;
typedef long long LL;
#define N 2005
#define met(a,b) (memset(a,b,sizeof(a))) int a[N], dp[N][N], n; int DFS(int L, int R, int k)
{
if(L>R || L< || R< || R>n || L>n) return -; if(dp[L][R]!=-)
return dp[L][R]; dp[L][R] = ;
dp[L][R] = max(DFS(L+, R, k+) + a[L]*k, DFS(L, R-, k+) + a[R]*k); return dp[L][R];
} int main()
{ while(scanf("%d", &n)!=EOF)
{
int i; met(dp, -);
met(a, ); for(i=; i<=n; i++)
scanf("%d", &a[i]); for(i=; i<=n; i++)
dp[i][i] = a[i]*n; dp[][n] = DFS(, n, ); printf("%d\n", dp[][n]);
}
return ;
}
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
using namespace std;
typedef long long LL;
#define N 2005
#define met(a,b) (memset(a,b,sizeof(a))) int a[N], dp[N][N], n; int main()
{ while(scanf("%d", &n)!=EOF)
{
int i, j, l; met(dp, );
met(a, ); for(i=; i<=n; i++)
scanf("%d", &a[i]); for(i=; i<=n; i++)
dp[i][i] = a[i]*n; for(l=; l<n; l++)
{
for(i=; i+l<=n; i++)
{
j = i+l;
dp[i][j] = max(dp[i+][j]+a[i]*(n-l), dp[i][j-]+a[j]*(n-l));
}
} printf("%d\n", dp[][n]);
}
return ;
}

(区间dp + 记忆化搜索)Treats for the Cows (POJ 3186)的更多相关文章

  1. UVA 10003 Cutting Sticks 区间DP+记忆化搜索

    UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...

  2. uva 10891 区间dp+记忆化搜索

    https://vjudge.net/problem/UVA-10891 给定一个序列x,A和B依次取数,规则是每次只能从头或者尾部取走若干个数,A和B采取的策略使得自己取出的数尽量和最大,A是先手, ...

  3. loj 1031(区间dp+记忆化搜索)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1031 思路:dp[i][j]表示从区间i-j中能取得的最大值,然后就是枚举分割点了. ...

  4. BZOJ1055[HAOI2008]玩具取名 【区间dp + 记忆化搜索】

    题目 某人有一套玩具,并想法给玩具命名.首先他选择WING四个字母中的任意一个字母作为玩具的基本名字.然后 他会根据自己的喜好,将名字中任意一个字母用“WING”中任意两个字母代替,使得自己的名字能够 ...

  5. HDU 2517 / POJ 1191 棋盘分割 区间DP / 记忆化搜索

    题目链接: 黑书 P116 HDU 2157 棋盘分割 POJ 1191 棋盘分割 分析:  枚举所有可能的切割方法. 但如果用递归的方法要加上记忆搜索, 不能会超时... 代码: #include& ...

  6. hdu 4597 Play Game(区间dp,记忆化搜索)

    Problem Description Alice and Bob are playing a game. There are two piles of cards. There are N card ...

  7. poj 1088 滑雪(区间dp+记忆化搜索)

    题目链接:http://poj.org/problem?id=1088 思路分析: 1>状态定义:状态dp[i][j]表示在位置map[i][j]可以滑雪的最长区域长度: 2>状态转移方程 ...

  8. Ural 1183 Brackets Sequence(区间DP+记忆化搜索)

    题目地址:Ural 1183 最终把这题给A了.. .拖拉了好长时间,.. 自己想还是想不出来,正好紫书上有这题. d[i][j]为输入序列从下标i到下标j最少须要加多少括号才干成为合法序列.0< ...

  9. 洛谷1880 区间dp+记忆化搜索 合并石子

    题目网址:https://www.luogu.com.cn/problem/P1880 题意是:给定一个序列,最小规则是相邻两个值的合并,开销是他们的和,将整个序列合并成一个值的情况下,求解该值的最小 ...

随机推荐

  1. JS如何获取上传标签的文件路径和文件名?

    如何使用JS获取type="file"的标签上传文件的文件路径及文件名: 代码: <!doctype html><html lang="en" ...

  2. python 数据类型 之 tuple 元组

    python 3.6.5 元组的特性和定义 与列表类型 只不过 [  ] 改成了() 特性: 1.不可变(元组本身不可变,但是可以存可变类型的element){猜测因为可变element的地址不可变而 ...

  3. python 之 基础

    变量 变量的作用: 标识符的命名规范: 掌握常量与变n量的区别: 变量定义规范: 声明变量: name='Alex Li' 三部分:变量名 赋值运算符 变量值 变量定义规则:1.变量名只能是字母.数字 ...

  4. python time 和 datetime 模块

    时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运行“type(time.time())”,返回的是float类型. 格式化的时间字 ...

  5. nginx默认配置和默认站点启动

    1.nginx的配置文件nginx.conf cd /etc/nginx/ vim nginx.conf 打开后的文件为: user nginx;worker_processes 1; error_l ...

  6. 可迭代对象(Iterable)和迭代器(Iterator)

     迭代是访问集合元素的一种方式. 迭代器是一个可以记住遍历的位置的对象. 迭代器对象从集合的第一 个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退. 1. 可迭代对象 以直接作用于 ...

  7. day1-windows下python和selenium的安装

    这是一个完整的安装包,下载下来是一个.exe的文件 只需双击,下一步下一步默认安装即可 python从2.7开始都会携带pip插件,做了scripe的环境变量可以,在网络畅通的情况下可以在cmd的命令 ...

  8. Delphi--最强大的开发工具(欢迎转载)

    最强大的开发工具 Delphi 目录 --------------------------------------------------------------------------- 前言 De ...

  9. Why Linux Doesn’t Need Defragmenting

    If you’re a Linux user, you’ve probably heard that you don’t need to defragment your Linux file syst ...

  10. idea窗口下方滚动条不明显设置

    在使用idea时,下方的滚动条老是显示不明显,每次点击拖拽都很费劲,在网上找了很多相关设置,最后确定了一个最好的办法解决问题: Shift (上档) +  鼠标滚动,这样就可以横向翻滚了,很方便 此方 ...