Description

FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a given period time.

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

Line 1: A single integer, N

Lines 2..N+1: Line i+1 contains the value of treat v(i)

Output

Line 1: The maximum revenue FJ can achieve by selling the treats

Sample Input

5
1
3
1
5
2

Sample Output

43

题意:
可以从数组的左右两端拿数字,权值依次上升,求权值乘上数字的和的最大值。
思路:
dp[i][j]表示起点为i,终点为j的组数,可以得到的最大值是多少。
首先枚举长度,再计算该长度所有的dp[i][j]的值。
代码:
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-);
int num[];
int dp[][];
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&num[i]);
}
for(int i=;i<=n;i++){
dp[i][i]=num[i]*n;
}
for(int len=;len<=n;len++){
for(int i=;i<=n;i++){
int j=i+len-;
if(j<=n)dp[i][j]=max(dp[i][j],dp[i][j-]+num[j]*(n-len+));
j=i-len+;
if(j>=)dp[j][i]=max(dp[j][i],dp[j+][i]+num[j]*(n-len+));
}
}
printf("%d\n",dp[][n]);
return ;
}

POJ 3186 Treats for the Cows (动态规划)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. POJ 3186 Treats for the Cows 一个简单DP

    DP[i][j]表示现在开头是i物品,结尾是j物品的最大值,最后扫一遍dp[1][1]-dp[n][n]就可得到答案了 稍微想一下,就可以, #include<iostream> #inc ...

  4. 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 ...

  5. POJ 3186 Treats for the Cows ——(DP)

    第一眼感觉是贪心,,果断WA.然后又设计了一个两个方向的dp方法,虽然觉得有点不对,但是过了样例,交了一发,还是WA,不知道为什么不对= =,感觉是dp的挺有道理的,,代码如下(WA的): #incl ...

  6. poj3186 Treats for the Cows

    http://poj.org/problem?id=3186 Treats for the Cows Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  7. POJ3186 Treats for the Cows —— DP

    题目链接:http://poj.org/problem?id=3186 Treats for the Cows Time Limit: 1000MS   Memory Limit: 65536K To ...

  8. (区间dp + 记忆化搜索)Treats for the Cows (POJ 3186)

    http://poj.org/problem?id=3186   Description FJ has purchased N (1 <= N <= 2000) yummy treats ...

  9. 【POJ - 3186】Treats for the Cows (区间dp)

    Treats for the Cows 先搬中文 Descriptions: 给你n个数字v(1),v(2),...,v(n-1),v(n),每次你可以取出最左端的数字或者取出最右端的数字,一共取n次 ...

随机推荐

  1. VMware+centos7克隆多个虚拟机

    最近在学习Hadoop,需要搭建分布式集群,自己在电脑上安装的VMware+centos7.前期我已经安装好了一个虚拟机,并把apache,jdk等的环境都配置好了,现在需要搭建集群了,所以需要克隆三 ...

  2. 在coding或者github建立个人站点域名绑定

    coding:静态的在域名服务器与是填写pages.coding.me,不是填写{你的名字}.coding.me A型的ip是ping 该域名ip github:上传大项目可能报错(报错名字忘记了), ...

  3. selenium-获取元素属性(六)

    获取元素属性很简单,使用 get_attribute 方法即可 如下图 获取具体的属性直接将该属性名当作参数传入即可 若是获取值,则获取的实则是该元素的 value,需要将 value 当参数传入 i ...

  4. c/c++ 网络编程 UDP 改变IP地址

    网络编程 UDP 改变IP地址 在程序里动态改变主机的IP地址 1,改变ipv4的地址 #include <stdio.h> #include <string.h> #incl ...

  5. 我认知的javascript之函数调用

    今天刚好周六没事,又由于工作的原因导致早上醒来就睡不着,无聊之下,就想到了 js 的function调用问题.当然,网上也是对javascript的一些事情说得很透了,但我觉得还是有必要把自己的想法说 ...

  6. python进阶之正则表达式

    概念: 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑. 目的? 给定一个正则表 ...

  7. django 视图模式

    一 视图 FBV --- function based view(基于函数视图) CBV --- class based view(基于类的视图函数) 二 请求方式 get post put/patc ...

  8. WINFORM因为 线程崩溃

    1.如果WinForm程序中使用了线程,而线程调用的代码中会抛出异常,该异常没有任何Try块处理.就会导致程序从主线程中跳出,导致WinForm程序突然崩溃性关闭.2.程序读写某些外部资源,比如数据库 ...

  9. kubernetes 集群安装etcd集群,带证书

    install etcd 准备证书 https://www.kubernetes.org.cn/3096.html 在master1需要安装CFSSL工具,这将会用来建立 TLS certificat ...

  10. 使用time+dd测试硬盘读写速度

    命令:time dd if=/dev/zero bs=1M count=2048 of=direct_2G   此命令为在当前目录下新建一个2G的文件 Demo如下: 写速度: time dd if= ...