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 ...
随机推荐
- 凸包GiftWrapping GrahamScan 算法实现
开始 游戏内有需求做多边形碰撞功能,但是接入box2d相对游戏的需求来说太重度了.所以准备自己实现碰撞. 确定多边形,必然要用到凸包的算法.在github上也找到了一些lua实现,但是这里的算法没有考 ...
- global,local,static的区别
1.在函数内部使用global关键字定义的变量可以成为全局变量,如果该变量已经被定义了,那么他的值就是原来的值,否则就是一个新的全局变量(一句话:已存在就不再创建): <?php $a=1; f ...
- 小知识点-ios跳过app store更新版本
版本更新实现的思路 获取自身的版本号 获取AppStore的版本号 自身的版本号和AppStore的比较 弹窗提示所需数据的获取的方式 1.获取自身的版本号 2.AppStore的版本号 Wechat ...
- 微信bug:建议了解,不要实验,不要手贱,不要。。。。
今天下午在群里聊天的时候,群友反应发现微信的一个bug:使用微信给好友发送‘15...............’(数字15后面加15个句号)会导致微信运行缓慢,到最后的应用未响应,退出微信. 解决办法 ...
- MySQL高级查询(二)
EXISTS 和NOT EXISTS子查询 EXISTS子查询 语法: SELECT ……… FROM 表名 WHERE EXISTS (子查询); 例: SELECT `studentNo` A ...
- Symbol
ES5 的对象属性名都是字符串,这容易造成属性名的冲突.比如,你使用了一个他人提供的对象,但又想为这个对象添加新的方法(mixin 模式),新方法的名字就有可能与现有方法产生冲突.如果有一种机制,保证 ...
- 斐波那契数列第N项f(N)[矩阵快速幂]
矩阵快速幂 定义矩阵A(m*n),B(p*q),A*B有意义当且仅当n=p.即A的列数等于B的行数. 且C=A*B,C(m*q). 例如: 进入正题,由于现在全国卷高考不考矩阵,也没多大了解.因为遇到 ...
- FTP基本操作类大全,外加c#基础公共帮助类
总结平时用到的一些FTP操作类,方便需要的用到.github地址:https://github.com/Jimmey-Jiang/Common.Utility 1.连接FTP服务器 /// <s ...
- PhoneWindow,ViewRoot,Activity之间的大致关系
http://www.nowamagic.net/academy/detail/50160216 在android里,我们都知道activity.但是一个activity跟一个Window是一个什么关 ...
- Oracle学习笔记之存储过程
...