POJ3186 Treats for the Cows —— DP
题目链接:http://poj.org/problem?id=3186
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 6548 | Accepted: 3446 |
Description
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
Lines 2..N+1: Line i+1 contains the value of treat v(i)
Output
Sample Input
5
1
3
1
5
2
Sample Output
43
Hint
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[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);
}
}
#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的更多相关文章
- kuangbin专题十二 POJ3186 Treats for the Cows (区间dp)
Treats for the Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7949 Accepted: 42 ...
- poj3186 Treats for the Cows
http://poj.org/problem?id=3186 Treats for the Cows Time Limit: 1000MS Memory Limit: 65536K Total S ...
- 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 ) ) 边 ...
- 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 ...
- poj3186 Treats for the Cows(区间)
题目链接:http://poj.org/problem?id=3186 题意:第一个数是N,接下来N个数,每次只能从队列的首或者尾取出元素. ans=每次取出的值*出列的序号.求ans的最大值. 样例 ...
- POJ3186:Treats for the Cows(区间DP)
Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for gi ...
- 【POJ - 3186】Treats for the Cows (区间dp)
Treats for the Cows 先搬中文 Descriptions: 给你n个数字v(1),v(2),...,v(n-1),v(n),每次你可以取出最左端的数字或者取出最右端的数字,一共取n次 ...
- 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 ...
- (区间dp + 记忆化搜索)Treats for the Cows (POJ 3186)
http://poj.org/problem?id=3186 Description FJ has purchased N (1 <= N <= 2000) yummy treats ...
随机推荐
- robot framework常见错误:RIDE运行一次后不显示log
在使用RIDE进行web自动化测试时,会发现经常运行第二遍不显示下方log,如下 原因: 代码使用的是谷歌浏览器.IE浏览器测试,运行一次后chromedriver.exe,IEDriverServe ...
- LR手动关联参数化问题总结
所谓的关联就是把脚本中某些写死的代码(hard-coded)数据,转变成截取自服务器所送的.动态的.每次都不一样的数据. 一般情况下,比较聪明的服务器在每个浏览器第一次跟它要数据时,都会在数据中夹带一 ...
- python蛋疼的编码decode、encode、unicode、str、byte的问题都在这了
相信很多人和我一样,被python蛋疼的编码问题纠缠不清,比如下面的 私以为出现这种错误的原因还是对一些基本的编解码概念不够熟悉,下面就说说我的理解: 首先python刚出来的时候unicode还没有 ...
- dfs树上的边
by GeneralLiu 一 开 始 学 tarjan 的 强连通分量 , 割边 , 割点 时 没有 学扎实 经过培训 ,发现了些 需要注意的 小细节 举个荔枝 dfs树 上的 边 学了 tar ...
- Codeforces474E - Pillars
Portal Description 给出一个\(n(n\leq10^5)\)的正整数序列\(\{a_n\}(a_i\leq10^{15})\)和正整数\(d(d\leq10^9)\),求\(\{a_ ...
- POJ2014 Flow Layout
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3161 Accepted: 2199 Description A f ...
- THUPC2018看题总结
THUPC2018看题总结 #6387. 「THUPC2018」绿绿与串串 / String 据说是签到题啊. 首先根据题目的意思,我们发现如果能找到那个最后一次选择的对称轴岂不是美滋滋. 自然地,我 ...
- P1093||T1142 奖学金 洛谷||codevs
http://codevs.cn/problem/1142/ || https://www.luogu.org/problem/show?pid=1093 题目描述 某小学最近得到了一笔赞助,打算拿出 ...
- Spring的Web MVC框架
以下内容引用自http://wiki.jikexueyuan.com/project/spring/web-mvc-framework.html: Spring web MVC框架提供了模型-视图-控 ...
- 制作自己的网站第二步***在Linux上装上需要的软件以及部署项目配置**
在购买自己的服务器后,如果想要把项目跑起来,就得安装一些必要的软件. 这里只说一些最基础最基本最不可或缺的几个.其他的可以根据自己的需要 安装使用. 首先,那就是配置jdk了,我们可以通过一些工具把下 ...