POJ 3189 Treats for the cows (two DP methods resolved)

Source: Internet
Author: User

Treats for the Cows
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4264 Accepted: 2155

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.


Method One

According to test instructions Direct Dp,dp[i][j][2],i is the first day, J is there J, 0 is taken from the front, 1 is taken from the back. Then it can be launched according to Dp[i-1][j][0],dp[i-1][j][1] dp[i][j][1], according to Dp[i-1][j-1][0],dp[i-1][j-1][1] launch dp[i][j][0].

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int Maxn=2000+100;int a[maxn];int dp[maxn][maxn][2];int main () {    int n;    while (~SCANF ("%d", &n))    {for        (int i=1;i<=n;i++)        scanf ("%d", &a[i]);        Memset (Dp,0,sizeof (DP));        int Ans=max (a[1],a[n]);        DP[1][1][0]=A[1];        Dp[1][0][1]=a[n];        for (int i=2;i<=n;i++)        {for            (int j=i;j>=0;j--)            {                if (dp[i-1][j-1][0]| | DP[I-1][J-1][1] &&j>0)                Dp[i][j][0]=max (dp[i-1][j-1][1],dp[i-1][j-1][0]) +a[j]*i;                if (dp[i-1][j][0]| | DP[I-1][J][1])                Dp[i][j][1]=max (dp[i-1][j][0],dp[i-1][j][1]) +a[n-i+j+1]*i;                Ans=max (Ans,dp[i][j][0]);                Ans=max (ans,dp[i][j][1]);            }        }        printf ("%d\n", ans);    }    return 0;}

Method two (interval DP)

The dp[i][j],i represents the interval starting point, J represents the interval end point, and LG represents the interval length.

Code:

#include <cstdio> #include <cstring> #include <algorithm> #include <iostream>using namespace std;const int Maxn=2000+200;int a[maxn];int dp[maxn][maxn];int main () {    int n;    while (~SCANF ("%d", &n))    {for        (int i=1;i<=n;i++)        {            scanf ("%d", &a[i]);        }        for (int i=1;i<=n;i++)        dp[i][i]=a[i]*n;//interval length is 0, only one number, is the last number for        (int lg=1;lg<n;lg++)        {            for (int i=1;i<=n;i++)            {                int j=i+lg;                Dp[i][j]=max (dp[i+1][j]+a[i]* (N-LG), dp[i][j-1]+a[j]* (N-LG));                Here is a push forward from the beginning of the last team, I~j can be launched from I+1~j and I~j-1            }        }        printf ("%d\n", Dp[1][n]);}    }


POJ 3189 Treats for the cows (two DP methods resolved)

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.