zoukankan      html  css  js  c++  java
  • 款待奶牛(treat)

    款待奶牛(treat)

    题目描述

    FJ有n(1≤n≤2000)个美味的食物,他想卖掉它们来赚钱给奶牛。这些食物放在一些箱子里,它们有些有趣的特性:
    (1)这些食物被编号为1~n,每一天FJ可以从这排箱子的头部或者尾部取出食物去卖;
    (2)这些食物放得越久,年龄越大,价值越大,食物i有一个初始的价值V(i);
    (3)放了a天后,年龄为a,食物最终的价值为V(i)×a。
    给定每一个食物的初始价值v(i),请求出FJ卖掉它们后可以获得的最大价值,第一天出售的食物的年龄为1,此后每增加一天食物的年龄就增加1。

    输入

    第1行:一个整数n;
    第i+l行:每行为食物i的初始价值V(i)。

    输出

    1行:FJ最终可以获得的最大价值。

    样例输入

    5
    1
    3
    1
    5
    2
    

    样例输出

    43
    

    提示


    样例说明:FJ出售这些食物(初始价值1,3,1,5,2)的顺序为:第一天卖掉第1个,第二天卖掉第5个,第三天卖掉第2个,第四天卖掉第3个,第5天卖掉第4个,获得最大的价值 1×1+2×2+3×3+4×1+5×5=43。

    分析:又是区间dp,还是很开心的,dp[j][i]表示以j为起点长度为i的最优解(注意:i表示最后i天的决策);

       一大神用记忆化搜索过了,简直666

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #include <ext/rope>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define vi vector<int>
    #define pii pair<int,int>
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    const int maxn=2e3+10;
    const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
    using namespace std;
    using namespace __gnu_cxx;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    int n,m,dp[maxn][maxn],a[maxn];
    int main()
    {
        int i,j,k,t;
        scanf("%d",&n);
        rep(i,1,n)scanf("%d",&a[i]);
        rep(i,1,n)
        {
            for(j=1;j+i<=n+1;j++)
            {
                dp[j][i]=max(dp[j+1][i-1]+(n-i+1)*a[j],dp[j][i-1]+(n-i+1)*a[j+i-1]);
            }
        }
        printf("%d
    ",dp[1][n]);
        //system ("pause");
        return 0;
    }
  • 相关阅读:
    链接服务器 "(null)" 的 OLE DB 访问接口 "Microsoft.Ace.OleDb.12.0" 报错。提供程序未给出有关错误的任何信息。
    iis应用程序池 内存溢出错误 System.OutOfMemoryException
    高性能JavaScript
    HTML5介绍
    Ubuntu 16.04安装Matlab 2016b教程
    C Standard Library: 9 Signals: <signal.h>
    C Standard Library:4 Mathematical Functions: <math.h>
    C Standard Library: File Positioning Functions
    C Standard Library: Formatted Input
    C Standard Library: 2 Character Class Tests: <ctype.h>
  • 原文地址:https://www.cnblogs.com/dyzll/p/5679717.html
Copyright © 2011-2022 走看看