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

 
题意:大概意思是,一批红酒开始有自身的价值,每天卖出第一个或最后一个,并且卖出的价值等于 原先的价值*存在的年数,求卖完所有的红酒能获得的最大价值
 
看完下面的提示立刻就想到了区间dp,dp[i][j]表示从i到j的最大价值,状态转移方程为dp[i][j]=max(dp[i+1][j]+a[i]*year,dp[i][j-1]+a[j]*a[j]*year);
 
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
#include<map>
using namespace std;
#define N 2006
int n;
int a[N];
int dp[N][N];
int main()
{
while(scanf("%d",&n)==1)
{
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
dp[i][i]=a[i]*n;
} for(int len=1;len<n;len++)
{
for(int i=1;i+len<=n;i++)
{
int j=i+len;
dp[i][j]=max(dp[i+1][j]+a[i]*(n-(j-i+1)+1),dp[i][j-1]+a[j]*(n-(j-i+1)+1));
}
} printf("%d\n",dp[1][n]);
}
return 0;
}

另外一种写法

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
#include<map>
using namespace std;
#define N 2006
int n;
int a[N];
int dp[N][N];
int main()
{
while(scanf("%d",&n)==1)
{
for(int i=1;i<=n;i++) scanf("%d",&a[i]); memset(dp,0,sizeof(dp));
for(int i=n;i>=1;i--)
{
for(int j=i;j<=n;j++)
{
dp[i][j]=max(dp[i+1][j]+a[i]*(n-(j-i+1)+1),dp[i][j-1]+a[j]*(n-(j-i+1)+1));
}
}
printf("%d\n",dp[1][n]);
}
return 0;
}

poj 3186 Treats for the Cows(区间dp)的更多相关文章

  1. POJ 3186 Treats for the Cows ——(DP)

    第一眼感觉是贪心,,果断WA.然后又设计了一个两个方向的dp方法,虽然觉得有点不对,但是过了样例,交了一发,还是WA,不知道为什么不对= =,感觉是dp的挺有道理的,,代码如下(WA的): #incl ...

  2. poj 3186 Treats for the Cows(dp)

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

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

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

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

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

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

  6. POJ 3186 Treats for the Cows (动态规划)

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

  7. Treats for the Cows 区间DP POJ 3186

    题目来源:http://poj.org/problem?id=3186 (http://www.fjutacm.com/Problem.jsp?pid=1389) /** 题目意思: 约翰经常给产奶量 ...

  8. POJ 3186 Treats for the Cows 一个简单DP

    DP[i][j]表示现在开头是i物品,结尾是j物品的最大值,最后扫一遍dp[1][1]-dp[n][n]就可得到答案了 稍微想一下,就可以, #include<iostream> #inc ...

  9. POJ 3186 Treats for the Cows

    简单DP dp[i][j]表示的是i到j这段区间获得的a[i]*(j-i)+... ...+a[j-1]*(n-1)+a[j]*n最大值 那么[i,j]这个区间的最大值肯定是由[i+1,j]与[i,j ...

随机推荐

  1. JS~模拟表单在新窗口打开,避免广告拦截

    说起广告拦截,这应该是浏览器的一个特性,它会将window.open产生的窗口默认为一个广告,将它进行拦截,但有时,这不是我们所希望的,有时,我们就是需要它在客户端的浏览器上弹出一个新窗口,以展示数据 ...

  2. 数组字符串与指针字符串的区别 char s[]="***" 和char *s="***"的区别

    char s[] = "wangshihui"; char *s = "wangshihui"; 皆宣告了s字符串,在C-style string的函数皆可使用 ...

  3. angularjs金额大写过滤器

    数字转中文 MyAppFilter.filter('rmbFilter',[function(){ function ChinaCost(input){ var numberValue=new Str ...

  4. css圆角

    在CSS3中圆角属性,有四个.三个.两个和一个值. 四个值: 第一个值为左上角,第二个值为右上角,第三个值为右下角,第四个值为左下角.

  5. Beacon浅析

    作者:hongbosun 一.Beacon简介 Beacon是基于BLE技术实现的物理设备.BLE(全称Bluetooth Low Energy)是蓝牙4.0技术规范的一部分.它起源于Nokia的Wi ...

  6. asp.net 面试基础题

    WebSite和WebApplication的区别1)当改变后台代码时,WebApplication需重启浏览器或者重新生成解决方案,而WebSite则不用:2)WebSite没有Solution,没 ...

  7. do/while(0) c4127

    原文链接:http://cnicholson.net/2009/03/stupid-c-tricks-dowhile0-and-c4127/ // NOISY CODE #define MULTI_L ...

  8. jquery多级联动(ajax查数据库)

    /id 代表下级下拉框ID,cityCode代表的是父级菜单代码,所有级菜单在同一张表,后台在加载是把菜单已经加入到Map缓存中.... //id 代表下级下拉框ID,cityCode代表的是父级菜单 ...

  9. Ubuntu与Ubuntu系统之间的mount挂载

    1.被挂载机 1>安装Ubuntu nfs apt-get install nfs-kernel-server 2>配置/etc/exports Ubuntu nfs允许挂载的目录及权限在 ...

  10. php提取身份证号码中的生日日期以及验证是否为未成年人的函数

    php 提取身份证号码中的生日日期以及确定是否成年的一个函数.可以同时确定15位和18位的身份证,经本人亲测,非常好用,分享函数代码如下: <?php //用php从身份证中提取生日,包括15位 ...