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 ...
随机推荐
- jenkins 提示No emails were triggered
发送邮件 Jenkins->系统管理->系统设置,在“邮件通知”里设置smtp服务器地址,以及发送者邮箱地址,在具体的任务构建完成以后,可以设置发送邮件,在某一个任务的"Add ...
- 【java基础 3】树形结构数据呈现的递归算法实现
一.基本概况 在我的项目中,常常会用到树形结构的数据,最为明显的就是左边菜单栏,类似于window folder一样的东西. 而我之前一直是借助前端封装好的ZTree等工具实现展示,而后台则通常使用递 ...
- [luoguP2831] 愤怒的小鸟(状压DP)
传送门 感觉这题不是很难,但是很恶心. 说一下几点. 1.预处理出来每两个点所构成的抛物线能消除的猪的集合. 2.如果两个点横坐标相同,则不能构成抛物线 3.a >= 0 continue 4. ...
- 使用WaveOut API播放WAV音频文件(解决卡顿)
虽然waveout已经过时,但是其api简单,有些时候也还是需要用到. 其实还是自己上msdn查阅相应api最靠谱,waveout也有提供暂停.设置音量等接口的,这里给个链接,需要的可以自己查找: h ...
- android开发里跳过的坑-电源锁WakeLock不起作用
由于个别需求,需要做一个不让系统屏常亮不休眠的APP,于是使用了电源锁,在oncreate里加了如下代码: PowerManager pm = (PowerManager) context .getS ...
- 《TCP/IP详解卷1:协议》——第5章 RARP:逆地址解析协议(转载)
1.引言 具有本地磁盘的系统引导时,一般是从磁盘上的配置文件中读取IP地址.但是无盘机,如X终端或无盘工作站,则需要采用其他方法来获得IP地址. 网络上的每个系统都具有唯一的硬件地址,它是由网络接口生 ...
- php 基础复习 2018-06-19
(1)date()函数 如果从代码返回的不是正确的时间,有可能是因为您的服务器位于其他国家或者被设置为不同时区. 因此,如果您需要基于具体位置的准确时间,您可以设置要用的时区. date_defaul ...
- 库操作&表操作
系统数据库 ps:系统数据库: mysql 授权库,主要存储系统用户的 权限信息 test MySQL数据库系统自动创建的 测试数据库 ination_schema 虚拟库,不占用磁盘空间,存储的是数 ...
- Animation显示ListView的每一条记录
activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...
- HDU 1558
输入线段的两个短点,如果线段相交那么他们属于一个集合,查看第i条线段所在的集合有几条线段. 好久没码码了,总是各种蠢. 首先找出两条直线的方程,求解相交点的横坐标,然后看是不是在线段内部. 没有注意题 ...