【动态规划】POJ-2385
一、题目
Description
It is a little known fact that cows love apples. Farmer John has two apple trees (which are conveniently numbered 1 and 2) in his field, each full of apples. Bessie cannot reach the apples when they are on the tree, so she must wait for them to fall. However, she must catch them in the air since the apples bruise when they hit the ground (and no one wants to eat bruised apples). Bessie is a quick eater, so an apple she does catch is eaten in just a few seconds.
Each minute, one of the two apple trees drops an apple. Bessie, having much practice, can catch an apple if she is standing under a tree from which one falls. While Bessie can walk between the two trees quickly (in much less than a minute), she can stand under only one tree at any time. Moreover, cows do not get a lot of exercise, so she is not willing to walk back and forth between the trees endlessly (and thus misses some apples).
Apples fall (one each minute) for T (1 <= T <= 1,000) minutes. Bessie is willing to walk back and forth at most W (1 <= W <= 30) times. Given which tree will drop an apple each minute, determine the maximum number of apples which Bessie can catch. Bessie starts at tree 1.
Input
- Line 1: Two space separated integers: T and W
- Lines 2..T+1: 1 or 2: the tree that will drop an apple each minute.
Output
- Line 1: The maximum number of apples Bessie can catch without walking more than W times.
Sample Input
7 2
2
1
1
2
2
1
1
Sample Output
6
Hint
INPUT DETAILS:
Seven apples fall - one from tree 2, then two in a row from tree 1, then two in a row from tree 2, then two in a row from tree 1. Bessie is willing to walk from one tree to the other twice.
OUTPUT DETAILS:
Bessie can catch six apples by staying under tree 1 until the first two have dropped, then moving to tree 2 for the next two, then returning back to tree 1 for the final two.
二、思路&心得
- 定义dp[i][j]为在第i秒,移动j次获得的最大苹果数。在零时刻时,所有的dp项均为0。
- 当j为0时,有dp[i][j] = dp[i - 1][j]
- 当j不为0时,有dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1]),即在 i - 1 时刻时,均有两种选择,一种选择移动,一种选择不移动。
- 注意题目并不是在移动越多次能获得越多苹果。
三、代码
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAX_T = 1005;
const int MAX_W = 35;
int main() {
int dp[MAX_T][MAX_W];
int num[MAX_T];
int T, W;
scanf("%d %d", &T, &W);
for (int i = 1; i <= T; i ++) {
scanf("%d", &num[i]);
}
for (int i = 1; i <= T; i ++) {
for (int j = 0; j <= W, j <= i; j ++) {
if (j == 0) dp[i][j] = dp[i - 1][j];
else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1]);
if ((num[i] - (j & 1) == 1)) dp[i][j] ++;
}
}
int ans = dp[T][0];
for (int i = 1; i <= W; i ++) {
ans = max(ans, dp[T][i]);
}
printf("%d\n", ans);
return 0;
}
【动态规划】POJ-2385的更多相关文章
- poj 2385【动态规划】
poj 2385 Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14007 Accepte ...
- poj 2385 Apple Catching(记录结果再利用的动态规划)
传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 有两颗苹果树,在每一时刻只有其中一棵苹果树会掉苹果,而Bessie可以在很短的时 ...
- 【POJ - 2385】Apple Catching(动态规划)
Apple Catching 直接翻译了 Descriptions 有两棵APP树,编号为1,2.每一秒,这两棵APP树中的其中一棵会掉一个APP.每一秒,你可以选择在当前APP树下接APP,或者迅速 ...
- 【DP】POJ 2385
题意:又是Bessie 这头牛在折腾,这回他喜欢吃苹果,于是在两棵苹果树下等着接苹果,但苹果不能落地后再接,吃的时间不算,假设他能拿得下所有苹果,但是这头牛太懒了[POJ另一道题目说它是头勤奋的奶牛, ...
- 二分+动态规划 POJ 1973 Software Company
Software Company Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1112 Accepted: 482 D ...
- [ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
- DP:Apple Catching(POJ 2385)
牛如何吃苹果 问题大意:一个叫Bessie的牛,可以吃苹果,然后有两棵树,树上苹果每分钟会掉一个,这只牛一分钟可以在两棵树中往返吃苹果(且不吃地上的),然后折返只能是有限次W,问你这只叫Bessie的 ...
- POJ 2385 Apple Catching
比起之前一直在刷的背包题,这道题可以算是最纯粹的dp了,写下简单题解. 题意是说cows在1树和2树下来回移动取苹果,有移动次数限制,问最后能拿到的最多苹果数,含有最优子结构性质,大致的状态转移也不难 ...
- A-Apple Catching(POJ 2385)
Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8759 Accepted: 4264 De ...
- POJ 2385 DP
题意:在苹果树下,初始在第一棵树下,告诉你在第几秒的时候,那棵树下会落下苹果,告诉最多能移动的次数,然后来回移动,求能得到的最大的苹果数目. 思路:三维DP,d[第i秒][已经移动j次][当前在(1, ...
随机推荐
- PHP+MySQL实现海量数据导入导出的总结:is_numbric函数的坑
前段时间有个需求:将生产环境的部分数据转移到测试服务器进行测试.由于只需要导入特定账号的数据,我就想着将写个脚本,将数据组装成sql语句导出为sql文件,然后转移到测试服务器,导入到MySQL中.想象 ...
- Python学习 :常用模块(二)
常用模块(二) 四.os模块 os模块是与操作系统交互的一个接口,用于对操作系统进行调用 os.getcwd() # 提供当前工作目录 os.chdir() # 改变当前工作目录 os.curdir( ...
- Scala模式匹配常用
今天在工作中遇到的几个小问题,总结一下: 1.因为业务需要调用PHP的接口,获取到的返回体需要做一段逻辑处理,然而某个字段接收到的参数是io.serializable类型,字段的类型不是预期的stri ...
- WPF开发学习笔记
1.命名规范: 插件名称统一以:CI.Client.Plugins.SYS.+TableName eg:CI.Client.Plugins.SYS.EnterPrise 2.插件文件目录: 3.D ...
- Python之面向对象-反射
一.什么是反射 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问,检测和修改它本省状态或行为的一种能力(自省).这一概念的提出很快引发了计算机科学领域关于应用反射性的研究.它首先被 ...
- Andorid Studio 模块化开发相关配置
Andorid Studio 模块化开发相关配置 下面以宿主APP模块和Uer_Module模块为例: 第一步:在项目根目录gradle.properties配置文件中添加如下代码 isNeedUse ...
- 解决Unity烘焙阴影锯齿精度不足的问题
烘焙阴影锯齿问题 烘焙后阴影锯齿明显,如下图: 烘焙的光照贴图质量主要受LightmapParameters 的Blur Radius和抗锯齿级别影响, 默认最高级别如下: 如果最高级别不能达到好的 ...
- oracle的多表合并查询-工作心得
本随笔文章,由个人博客(鸟不拉屎)转移至博客园 发布时间: 2018 年 11 月 29 日 原地址:https://niaobulashi.com/archives/oracle-select-al ...
- java基础---JDK、JRE、JVM的区别和联系
当我们学习java语言时,首先需要安装到我们电脑上的就是jdk.jdk是java语言的开发环境,只有安装了jdk,我们才能使用java语言开发程序. JDK=JRE+开发工具包 JRE=JVM+核心类 ...
- 会了这十种Python优雅的写法,让你工作效率翻十倍,一人顶十人用!
我们都知道,Python 的设计哲学是「优雅」.「明确」.「简单」.这也许很多人选择 Python 的原因.但是我收到有些伙伴反馈,他写的 Python 并不优雅,甚至很臃肿,那可能是你的姿势不对 ...