(区间dp + 记忆化搜索)Treats for the Cows (POJ 3186)
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.
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
using namespace std;
typedef long long LL;
#define N 2005
#define met(a,b) (memset(a,b,sizeof(a))) int a[N], dp[N][N], n; int DFS(int L, int R, int k)
{
if(L>R || L< || R< || R>n || L>n) return -; if(dp[L][R]!=-)
return dp[L][R]; dp[L][R] = ;
dp[L][R] = max(DFS(L+, R, k+) + a[L]*k, DFS(L, R-, k+) + a[R]*k); return dp[L][R];
} int main()
{ while(scanf("%d", &n)!=EOF)
{
int i; met(dp, -);
met(a, ); for(i=; i<=n; i++)
scanf("%d", &a[i]); for(i=; i<=n; i++)
dp[i][i] = a[i]*n; dp[][n] = DFS(, n, ); printf("%d\n", dp[][n]);
}
return ;
}
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
using namespace std;
typedef long long LL;
#define N 2005
#define met(a,b) (memset(a,b,sizeof(a))) int a[N], dp[N][N], n; int main()
{ while(scanf("%d", &n)!=EOF)
{
int i, j, l; met(dp, );
met(a, ); for(i=; i<=n; i++)
scanf("%d", &a[i]); for(i=; i<=n; i++)
dp[i][i] = a[i]*n; for(l=; l<n; l++)
{
for(i=; i+l<=n; i++)
{
j = i+l;
dp[i][j] = max(dp[i+][j]+a[i]*(n-l), dp[i][j-]+a[j]*(n-l));
}
} printf("%d\n", dp[][n]);
}
return ;
}
(区间dp + 记忆化搜索)Treats for the Cows (POJ 3186)的更多相关文章
- UVA 10003 Cutting Sticks 区间DP+记忆化搜索
UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...
- uva 10891 区间dp+记忆化搜索
https://vjudge.net/problem/UVA-10891 给定一个序列x,A和B依次取数,规则是每次只能从头或者尾部取走若干个数,A和B采取的策略使得自己取出的数尽量和最大,A是先手, ...
- loj 1031(区间dp+记忆化搜索)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1031 思路:dp[i][j]表示从区间i-j中能取得的最大值,然后就是枚举分割点了. ...
- BZOJ1055[HAOI2008]玩具取名 【区间dp + 记忆化搜索】
题目 某人有一套玩具,并想法给玩具命名.首先他选择WING四个字母中的任意一个字母作为玩具的基本名字.然后 他会根据自己的喜好,将名字中任意一个字母用“WING”中任意两个字母代替,使得自己的名字能够 ...
- HDU 2517 / POJ 1191 棋盘分割 区间DP / 记忆化搜索
题目链接: 黑书 P116 HDU 2157 棋盘分割 POJ 1191 棋盘分割 分析: 枚举所有可能的切割方法. 但如果用递归的方法要加上记忆搜索, 不能会超时... 代码: #include& ...
- hdu 4597 Play Game(区间dp,记忆化搜索)
Problem Description Alice and Bob are playing a game. There are two piles of cards. There are N card ...
- poj 1088 滑雪(区间dp+记忆化搜索)
题目链接:http://poj.org/problem?id=1088 思路分析: 1>状态定义:状态dp[i][j]表示在位置map[i][j]可以滑雪的最长区域长度: 2>状态转移方程 ...
- Ural 1183 Brackets Sequence(区间DP+记忆化搜索)
题目地址:Ural 1183 最终把这题给A了.. .拖拉了好长时间,.. 自己想还是想不出来,正好紫书上有这题. d[i][j]为输入序列从下标i到下标j最少须要加多少括号才干成为合法序列.0< ...
- 洛谷1880 区间dp+记忆化搜索 合并石子
题目网址:https://www.luogu.com.cn/problem/P1880 题意是:给定一个序列,最小规则是相邻两个值的合并,开销是他们的和,将整个序列合并成一个值的情况下,求解该值的最小 ...
随机推荐
- Exploring the world of Android :: Part 1
This blog is accidentally find out, it tells the story of one of our friends about the exploration o ...
- MBP 使用笔记
1.svn下载指令(终端) svn checkout https://svn.openslam.org/data/svn/gmapping 参考:http://blog.csdn.net/q19910 ...
- js计算日期增加
<div class="time"> <i class="visa_icon prev"></i><span id=& ...
- 23.Mysql应用优化
23.应用优化23.1 使用连接池应用启动时创建好连接,以供用户使用,而不是每次创建. 23.2 减少对Mysql的访问 23.2.1 避免对同一数据做重复检索合并简单查询,减少访问次数. 23.2. ...
- C&Cpp.CallGraph
1. CodeViz http://www.skynet.ie/~mel/projects/codeviz/ 2. http://my.oschina.net/zmlblog/blog/186308
- Laravel 利用 observer 类基于状态属性,对进行删除和修改的控制
1 我们知道 Observer 类可以监听模型类的相关事件 1.1 creating, created, updating, updated, saving, saved, deleting, del ...
- list,set等集合遍历时,不能remove集合中的元素。需要new一个Object或者list,set,里面add需要删除的元素,等集合遍历完了进行remove(Object)或者removeAll(list/set)操作
list,set等集合遍历时,不能remove集合中的元素.需要new一个Object或者list,set,里面add需要删除的元素,等集合遍历完了进行remove(Object)或者removeAl ...
- Java SE EE ME用处
Java SE: 又称J2SE,开发部署桌面应用程序: Java EE:又称J2EE,开发网站 Java ME:是做手机APP开发 EE在SE基础上构建,提供web服务.组件模型.管理和通信API
- robotframework 运行集合
Robot Framework 运行测试通过 pybot 命令,检查 _C:\Python36\Scripts_ 目录下是否有 pybot.bat 文件,正确安装 Robot Framework 一定 ...
- iOS中四种实例变量的范围类型@private@protected@public@package
文档上记录是这样的 The Scope of Instance Variables Toenforce the ability of an object to hide its data, the c ...