POJ3186(KB12-O DP)
Treats for the Cows
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5801 | Accepted: 3003 |
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
//2017-04-06
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int N = ;
int v[N], n, dp[N][N];//dp[l][r]表示区间l~r间的最大收益
//状态转移方程:dp[l][r] = max(dp[l+1][r]+day*v[l], dp[l][r-1]+day*v[r]) int dfs(int l, int r, int day)
{
if(l > r)return ;
if(dp[l][r])return dp[l][r];
if(l == r)return dp[l][r] = day*v[l];
return dp[l][r] = max(dfs(l+, r, day+)+day*v[l], dfs(l, r-, day+)+day*v[r]);
} int main()
{
while(scanf("%d", &n)!=EOF)
{
for(int i = ; i < n; i++)
scanf("%d", &v[i]);
memset(dp, , sizeof(dp));
int ans = dfs(, n-, );
printf("%d\n", ans);
} return ;
}
POJ3186(KB12-O DP)的更多相关文章
- poj3186(区间DP)
题目链接:http://poj.org/problem?id=3186 思路: 区间DP,给treat编号为1..n,状态很明显是上界i和下界j,dp[i][j]表示从下标i到下标j之间数据的最大价值 ...
- POJ3186【区间DP】
题意: 每次只能取两端,然后第 i 次取要val[ i ]*i,求一个最大值 一切都是错觉[读者省略此段] 这道题目一开始想的就是记忆化搜索,然后太天真了?好像是,一开始用一维dp[ i ]直接代表一 ...
- POJ3186:Treats for the Cows(区间DP)
Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for gi ...
- POJ3186 DP
Treats for the Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5753 Accepted: 29 ...
- kuangbin专题十二 POJ3186 Treats for the Cows (区间dp)
Treats for the Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7949 Accepted: 42 ...
- poj3186(区间dp)
题目链接:http://poj.org/problem?id=3186 题意:给一行n个数,每次可以取出行首或者行末的数,如果第ai是第i次取出的,可以得到ai*i的收益,求最大的总收益: 思路:区间 ...
- POJ3186 Treats for the Cows —— DP
题目链接:http://poj.org/problem?id=3186 Treats for the Cows Time Limit: 1000MS Memory Limit: 65536K To ...
- 「kuangbin带你飞」专题十二 基础DP
layout: post title: 「kuangbin带你飞」专题十二 基础DP author: "luowentaoaa" catalog: true tags: mathj ...
- 各种DP总结
一.数位DP 1.含有或不含某个数“xx”: HDU3555 Bomb HDU2089 不要62 2.满足某些条件,如能整除某个数,或者数位上保持某种特性: HDU3652 B-number Code ...
随机推荐
- GoLang学习之数据类型
数据类型 Go语言按类别有以下几种数据类型: bool,一个字节,值是true或者false,不可以用0或者1表示 int/uint(带符号为与不带符号位的int类型):根据平台不同是32位或者64位 ...
- Swift5 语言指南(二十一) 嵌套类型
通常创建枚举以支持特定类或结构的功能.类似地,定义纯粹在更复杂类型的上下文中使用的实用程序类和结构可能是方便的.为此,Swift允许您定义嵌套类型,从而在它们支持的类型定义中嵌套支持枚举,类和结构. ...
- ASP.NET Core WebApi 项目部署到 IIS 服务器的总结
Point: - ASP.NET Core WebApi 项目 - 发布到 IIS 服务器 1. 选择 File System 2. 输入要发布到的路径 # 其它默认,直接发布 3. 打开 IIS,添 ...
- vue教程2-03 vue计算属性的使用 computed
vue教程2-03 vue计算属性的使用 computed computed:{ b:function(){ //默认调用get return 值 } } ---------------------- ...
- Winform 多线程--解决界面卡死问题
public class ThreadInvoker { /// <summary> /// 回调委托 带参数的 /// </summary> /// <param na ...
- c++实现二叉树层序、前序创建二叉树,递归非递归实现二叉树遍历
#include <iostream> #include <cstdio> #include <stdio.h> #include <string> # ...
- setPreferredContentSize error in ios app
Creating "IOS Project" in xcode 5 causes the following when launching for iPad simulator. ...
- 全网最详细的Windows系统里PLSQL Developer 64bit安装之后的一些配置(图文详解)
不多说,直接上干货! 注意的是: 本地若没有安装Oracle服务端,Oracle server服务端64位,是远程连接,因此本地配置PLSQL Developer64位. PLSQL Develope ...
- POJ1065 Wooden Sticks(贪心+动态规划——单调递减或递增序列)
描述 C小加有一些木棒,它们的长度和质量都已经知道,需要一个机器处理这些木棒,机器开启的时候需要耗费一个单位的时间,如果第i+1个木棒的重量和长度都大于等于 第i个处理的木棒,那么将不会耗费时间,否则 ...
- Linux-(lsof,ifconfig,route)
lsof命令 1.命令格式: lsof [参数][文件] 2.命令功能: lsof(list open files)是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通 ...