poj 3186 Treats for the Cows(dp)
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.
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int a[];
int dp[][];
int main(){
ios::sync_with_stdio(false);
int n;
while(cin>>n){
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++){
cin>>a[i];
// sum[i]+=a[i];
}
dp[][]=a[n];
dp[][]=a[];
for(int i=;i<=n;i++){
for(int j=n;j>=n-i+;j--)
dp[i][]+=(a[j]*(n-j+));
for(int j=;j<=i;j++){
if(dp[i-][j-]+a[j]*i<dp[i-][j]+a[n-(i--j)]*i){
dp[i][j]=dp[i-][j]+a[n-(i--j)]*i;
}else{
dp[i][j]=dp[i-][j-]+a[j]*i;
}
}
}
int ans=-inf;
for(int i=;i<=n;i++)
ans=max(dp[n][i],ans);
cout<<ans<<endl;
}
return ;
}
poj 3186 Treats for the Cows(dp)的更多相关文章
- 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 ...
- 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
DP[i][j]表示现在开头是i物品,结尾是j物品的最大值,最后扫一遍dp[1][1]-dp[n][n]就可得到答案了 稍微想一下,就可以, #include<iostream> #inc ...
- POJ 3186 Treats for the Cows ——(DP)
第一眼感觉是贪心,,果断WA.然后又设计了一个两个方向的dp方法,虽然觉得有点不对,但是过了样例,交了一发,还是WA,不知道为什么不对= =,感觉是dp的挺有道理的,,代码如下(WA的): #incl ...
- POJ 3186 Treats for the Cows
简单DP dp[i][j]表示的是i到j这段区间获得的a[i]*(j-i)+... ...+a[j-1]*(n-1)+a[j]*n最大值 那么[i,j]这个区间的最大值肯定是由[i+1,j]与[i,j ...
- POJ3186 Treats for the Cows —— DP
题目链接:http://poj.org/problem?id=3186 Treats for the Cows Time Limit: 1000MS Memory Limit: 65536K To ...
- 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 ) ) 边 ...
- poj3186 Treats for the Cows
http://poj.org/problem?id=3186 Treats for the Cows Time Limit: 1000MS Memory Limit: 65536K Total S ...
- (区间dp + 记忆化搜索)Treats for the Cows (POJ 3186)
http://poj.org/problem?id=3186 Description FJ has purchased N (1 <= N <= 2000) yummy treats ...
随机推荐
- python基础之数据类型和数值类型
python3的六大数据类型: 1.tuple元组 2.number数字 3.string字符串 4.set集合 5.list列表 6.dictionary字典 其中不可变数据3个:tuple.num ...
- C#设计模式之6:抽象工厂模式
前面分析了简单工厂模式和工厂方法模式,接着来看一下抽象工厂模式,他与工厂方法模式有一些相似的地方,也有不同的地方. 先来看一个不用工厂方法模式实现的订购披萨的代码: 对象依赖的问题:当你直接实例化一个 ...
- 剑指Offer(9)
题目: 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 解法: 要考虑到底数为0,指数为负数的情况,这道题主要考的是对边界值的思考. p ...
- Flutter之Container详解
1 基本内容1.1 继续关系Object > Diagnosticable > DiagnosticableTree > Widget > StatelessWidget &g ...
- scrapy全站爬取拉勾网及CrawSpider介绍
一.指定模板创建爬虫文件 命令 创建成功后的模板,把http改为https 二.CrawSpider源码介绍 1.官网介绍: 这是用于抓取常规网站的最常用的蜘蛛,因为它通过定义一组规则为跟踪链接提供了 ...
- spec文件中的 %pre %post %preun %postun
转载http://meinit.nl/rpm-spec-prepostpreunpostun-argument-values RPM has 4 parts where (shell) scripts ...
- 莫烦sklearn学习自修第八天【过拟合问题】
1. 什么是过拟合问题 所谓过拟合问题指的是使用训练样本进行训练时100%正确分类或规划,当使用测试样本时则不能正确分类和规划 2. 代码实战(模拟过拟合问题) from __future__ imp ...
- PHP金额工具类之将阿利伯数字转换为大写中文数字
1.将阿拉伯数字转换为中文大写数字 <?php namespace core\components; class PriceHelper extends \yii\base\Component{ ...
- Http请求笔记
1 HTTP请求报文组成: 请求行:请求方法 url 协议版本 请求头:报文头-属性名:属性值 Accept属性告诉服务端-客户端接受什么类型的响应,可为一个或多个mime类型值 Cookie:服务端 ...
- easyui combobox 在datagrid中动态加载数据
场景:datagrid 中用编辑框修改数据,有一个列使用的combobox 在可编辑的时候需要动态绑定数据,这个数据是在根据其他条件可变的 思路:在每次开启编辑框的时候动态绑定数据, datagri ...