[Luogu] P2858 [USACO06FEB]奶牛零食Treats for the Cows

题目描述

约翰经常给产奶量高的奶牛发特殊津贴,于是很快奶牛们拥有了大笔不知该怎么花的钱.为此,约翰购置了N(1≤N≤2000)份美味的零食来卖给奶牛们.每天约翰售出一份零食.当然约翰希望这些零食全部售出后能得到最大的收益.这些零食有以下这些有趣的特性:

•零食按照 1...N 编号,它们被排成一列放在一个很长的盒子里.盒子的两端都有开口,约翰每天可以从盒子的任一端取出最外面的一个.

•与美酒与好吃的奶酪相似,这些零食储存得越久就越好吃.当然,这样约翰就可以把它们卖出更高的价钱.

•每份零食的初始价值不一定相同.约翰进货时,第 i 份零食的初始价值为 Vi(1≤Vi≤1000).

•第 i 份零食如果在被买进后的第 a 天出售,则它的售价是 Vi×a .

Vi?是从盒子顶端往下的第 i 份零食的初始价值.约翰告诉了你所有零食的初始价值,并希望你能帮他计算一下,在这些零食全被卖出后,他最多能得到多少钱.

题目解析

裸区间DP

写一点心得:其实区间dp所关心的只是当前区间的大小和当前在区间的那一边,对于区间里面发生了什么事情我们不在乎。

想清楚思路其实不难写,dp[i][j][0/1]表示当前选了i个了,右端点是j,在区间的哪一边(左右)。

Code

#include<iostream>
#include<cstdio>
using namespace std;

const int MAXN = 2000 + 5;

int n;
int w[MAXN];
int dp[MAXN][MAXN][2];

int _max(int x,int y) {
    return x > y ? x : y;
}

int main() {
    scanf("%d",&n);
    for(int i = 1;i <= n;i++) {
        scanf("%d",&w[i]);
    }
    for(int i = 1;i <= n;i++) {//选了的个数
        for(int j = 0;j <= i;j++) {//端点
            dp[i][j][0] = max(dp[i-1][j-1][0],dp[i-1][j-1][1]) + w[j]*i;
            dp[i][j][1] = max(dp[i-1][j][0],dp[i-1][j][1]) + w[n+1+j-i] * i;
        }
    }
    int ans = 0;
    for(int i = 0;i <= n;i++) {
        ans = _max(ans,max(dp[n][i][0],dp[n][i][1]));
    }
    printf("%d",ans);
}

原文地址:https://www.cnblogs.com/floatiy/p/9503991.html

时间: 2024-10-17 23:18:23

[Luogu] P2858 [USACO06FEB]奶牛零食Treats for the Cows的相关文章

P2858 [USACO06FEB]奶牛零食Treats for the Cows

题目描述 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 re

洛谷 P2858 [USACO06FEB]奶牛零食Treats for the Cows

题目描述 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 re

AC日记——[USACO06FEB]奶牛零食Treats for the Cows 洛谷 P2858

[USACO06FEB]奶牛零食Treats for the Cows 思路: 区间DP: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 2005 #define ll long long ll n,ai[maxn],dp[maxn][maxn],sum[maxn]; inline void in(ll &now) { char Cget=getchar();now=0; while(Cget>'9'

【洛谷P2858&#183;动态规划】[USACO06FEB]奶牛零食Treats for the Cows

题面 题目描述 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

[luoguP2858] [USACO06FEB]奶牛零食Treats for the Cows(DP)

传送门 f[i][j][k] 表示 左右两段取到 i .... j 时,取 k 次的最优解 可以优化 k 其实等于 n - j + i 则 f[i][j] = max(f[i + 1][j] + a[i] * (n - j + i), f[i][j - 1] + a[j] * (n - j + i)) 边界 f[i][i] = a[i] * n 递推顺序不好求,所以选择记忆化搜索. ——代码 1 #include <cstdio> 2 #include <iostream> 3 4

bzoj1652:treats for the cows

1652: [Usaco2006 Feb]Treats for the Cows Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 271  Solved: 209[Submit][Status][Discuss] Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk.

BZOJ 1652: [Usaco2006 Feb]Treats for the Cows( dp )

dp( L , R ) = max( dp( L + 1 , R ) + V_L * ( n - R + L ) , dp( L , R - 1 ) + V_R * ( n - R + L ) ) 边界 : dp( i , i ) = V[ i ] * n -------------------------------------------------------------------------------------------- #include<cstdio> #include&l

BZOJ 1652: [Usaco2006 Feb]Treats for the Cows

题目 1652: [Usaco2006 Feb]Treats for the Cows Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 234  Solved: 185[Submit][Status] Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ se

[BZOJ 1652][USACO 06FEB]Treats for the Cows 题解(区间DP)

[BZOJ 1652][USACO 06FEB]Treats for the Cows Description 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