zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    JavaScript 命名空间
    雅虎网站页面性能优化的34条黄金守则
    利用模板引擎配合ajax进行数据的导入
    canvas 实现小人的行走和上下左右的变换
    canvas 做一个小鸟运动的小游戏 (第二步) 使小鸟飞起来
    canvas 做一个小鸟运动的小游戏 (第一步)
    canvas 画一个小时钟
    更改博客的通知
    pat advanced 1139. First Contact (30)
    10分钟上手python pandas
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11664502.html
Copyright © 2011-2022 走看看