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 ...
随机推荐
- wget下载整个网站
wget下载整个网站wget下载整个网站可以使用下面的命令 wget -r -p -k -np http://hi.baidu.com/phps , -r 表示递归下载,会下载所有的链接,不过要注意的 ...
- eclipse中搜狗输入法中文状态下输出的全是英文
在eclipse中搜狗输入法变成了如图这样 在中文状态下,提示的全是中文. 查询到的解决方案: 快捷键ctrl+shift+E关闭搜狗智能英文.然而与eclipse中 Ctrl+shift+E 快捷键 ...
- IO模型分析
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...
- The Super Powers
The Super Powers Time Limit: 1000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [Subm ...
- Problem 2062 Suneast & Yayamao 二进制(多重背包的理解基础)
Problem 2062 Suneast & Yayamao Accept: 143 Submit: 313T ...
- 【框架学习与探究之宿主服务--Topshelf】
前言 此文欢迎转载,原始链接地址:http://www.cnblogs.com/DjlNet/p/7603819.html 正文 原先也偶然见过这个关键词,当时只是有个大致了解貌似和WinServic ...
- 关于AVALON总线动态地址对齐
在NIOS的使用中,我们往往要用到自定义外设,然后通过AVALON交换架构和NIOSII进行通信. AVALON总线,其实是一种交换架构的协议,在自定义外设挂在AVALON总线上时,一定要注意地址对齐 ...
- webpack——devtool里的7种SourceMap模式
我们先来看看文档对这 7 种模式的解释: 模式 解释 eval 每个module会封装到 eval 里包裹起来执行,并且会在末尾追加注释 //@ sourceURL. source-map 生成一个S ...
- Feature Scaling深入理解
Feature Scaling 可以翻译为特征归一化,或者数据归一化,比如统计学习中,我们一般都会对不同量纲的特征做归一化,深度学习中经常会谈到增加的BN层,LRN层会带来训练收敛速度的提升,等等.问 ...
- 基于java的后台截图功能的实现
Java后台截图功能的实现 背景介绍: 在近期开发的可视化二期项目中的邮件项目中,邮件中的正文中含有图片.该图片的产生是将一些html网页转为图片格式,刚开始考虑使用第三方组件库html2image和 ...