Treats for the Cows
| Time Limit: 1000MS |
|
Memory Limit: 65536K |
| Total Submissions: 4234 |
|
Accepted: 2132 |
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.
Source
Usaco 2006 February Gold & Silver
Interval DP, consider [I, j] this paragraph, assuming that the other has not been taken out, then [I, j] must be [i + 1, j] or [I, j-1] pushed
DP[I][J] = max (dp[i + 1][j] + v[i] * (N-(j-i)), dp[i][j-1] + v[j] * (N-(j-i)))
#include <map> #include <set> #include <list> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring># Include <iostream> #include <algorithm>using namespace std;const int N = 2010;int Dp[n][n];int val[n];int Main () {int n;while (~scanf ("%d", &n)) {memset (DP, 0, sizeof (DP)), for (int i = 1; I <= n; ++i) {scanf ("%d", &val[i]);d P[i][i] = Val[i];} for (int i = n; i >= 1; i.) {for (int j = i; j <= N; ++j) {Dp[i][j] = max (dp[i + 1][j] + val[i] * (N-(j-i + 1) + 1), Dp[i][j-1] + val[j] * (N-(j-i + 1) + 1));}} printf ("%d\n", Dp[1][n]);} return 0;}
Poj3186--treats for the Cows