POJ3186

题意

给n个数,排成一排,每次可以从队头或者队尾拿一个数,贡献是第几个拿的乘*这个数,问最大和

分析

逆向递推!!!!

这种问题很显然啊,明显当前的选取与没有选取到的有关系,所以要逆向递推

定义:dp[i][j]:表示从i到j(即起点为i终点为j),直接枚举区间长度和起点,注意贡献是递减的

转移:相邻位置

#include <cstdio>
#include <iostream>
#include <queue>
#include <algorithm>
#include <cstring>
#include <set>
using namespace std;
#define ll long long

const int maxn = 2000 + 10;


int a[maxn];
int dp[maxn][maxn];
int n;

int main()
{
    scanf("%d",&n);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d",&a[i]);
        dp[i][i]=a[i]*n;
    }
    for(int i = 1; i < n; i++)
    {
        for(int j = 1;j+i<=n; j++)
        {
            dp[j][j+i] = max(dp[j+1][j+i]+a[j]*(n-i) , dp[j][j+i-1] + a[j+i]*(n-i));
        }
    }
    printf("%d
", dp[1][n]);
    return 0;
}
View Code
要么优秀要么生锈
原文地址:https://www.cnblogs.com/Superwalker/p/7923062.html