Title Link: http://poj.org/problem?id=3186
Description
FJ have purchased N (1 <= n <=) 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 is interesting for many reasons:
- The treats is numbered 1..N and stored sequentially in single file in a long box, which is open at both ends. On any day, the FJ can retrieve one treat from either end of the stash.
- Like fine wines and delicious cheeses, the treats improve with age and command greater prices.
- The treats is not uniform:some is better and has higher intrinsic value. Treat I has value V (i) (1 <= v (i) <= 1000).
- Cows pay more for treats that has aged longer:a cow would pay V (i) *a for a treat of age a.
Given The values V (i) of each of the treats lined up and order of the index I in their box, what is the greatest value FJ C A receive for them if he orders their sale optimally?
The first treat are sold on Day 1 and have 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
513152
Sample Output
43
Hint
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.The topic did not see clearly, or see the last hint only understand (fog), to the effect is , to a sequence, each time can only from the beginning or from the tail to take a number, the first I take the number of I, sum the maximum is how much. state transfer equation with Dp[i][j] indicates that the column header takes the number of I, the column end of the J number, then Dp[i][j] only with dp[i-1][j] and dp[i][j-1], then get the state transfer equation dp[i][j] = max (dp[i-1) [j] + val[i-1] * (i + j), Dp[i][j-1] + val[n-j] * (i + j)), here Val I was the subscript starting from 0.
1#include <iostream>2#include <cstring>3#include <cmath>4#include <algorithm>5 using namespacestd;6 7 intval[2005];8 intdp[2005][2005];9 Ten intMain () { OneIos::sync_with_stdio (false ); A - intN; - while(Cin >>N) { the for(inti =0; I < n; i++ ) -CIN >>Val[i]; -Memset (DP,0,sizeof(DP)); - +dp[1][0] = val[0]; - for(inti =2; I <= N; i++ ) +dp[i][0] = Dp[i-1][0] + val[i-1] *i; A atdp[0][1] = Val[n-1]; - for(inti =2; I <= N; i++ ) -dp[0][i] = dp[0][i-1] + val[n-i] *i; - - for(inti =1; I <= N; i++ ) - for(intj =1; j + i <= N; J + + ) inDP[I][J] = max (Dp[i-1][J] + val[i-1] * (i + j), Dp[i][j-1] + val[n-j] * (i +j)); - to intAns =0; + for(inti =0; I <= N; i++ ) -ans = max (ans, dp[i][n-i]); the *cout << ans <<Endl; $ }Panax Notoginseng - return 0; the}
POJ-3186 Treats for the Cows (DP)