[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 const int MAXN = 2001;
 5 int n;
 6 int a[MAXN], f[MAXN][MAXN];
 7
 8 inline int read()
 9 {
10     int x = 0, f = 1;
11     char ch = getchar();
12     for(; !isdigit(ch); ch = getchar()) if(ch == ‘-‘) f = -1;
13     for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - ‘0‘;
14     return x * f;
15 }
16
17 inline int max(int x, int y)
18 {
19     return x > y ? x : y;
20 }
21
22 inline int dp(int x, int y)
23 {
24     if(f[x][y]) return f[x][y];
25     if(x == y) return f[x][y] = a[x] * n;
26     else return f[x][y] = max(dp(x + 1, y) + a[x] * (n - y + x), dp(x, y - 1) + a[y] * (n - y + x));
27 }
28
29 int main()
30 {
31     int i;
32     n = read();
33     for(i = 1; i <= n; i++) a[i] = read();
34     printf("%d\n", dp(1, n));
35     return 0;
36 }

时间: 2024-08-08 09:28:06

[luoguP2858] [USACO06FEB]奶牛零食Treats for the Cows(DP)的相关文章

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 [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

【洛谷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

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

题目描述 约翰经常给产奶量高的奶牛发特殊津贴,于是很快奶牛们拥有了大笔不知该怎么花的钱.为此,约翰购置了N(1≤N≤2000)份美味的零食来卖给奶牛们.每天约翰售出一份零食.当然约翰希望这些零食全部售出后能得到最大的收益.这些零食有以下这些有趣的特性: •零食按照 1...N 编号,它们被排成一列放在一个很长的盒子里.盒子的两端都有开口,约翰每天可以从盒子的任一端取出最外面的一个. •与美酒与好吃的奶酪相似,这些零食储存得越久就越好吃.当然,这样约翰就可以把它们卖出更高的价钱. •每份零食的初始

poj 3186 Treats for the Cows dp

#include <cstdio> #include <algorithm> using namespace std; #define maxn 2100 int dp[maxn][maxn]; int val[maxn]; int n; int main() { while(scanf("%d",&n)!=EOF) { int i,j; for(i=1;i<=n;i++) { scanf("%d",&val[i]);

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

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

题目 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