Treats for the Cows
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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.
/*
题意:给你一个双向队列,每次可以从队首,或者从队尾取出元素,每次操作会获得相应的价值,第i个取出的元素a
得到的价值就是i*a,问你能取出的最大价值是多少 初步思路:先贪心搞一发试试 #改进:用一个数组b 存放着 逆序的数组 a,dp[i][j]表示,a数组取i 个,b数组取 j个时的最大值,得到状态转移方程:
dp[i][j]=max(dp[i-1][j]+a[i]*(i+j),dp[i][j-1]+b[j]*(i+j)); 实际上就是 左边取 i 个,右边取 j
个的最大值 #错误:上面的初始化,没法搞,当i等于零或者j等于零的时候,问题就重新转化为题目要求的问题了,换一个思路,从里面向
外边扩,不从边上开始,从里往外,因为最终的顶点并不是确定的,dp[i][j]表示从i到j能得到的最大价值,得到状态
转移方程: dp[i][j]=max( dp[i+1][j]+a[i]*( n-j+i ), dp[i][j-1]+a[j]*( n-j+i ) ); #错误:得不到正确的结果 #注意:上面的想法没错,但是i是逆向循环的,因为正向循环的话,记录的状态都是接下来的状态,根本没有参考上一个状态,
进行判断结果
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int n;
int l,r;
int a[];
int dp[][];
void init(){
memset(dp,,sizeof dp);
memset(a,,sizeof a);
}
int main(){
// freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF){
init();
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
dp[i][i]=a[i]*n;//初始化每一个最后拿的话都是,a[i]*n
}
for(int i=n-;i>=;i--){
for(int j=i+;j<=n;j++){
dp[i][j]=max( dp[i+][j]+a[i]*( n-j+i ), dp[i][j-]+a[j]*( n-j+i ) );
// cout<<"( "<<i<<" ,"<<j<<" ) "<<( n-j+i )<<endl;
}
}
// for(int i=1;i<=n;i++){
// for(int j=1;j<=n;j++){
// cout<<dp[i][j]<<" ";
// }
// cout<<endl;
// }
printf("%d\n",dp[][n]);
}
return ;
}
Treats for the Cows的更多相关文章
- 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 Treats for the Cows
http://poj.org/problem?id=3186 Treats for the Cows Time Limit: 1000MS Memory Limit: 65536K Total S ...
- 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 ...
- 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 ) ) 边 ...
- BZOJ 1652: [Usaco2006 Feb]Treats for the Cows
题目 1652: [Usaco2006 Feb]Treats for the Cows Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 234 Solve ...
- POJ 3186 Treats for the Cows (动态规划)
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)
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 ...
- [BZOJ 1652][USACO 06FEB]Treats for the Cows 题解(区间DP)
[BZOJ 1652][USACO 06FEB]Treats for the Cows Description FJ has purchased N (1 <= N <= 2000) yu ...
随机推荐
- 【设计模式】module(模块)模式
写在前面 最近刚接触到设计模式, <head first设计模式>里有一篇文章,是说使用模式的心智, 1.初学者"心智" :"我要为HELLO WORLD找个 ...
- CSS3微信启动页天王星版
今天被微信启动页刷屏了. 一直还以为启动页背景显示的月球的.今天才了解到这么有含义. 我也蹭一下微信的热度,做一个HTML+CSS版本的. 用CSS画地球太困难了,来个简单点的,天王星版. 主要使用到 ...
- AngularJS -- 提供者(Providers)
点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ 每个Web应用程序都是有多个对象组合.协作来完成任务的.这些对象需要被实例化,并且连接在 ...
- Hbase对时,时差范围的确定
Hbase对时具有严格的要求,集群内部所有机器之间的时差默认不能超过30秒,也就是说,一旦某个regionserver节点上的时间与master节点上的时间差值超过30秒,就会导致相应的regions ...
- POJ-2417-Discrete Logging(BSGS)
Given a prime P, 2 <= P < 2 31, an integer B, 2 <= B < P, and an integer N, 1 <= N &l ...
- OpenVPN CentOS7 安装部署配置详解
一 .概念相关 1.vpn 介绍 vpn 虚拟专用网络,是依靠isp和其他的nsp,在公共网络中建立专用的数据通信网络的技术.在vpn中任意两点之间的链接并没有传统的专网所需的端到端的物理链路,而是利 ...
- php中如何给类规范的注释
@access 使用范围:class,function,var,define,module 该标记用于指明关键字的存取权限:private.public或proteced @author 指明作者 @ ...
- Jquery Ajax和getJSON获取后台普通Json数据和层级Json数据解析
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 通过ssh秘钥的方式可以连接上CE68交换机
结论:按照CE68交换机的用户手册中的指导,可以通过ssh 秘钥的方式连接上交换机. 1.先按照eNSP连接到网卡的方式,给CE68配置一个ip地址: 192.168.56.2 2.按照交换机的用户指 ...
- Mybatis的parameterType传入多个参数
如果查询的条件有多个的时候,mybatis有三种传入方式: 1.通过注解传入 例如: public interface Mapper(){ public User login(@Param(" ...