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 ...
随机推荐
- idea 神键
http://blog.csdn.net/dc_726/article/details/42784275 idea 中几个十分酸爽的快捷键.
- zoj 2201 No Brainer
No Brainer Time Limit: 2 Seconds Memory Limit: 65536 KB Zombies love to eat brains. Yum. Input ...
- hihoCoder#1139 二分·二分答案
原题地址 挺简单一道题,结果因为一时傻逼写错一个小地方,导致WA成狗了_(:з」∠)_ 代码: #include <iostream> #include <cstring> # ...
- C# 通过HTTP代理访问Socket来获取邮件
C# 通过HTTP代理访问Socket来获取邮件 关键穿透代理的代码(通过HTTP代理获取TcpClent) public class ClientHelper { public static Tcp ...
- 【ZJOI2017 Round1练习】D2T2 iqtest(排列组合)
题意: 思路: 根据欧拉定理,a^(phi(n)-1)为a mod n的逆元 ..]of longint; s,ans,x,mo,k,phi,tmp:int64; i,m,n,j:longint; f ...
- IText 生成pdf,处理table cell列跨页缺失的问题
/** * 创建(table)PDF,处理cell 跨页处理 * @param savePath(需要保存的pdf路径) * @param pmbs (数据库查询的数据) ...
- Apache 文件根目录设置修改方法 (Document Root)
最近在学习WordPress,使用appServ 在windows上搭建Php开发环境 在网上查找到的关于修改Apache服务器根目录的资料,对比学习,再此记录 在安装 Apache 时,系统会给定一 ...
- APP后端处理视频的方案
在当前的app应用中,到处都能看到视频的身影,例如,在社交类的app上,用户可以拍摄属于自己的小视频,并发布到相应得栏目,增加和好友们互动的机会. 后台常见的视频处理有以下几种: · ...
- HDU 5668 Circle
中国剩余定理. 可以手动模拟一下每一次开始的人的编号和结束的人的编号. 每次删掉一个人,对剩下的人重新编号. 这样一次模拟下来,可以得到n个方程 形如:(u[i]+k)%(n-i+1)=v[i] 化简 ...
- Spring中使用byType实现Beans自动装配
以下内容引用自http://wiki.jikexueyuan.com/project/spring/beans-auto-wiring/spring-autowiring-byType.html: 此 ...