POJ3186 (Interval DP)

topic link

The meaning of the question: There is a two-way queue from which you can take numbers. The k-th number taken out is multiplied by k to be the value of this number, and then the maximum value of all numbers is calculated.

Solution:
easy to get d p [ i ] [ j ] = m a x ( d p [ i + 1 ] [ j ] + v [ i ] ( n j + i ) , d p [ i ] [ j 1 ] + v [ j ] ( n j + i ) )
Then when solving the current left boundary i, you need to refer to i+1, and the right boundary j needs to refer to j-1, so it is convenient for i to reverse order, and j to traverse in order.
(Indicates that I have written dfs for a long time...)

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,num[2005],ans;
int dp[2001][2001];
int main()
{
    while(~scanf("%d",&n))
    {
        memset(num,0,sizeof num);
        memset(dp,0,sizeof dp);
        for (int i = 1; i <= n; i++)
        {
            scanf("%d",&num[i]);
            dp[i][i] = num[i];
        }
        ans = 0;
        for (int i = n - 1; i >= 1; i--)
        {
            for (int j = i; j <= n; j++)
            {
                dp[i][j] = max(dp[i + 1][j] + num[i]*(n - j + i),dp[i][j - 1] + num[j]*(n - j + i));
            }
        }
        printf("%d\n",dp[1][n]);
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324487137&siteId=291194637