题目链接:http://poj.org/problem?id=3186

Treats for the Cows
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6548   Accepted: 3446

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

 
 
 
一.正向思维:
1.dp[l][r]表示:左边取了l个, 右边取了r个的最大值。
2.枚举左边取了多少个, 再枚举右边取了多少个。
3.对于当前的 dp[l][r],它可以是在dp[l-1][r]的基础上取了a[l];也可以是在dp[l][r-1]的基础上取了a[n+1-r]。所以:
dp[l][r] = max(dp[l-1][r]+a[l], dp[l][r-1]+a[n+1-r])

当然,还需要注意边界条件:l-1>=0,r-1>=0

 
代码如下:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 2e3+; int n;
int a[MAXN], dp[MAXN][MAXN]; int main()
{
while(scanf("%d", &n)!=EOF)
{
for(int i = ; i<=n; i++)
scanf("%d", &a[i]); memset(dp, , sizeof(dp));
for(int l = ; l<=n; l++)
for(int r = ; l+r<=n; r++)
{
if(l!=) dp[l][r] = max(dp[l-][r]+(l+r)*a[l], dp[l][r]);
if(r!=) dp[l][r] = max(dp[l][r], dp[l][r-]+(l+r)*a[n+-r]);
} int ans = -INF;
for(int l = ; l<=n; l++)
ans = max(ans, dp[l][n-l]);
printf("%d\n", ans);
}
}
 
二.逆向思维:
1.逆向推导, 即把过程逆过来,然后就变成了:从中间开始往外取,这样就变成了连续的一段。
2.dp[i][j]表示:区间[i, j]的最大值。
3.枚举区间长度, 然后再枚举起点(终点就确定了)。对于dp[i][j],它可以是在dp[i+1][j]的基础上取了a[i],也可以是在dp[i][j-1]的基础上取了a[j]。两者取其大。
 
 
代码如下:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 2e3+; int n;
int a[MAXN], dp[MAXN][MAXN]; int main()
{
while(scanf("%d", &n)!=EOF)
{
for(int i = ; i<=n; i++)
scanf("%d", &a[i]); for(int i = ; i<=n; i++)
dp[i][i] = a[i]*n; for(int len = ; len<=n; len++)
for(int i = ; i+len-<=n; i++)
{
int j = i+len-;
dp[i][j] = max(dp[i+][j]+(n-len+)*a[i], dp[i][j-]+(n-len+)*a[j]);
} printf("%d\n", dp[][n]);
}
}

三.记忆化搜索:

1.上面的两种方法都要考虑枚举顺序的问题,有时比较不好处理。那么可以用记忆化搜索。

2. 思维与方法二一样,只是写法不同。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 2e3+; int n;
int a[MAXN], dp[MAXN][MAXN]; int dfs(int l, int r)
{
if(l==r) return n*a[l];
if(dp[l][r]!=-) return dp[l][r];
int k = n-r+l; dp[l][r] = max(k*a[l]+dfs(l+, r), k*a[r]+dfs(l,r-));
return dp[l][r];
} int main()
{
while(scanf("%d", &n)!=EOF)
{
for(int i = ; i<=n; i++)
scanf("%d", &a[i]); memset(dp, -, sizeof(dp));
printf("%d\n", dfs(,n));
}
}

POJ3186 Treats for the Cows —— DP的更多相关文章

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

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

  2. poj3186 Treats for the Cows

    http://poj.org/problem?id=3186 Treats for the Cows Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  3. BZOJ 1652: [Usaco2006 Feb]Treats for the Cows( dp )

    dp( L , R ) = max( dp( L + 1 , R ) + V_L * ( n - R + L ) , dp( L , R - 1 ) + V_R * ( n - R + L ) ) 边 ...

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

  5. poj3186 Treats for the Cows(区间)

    题目链接:http://poj.org/problem?id=3186 题意:第一个数是N,接下来N个数,每次只能从队列的首或者尾取出元素. ans=每次取出的值*出列的序号.求ans的最大值. 样例 ...

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

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

  7. 【POJ - 3186】Treats for the Cows (区间dp)

    Treats for the Cows 先搬中文 Descriptions: 给你n个数字v(1),v(2),...,v(n-1),v(n),每次你可以取出最左端的数字或者取出最右端的数字,一共取n次 ...

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

  9. (区间dp + 记忆化搜索)Treats for the Cows (POJ 3186)

    http://poj.org/problem?id=3186   Description FJ has purchased N (1 <= N <= 2000) yummy treats ...

随机推荐

  1. fiddler终极教程

    http://www.cnblogs.com/yoyoketang/tag/fiddler/

  2. python自定义模块导入方法,文件夹,包的区别

    python模块导入,网上介绍的资料很多,方法也众说纷纭.根据自己的实践,感觉这个方法最简单直接,而且可以与主流的python ide生成的工程是一样的. 规则只有三条 1.      严格区分包和文 ...

  3. 一个python爬虫协程的写法(gevent模块)

    from bs4 import BeautifulSoup import requests import gevent from gevent import monkey, pool monkey.p ...

  4. NYOJ-183赚钱啦,bellman//spfa水过,,题还是蛮变态的赶脚~~

    赚钱啦 时间限制:1000 ms  |  内存限制:65535 KB 难度:5 描述 某国家里有N个城市,分别编号为0~N-1,一个精明的商人准备从0号城市旅行到N-1号城市,在旅行的过程中,从一个城 ...

  5. python基础知识--条件判断和循环

    一.输入输出 python怎么来接收用户输入呢,使用input函数,python2中使用raw_input,接收的是一个字符串,输出呢,第一个程序已经写的使用print,代码入下: 1 name=in ...

  6. HDU 3264 区间内的最大最小之差

    题目链接:http://poj.org/problem?id=3264 题目大意:在给定一堆牛的数量以及其高度的时候,每次给定一段区间,求这个区间内最高的牛和最矮的牛的高度之差为多少. 可以直接利用R ...

  7. BZOJ3926 (后缀自动机)

    BZOJ3926 诸神眷顾的幻想乡 Problem : 给一个n个节点的树(n<=10^5), 每个点有一种颜色(c<=10), 询问所有点对之间路径组成字符串的种类.保证叶子节点小于等于 ...

  8. Free Web Application Firewall相关资料

    http://www.freewaf.org/solution/#1 http://baike.soso.com/v60659982.htm

  9. 洛谷—— P3372 【模板】线段树 1

    P3372 [模板]线段树 1 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别 ...

  10. Netflix是什么,与Spring Cloud有什么关系

    说明:以下总结的观点不一定准确,但是是最好理解的. 1.首先,Netflix是一家做视频的网站,可以这么说该网站上的美剧应该是最火的. 2.Netflix是一家没有CTO的公司,正是这样的组织架构能使 ...