您的位置:首页 > 其它

POJ - 3186 Treats for the Cows

2017-05-06 09:22 260 查看
题目:

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

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

题意及思路:

照着翻译都没懂这题到底是要干嘛。。感觉6级过不了了。。

后来看别人的博客才懂,大概意思就是这些数字要么只能拿首端,要么只能拿尾端,拿的价值是 v[i]*a,拿第一个时a为1,看到这里基本状态转移就出来了:

dp[i][j] = max(dp[i-1][j] + data[i-1]*a,dp[i][j+1] + data[j+1]*a);都说是区间dp,然而还没理解区间dp到底是啥。

这题的枚举顺序是很明显的,i正序,j逆序,别忘记在i==j时还有最后一个的价值。

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;
#define LL long long
const int maxn = 2005;
int dp[maxn][maxn];
int data[maxn];

int main()
{
int n;
scanf("%d",&n);
for(int i = 1; i <= n ; i++)
scanf("%d",&data[i]);
dp[1]
= 0;
dp[1][n-1] = data
;
dp[2]
= data[1];

int i,j;
int ans = 0;
for(i = 1; i <= n; i++)
{
for(j = n; j >= i; j--)
{
int a = n-(j-i+1);
dp[i][j] = max(dp[i-1][j]+a*data[i-1], dp[i][j+1] + a*data[j+1]);
}
ans = max(ans, dp[i][i] + data[i]*n);
}
printf("%d\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: