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 ...
随机推荐
- EGit使用教程:第一篇 添加工程到版本控制
配置 确定身份 当每次提交的时候,Git需要跟踪这次提交,确认是哪个用户提交的.用户由 user.name 和 user.email 组成,这个信息包含在 ~/.gitconfig 文件中. ~ 代表 ...
- 内核对象 windows操作系统
问题: 什么是内核对象? 答:内核对象实际上时由内核分配的一块内存,而且只能由内核来访问.它时一个数据结构,成员包含有关于该对象的信息.一些成员对于所有对象类型都是一样的,比如对象名称.安全描述.使用 ...
- Java课堂作业01
题目:编写一个程序,此程序从命令行接收多个数字,求和之后输出结果. 设计思想:用for循环将string型转换为int型,再用sum求和,使其一直相加,到达最大长度,sum即为所求sum. 程序流程图 ...
- Linux用户和文件权限管理
本文为原创文章,转载请标明出处 目录 用户管理 系统用户文件 添加用户 useradd 设置用户密码 passwd 删除用户 userdel 用户管理 usermod 用户组管理 系统用户组文件 添加 ...
- 跨平台的 NodeJS 组件解决 .NetCore 不支持 System.Drawing图形功能的若干问题
问题 生成缩略图 生成验证码 生成二维码 给图片加水印 外部引用 Node 不解释 https://nodejs.org/en/download/ sharp 高性能缩略图 https://gi ...
- 关于安全性问题:(XSS,csrf,cors,jsonp,同源策略)
关于安全性问题:(XSS,csrf,cors,jsonp,同源策略) Ajax 是无需刷新页面就能从服务器获取数据的一种方法.它的核心对象是XHR,同源策略是ajax的一种约束,它为通信设置了相同的协 ...
- Jquery添加和全部删除
<html> <head> <meta charset="utf-8" /> <title></title> <s ...
- 将 C# 枚举反序列化为 JSON 字符串 实践
一.定义枚举 public enum SiteTypeEnum { 中转部 = 1, 网点 = 2 } 还有 BooleanEnum 和 OptTypeEnum 这两个枚举,这里暂且省略了它们的定义. ...
- iOS正确使用const,static,extern
static 修饰局部变量 让局部变量只初始化一次 局部变量在程序中只有一份内存 并不会改变局部变量的作用域,仅仅是改变了局部变量的生命周期(只到程序结束,这个局部变量才会销毁) 修饰全局变量 全局变 ...
- c语言入门
c 语言现在是一门很流行的语言,它介于汇编语言和高级语言之间,我认为 它属于中级语言,如c语言 的指针 ,位操作符,等,因为接近于汇编语言,c语言的执行代码效率很高 现在大多数的系统 如unix,和l ...