poj2385 - Apple Catching【动态规划】
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.
Source
思路:首先,我们定义了一个num数组,其中num[0][i]表示第i分钟时第一颗树掉苹果,num[1][i]表示第i分钟时第二颗树掉苹果。
我们可以推导
1. 当j==0时,也就是说没有移动一次,dp[i][j]=dp[i-1][j]+num[0][i];
2. 当j!=0时, dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+num[j%2==1][i];其中num数组是判断他要去的那棵树是否有苹果掉下来。
大概思路就是这样,具体细节看代码。
#include<cstdio>
#include<cstring>
#include <iostream>
using namespace std;
const int maxn=1005;
int num[2][maxn],dp[maxn][35];
int main()
{
int t,w;
scanf("%d%d",&t,&w);
memset(num,0,sizeof(num));
for(int i=1;i<=t;++i)
{
int x;
scanf("%d",&x);
if(x==1) //第一颗树掉苹果
num[0][i]=1;
else //第二颗树掉苹果
num[1][i]=1;
}
memset(dp,0,sizeof(dp));
dp[1][0]=num[0][1],dp[1][1]=num[1][1]; //初始化,临界条件
for(int i=2;i<=t;++i)
{
for(int j=0;j<=w && j<=i;++j)
{
if(j==0)
dp[i][j]=dp[i-1][j]+num[0][i];
else
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+num[j%2==1][i];
} //转移与不转移比较,选择较大值
}
printf("%d\n",dp[t][w]);
return 0;
}
poj2385 - Apple Catching【动态规划】的更多相关文章
- POJ2385——Apple Catching
$Apple~Catching$ Time Limit: 1000MS Memory Limit: 6553 ...
- poj2385 Apple Catching (线性dp)
题目传送门 Apple Catching Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 154 ...
- poj2385 Apple Catching(dp状态转移方程推导)
https://vjudge.net/problem/POJ-2385 猛刷简单dp的第一天的第一题. 状态:dp[i][j]表示第i秒移动j次所得的最大苹果数.关键要想到移动j次,根据j的奇偶判断人 ...
- poj2385 Apple Catching
思路: 简单dp. 实现: #include <iostream> #include <cstdio> #include <cstring> using names ...
- 【POJ - 2385】Apple Catching(动态规划)
Apple Catching 直接翻译了 Descriptions 有两棵APP树,编号为1,2.每一秒,这两棵APP树中的其中一棵会掉一个APP.每一秒,你可以选择在当前APP树下接APP,或者迅速 ...
- Apple Catching(POJ 2385)
Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9978 Accepted: 4839 De ...
- Apple Catching(dp)
Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9831 Accepted: 4779 De ...
- BZOJ 3384: [Usaco2004 Nov]Apple Catching 接苹果( dp )
dp dp( x , k ) = max( dp( x - 1 , k - 1 ) + *** , dp( x - 1 , k ) + *** ) *** = 0 or 1 ,根据情况 (BZOJ 1 ...
- 3384/1750: [Usaco2004 Nov]Apple Catching 接苹果
3384/1750: [Usaco2004 Nov]Apple Catching 接苹果 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 18 Solv ...
随机推荐
- Ubuntu下安装C/C++开发环境【!!!有更新!!!Ubuntu14.10下使用eclipse搭建C语言开发环境】
(1)第一步安装Eclipse,有两种方法,使用软件市场搜索就可以得到,安装就可以 另外一种是使用终端安装,命令例如以下: sudo su进入root模式 输入password 然后 输入:sudo ...
- MySQL hash分区(四)
具体描写叙述总结请看MySQL分区(一) 样例:该样例为本人个人学习总结分享->具体说明-->有问题欢迎前来交流 watermark/2/text/aHR0cDovL2Jsb2cuY3Nk ...
- Elasticsearch安装中文分词插件ik
Elasticsearch默认提供的分词器,会把每一个汉字分开,而不是我们想要的依据关键词来分词.比如: curl -XPOST "http://localhost:9200/userinf ...
- research plan1111
Hello prof.Choi 感谢您的来电,与您的这次通话我已经期盼了很久.我来做个自我介绍,我叫陈飞,今年27岁了,是河北地质大学计算机科学专业的本科毕业生.我非常想提高自己的学历,现在经过刘老师 ...
- Kubernetes——自动扩展容器!假设你突然需要增加你的应用;你只需要告诉deployment一个新的 pod 副本总数即可
参考:http://kubernetes.kansea.com/docs/hellonode/ 现在你应该可以通过这个地址来访问这个service: http://EXTERNAL_IP:8080 或 ...
- P2120 [ZJOI2007]仓库建设 斜率优化dp
好题,这题是我理解的第一道斜率优化dp,自然要写一发题解.首先我们要写出普通的表达式,然后先用前缀和优化.然后呢?我们观察发现,x[i]是递增,而我们发现的斜率也是需要是递增的,然后就维护一个单调递增 ...
- eclipse和jdk版本对应问题
日常开发中,32位eclipse要用32位jdk,64位则必须要用64位jdk,否则启动时就会报错,load jvm.dll失败,昨天又遇到了这个问题.更换对应的版本之后就好了.tomcat等应用也有 ...
- Robert 的军队
题目描述 Winter is coming. Robert 是个昏庸的君主,整日沉迷于吃喝玩乐,终于,当寒冬降临,他不得不组 织军队来对抗敌人. 尽管如此,他仍然是个喜欢玩耍的人,还有点强迫症,他希望 ...
- Ruby 遍历多个数组
puts("----------------------------------------") puts(" 多重指定 test") ...
- cookie应用(一周内免登陆)
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...