[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
递推顺序不好求,所以选择记忆化搜索。
——代码
#include <cstdio>
#include <iostream> const int MAXN = ;
int n;
int a[MAXN], f[MAXN][MAXN]; inline int read()
{
int x = , f = ;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -;
for(; isdigit(ch); ch = getchar()) x = (x << ) + (x << ) + ch - '';
return x * f;
} inline int max(int x, int y)
{
return x > y ? x : y;
} inline int dp(int x, int y)
{
if(f[x][y]) return f[x][y];
if(x == y) return f[x][y] = a[x] * n;
else return f[x][y] = max(dp(x + , y) + a[x] * (n - y + x), dp(x, y - ) + a[y] * (n - y + x));
} int main()
{
int i;
n = read();
for(i = ; i <= n; i++) a[i] = read();
printf("%d\n", dp(, n));
return ;
}
[luoguP2858] [USACO06FEB]奶牛零食Treats for the Cows(DP)的更多相关文章
- 「USACO06FEB」「LuoguP2858」奶牛零食Treats for the Cows(区间dp
题目描述 FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving va ...
- P2858 [USACO06FEB]奶牛零食Treats for the Cows
P2858 [USACO06FEB]奶牛零食Treats for the Cows区间dp,级像矩阵取数, f[i][i+l]=max(f[i+1][i+l]+a[i]*(m-l),f[i][i+l- ...
- bzoj1652 / P2858 [USACO06FEB]奶牛零食Treats for the Cows
P2858 [USACO06FEB]奶牛零食Treats for the Cows 区间dp 设$f[l][r]$为取区间$[l,r]$的最优解,蓝后倒着推 $f[l][r]=max(f[l+1][r ...
- AC日记——[USACO06FEB]奶牛零食Treats for the Cows 洛谷 P2858
[USACO06FEB]奶牛零食Treats for the Cows 思路: 区间DP: 代码: #include <bits/stdc++.h> using namespace std ...
- 洛谷 P2858 [USACO06FEB]奶牛零食Treats for the Cows 题解
P2858 [USACO06FEB]奶牛零食Treats for the Cows 题目描述 FJ has purchased N (1 <= N <= 2000) yummy treat ...
- 洛谷 P2858 [USACO06FEB]奶牛零食Treats for the Cows
题目描述 FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving va ...
- 区间DP【p2858】[USACO06FEB]奶牛零食Treats for the Cows
Description 约翰经常给产奶量高的奶牛发特殊津贴,于是很快奶牛们拥有了大笔不知该怎么花的钱.为此,约翰购置了N(1≤N≤2000)份美味的零食来卖给奶牛们.每天约翰售出一份零食.当然约翰希望 ...
- Luogu P2858 [USACO06FEB]奶牛零食Treats for the Cows 【区间dp】By cellur925
题目传送门 做完A Game以后找道区间dp练练手...结果这题没写出来(哭). 和A Game一样的性质,从两边取,但是竟然还有天数,鉴于之前做dp经常在状态中少保存一些东西,所以这次精心设计了状态 ...
- 洛谷P2858 【[USACO06FEB]奶牛零食Treats for the Cows】
我们可以记录头和尾再加一个卖了的零食数目,如果头超过尾就return 0. 如果遇到需要重复使用的数,(也就是不为零的d数组)就直接return d[tuo][wei]. 如果没有,就取卖头一个与最后 ...
随机推荐
- 平方分割poj2104K-th Number
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 59798 Accepted: 20879 Ca ...
- ACM_Hailstone HOTPO
Hailstone HOTPO Time Limit: 2000/1000ms (Java/Others) Problem Description: The hailstone sequence is ...
- nginx connect failed (110- Connection timed out) 问题排查
首先排查 ping 下 nginx 与代理服务是否ping 的通,带端口的,telnet 下端口号是否是通的,本次遇到问题为 telnet 发现有台服务器不通,原因是端口未开放
- Java 8 (1) 行为参数化
行为参数化就是可以帮助你处理频繁变更需求的一种软件开发模式.它意味着拿出一个代码块,把它准备好却不去执行它.这个代码块以后可以被你程序的其他部分调用,这意味着你可以推迟这块代码的执行.例如:你可以将代 ...
- Myeclipse2014安装&破解激活
市场上很多JavaWeb的IDE比如Idea(听说用好开发效率会很高),eclipse(插件丰富还免费),但是对于初学者还是为了提高学习的效率(Myeclipse创建web项目的时候可以自动生成一些配 ...
- pom.xml详情
这里借鉴一下csdn中的一个系列的博客: 第一篇:POM文件详解 第二篇:maven中的依赖作用范围 第三篇:maven中的可选依赖和依赖排除 第四篇:maven中的dependencies和depe ...
- DecorView 的创建
在Activity 的启动过程中,调用ActivityThread 的handleResumeActivity 方法时,先得到一个与Activity 关联的PhoneWindow 对象,然后通过Pho ...
- php redis 操作大全
类和方法 用法 Redis类 类RedisException 预定义的常量 Redis类 说明:创建一个Redis客户端 例 $redis = new Redis(); 类RedisException ...
- poi获取word批注
package test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcept ...
- 2星|《约见投资人》:A股上市公司软文集
约见资本人:58家上市公司创始人亲述创业之路 全书写了58个A股上市公司的故事,基本是宣传上市公司老总的软文.基本的套路是创始人历尽苦难创立了公司,取得了好业绩.最希望看的分析与数据几乎没有.看了一小 ...