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 first treat is sold on day 1 and has age a=1. Each subsequent day increases the age by 1.
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.
The first treat is sold on day 1 and has age a=1. Each subsequent day increases the age by 1.
Line 1: A single integer, N
Lines 2..N+1: Line i+1 contains the value of treat v(i)
Lines 2..N+1: Line i+1 contains the value of treat v(i)
Line 1: The maximum revenue FJ can achieve by selling the treats
5 1 3 1 5 2
43
Explanation of the sample:
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.
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.
题目大意就是给一个序列,每次从序列头或序列尾取出一个数字乘以当前的权重,求能取到的最大值是多少
直接记忆化搜索,n平方的复杂度,太舒服了
AC代码
#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
int num[2005];
int dp[2005][2005]={0};
int dfs(int sta,int End,int now)
{
if(dp[sta][End]&&sta!=End)return dp[sta][End];
if(dp[sta][End])return dp[sta][End]*now;//取出来的不是区间而是单个数字的时候才乘以权重
int ans=max(dfs(sta,End-1,now+1)+dp[End][End]*now,dfs(sta+1,End,now+1)+dp[sta][sta]*now);
return dp[sta][End]=ans;
}
int main()
{
int N;
while(~scanf("%d",&N))
{
for(int i=1;i<=N;i++)scanf("%d",&dp[i][i]);
printf("%d\n",dfs(1,N,1));
}
return 0;
}