简体   繁体   中英

What is the best approach to dp for question TRT (treat for cows) of spoj?

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 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. Given the values v(i) of each of the treats lined up in order of the index i in their box, what is the greatest value FJ can receive for them if he orders their sale optimally?

The first treat is sold on day 1 and has 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 The maximum revenue FJ can achieve by selling the treats

Example Input: 5

1 3 1 5 2

Output: 43

What is the basic approach to dp in this problem and how do i process age in dp matrix...? Only approach hitting on my mind is the recursive one... i'm new to DP and i've done some basic dp problems but this is outta my mind... this is what i tried so far

int give(int a[],int x,int y,int age)
{

if(x==y) return age*a[x];

return max(age*a[x]+give(a,x+1,y,age+1),age*a[y]+give(a,x,y-1,age+1));

}

x=starting index ,initialized from 0, y=last index , n-1, age=1 at first call

The first thing to observe is that the current state doesn't depend on the history of where we were taking the treats from. The only thing we care about is how many times we took the treat from the left and how many times we took it from the right.

Therefore the state can be encoded with just 2 numbers: left offset and right offset.

Let f(i,j) be the amount of money we can still get if we until the current move sold. We know that there we have already taken i+j objects from the line, so the age is also i+j .

Therefore we just have to check which position we want to take from, and pick the better one. Well if we pick a treat from the left our amount gained will be

cost * time + f( i + 1 , j ) = cost * (i + j) + f( i + 1 , j ) .

The amount if we take from the right has a very similar formula

cost_right * (i + j) + f( i , j + 1 ) .

Therefore we can knowing f(i+1,j) and f(i,j+1) compute f(i,j) .

Well this can be done using dynamic programming by storing a 2D array of size n*n that stores either -1 if f(i,j) is unknown, or the value of f(x,y) if it is known. Then you can simply update the recursive method described above to actually store the results and return the solution if it is already known. If we take a look at your code example we can modify it to run in time.

    int give(int a[],int x,int y,int age)
    {
        if(dp[x][y]!=-1) return dp[x][y];
        if(x==y) return age*a[x];

        int answer=max(age*a[x]+give(a,x+1,y,age+1),age*a[y]+give(a,x,y-1,age+1));

        dp[x][y]=answer;
        return answer;
    }

This is just a snippet, as you'll need to fix the boundary conditions and make the actual global dp array, but I hope it is enough to get started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM