POJ 3186 Treats for the cows (interval DP)

Source: Internet
Author: User

Treats for the Cows

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4375 Accepted: 2226

Description FJ have purchased N (1 <= n <=) yummy treats for the cows who get money for giving vast amounts of M Ilk. 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 that's 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

5
1
3
1
5
2

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 asks from both sides to take, the weight value is a[i]*cnt, this is very bad processing.

Assuming from the inside to take, take the innermost that the weight value is A[i]*n, the penultimate is a[j]* (n-1),

DP[I][J] means that the maximum value of the [I,j] interval is taken, then the dp[i][i] is easy to get dp[i][i+1];

Dp[i][j]=max (DP[I+1][J]+A[I]*CNT,DP[I][J-1]+A[J]*CNT); (CNT is the number of times to take numbers)

The final answer is Dp[1][n];


#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <queue>
using namespace std;
#define LL Long Long
#define MEM (a,t) memset (A,t,sizeof (a))
#define N 2005
const int m=100005;
const int inf=0x1f1f1f1f;
int a[n],dp[n][n];
int main ()
{
    int i,j,k,n;
    while (~SCANF ("%d", &n))
    {
        mem (dp,0);
        for (i=1;i<=n;i++)
        {
            scanf ("%d", &a[i]);     The dp[i][i]=a[i]*n of the interval from the inside to the outside thrust
            ;    Last fetch is a[i]
        } for
        (k=1;k<n;k++)//Interval length is k+1 when
        {for
            (i=1;i+k<=n;i++)      //enumeration interval length is K + The beginning of each interval of 1
            {
                j=i+k;               The end of the interval is j=i+k
                dp[i][j]=max (dp[i+1][j]+a[i]* (n-k), dp[i][j-1]+a[j]* (n-k));
            }            The optimal solution for this interval is to take the maximum value of the left or right of the two
        }
        printf ("%d\n", Dp[1][n]);
    }
    return 0;
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.