POJ-3186-Treats for the Cows(记忆化搜索)

链接:

https://vjudge.net/problem/POJ-3186

题意:

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.

思路:

Dp[i, j]表示对于l-r最多能卖多少钱, 对于当前可以卖l,或r, 记录age往下Dfs.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const LL MOD = 20090717;
const int MAXN = 2e3+10;

LL Dp[MAXN][MAXN];
int a[MAXN];
int n;

LL Dfs(int l, int r, int step)
{
    if (Dp[l][r] != -1)
        return Dp[l][r];
    if (l == r)
    {
        Dp[l][r] = a[l]*step;
        return Dp[l][r];
    }
    LL val = 0;
    val = max(val, a[l]*step+Dfs(l+1, r, step+1));
    val = max(val, a[r]*step+Dfs(l, r-1, step+1));
    Dp[l][r] = val;
    return Dp[l][r];
}

int main()
{
    scanf("%d", &n);
    for (int i = 1;i <= n;i++)
        scanf("%d", &a[i]);
    memset(Dp, -1, sizeof(Dp));
    printf("%lld
", Dfs(1, n, 1));

    return 0;
}
原文地址:https://www.cnblogs.com/YDDDD/p/11664502.html