Description

It is a little known fact that cows love apples. Farmer John has two apple trees (which are conveniently numbered  and ) 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 ( <= T <= ,) minutes. Bessie is willing to walk back and forth at most W ( <= W <= ) times. Given which tree will drop an apple each minute, determine the maximum number of apples which Bessie can catch. Bessie starts at tree .

Input

* Line : Two space separated integers: T and W 

* Lines ..T+:  or : the tree that will drop an apple each minute.

Output

* Line : The maximum number of apples Bessie can catch without walking more than W times.

Sample Input


Sample Output


Hint

INPUT DETAILS: 

Seven apples fall - one from tree , then two in a row from tree , then two in a row from tree , then two in a row from tree . Bessie is willing to walk from one tree to the other twice. 

OUTPUT DETAILS: 

Bessie can catch six apples by staying under tree  until the first two have dropped, then moving to tree  for the next two, then returning back to tree  for the final two.

Source

 
设dp[i][j]表示找到第i个苹果时,走了j步时 苹果的最大值。
首先要初始化,见代码
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]);表示走或不走取最大值。然后判断是否能够dp[i][j]++。最后找出最大值
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<cmath>
using namespace std;
#define W 36
#define N 1006
int dp[N][W];
int n,w;
int a[N];
int main()
{
while(scanf("%d%d",&n,&w)==){
//int sum=0;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
memset(dp,,sizeof(dp));
if(a[]==){
dp[][]=;
dp[][]=;
}
if(a[]==){
dp[][]=;
dp[][]=;
} for(int i=;i<=n;i++){
for(int j=;j<=w;j++){
if(j==){
dp[i][j]=dp[i-][j]+(j%+==a[i]);
continue;
}
dp[i][j]=max(dp[i-][j],dp[i-][j-]);
if(j%+==a[i]){
dp[i][j]++;
}
}
}
int ans=dp[n][];
for(int i=;i<=w;i++){
ans=max(ans,dp[n][i]);
}
printf("%d\n",ans); }
return ;
}

还有一种方法:

设dp[i][j]表示找到第i个苹果时,最多走了j步 苹果的最大值

则可以由

           前i-1分钟最多走j次

           前i-1分钟最多走j-1次

     这两个状态转移过来

注意,第二种的转移第j次可以选择走或者不走。因为是最多走j次

跟以前做过的一个树形DP神似

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<cmath>
using namespace std;
#define W 36
#define N 1006
int dp[N][W];
int n,w;
int a[N];
int main()
{
while(scanf("%d%d",&n,&w)==){
//int sum=0;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
memset(dp,,sizeof(dp));
if(a[]==) dp[][]=;
dp[][]=;
for(int i=;i<=n;i++){
for(int j=;j<=w;j++){
if(j==){
dp[i][j]=dp[i-][j]+(j%+==a[i]);
continue;
} dp[i][j]=max(dp[i][j],dp[i-][j]+(j%+==a[i]));
dp[i][j]=max(dp[i][j],dp[i-][j-]+(j%==a[i]));
dp[i][j]=max(dp[i][j],dp[i-][j-]+(j%+==a[i]));
}
}
printf("%d\n",dp[n][w]);
}
return ;
}

poj 2385 Apple Catching(dp)的更多相关文章

  1. poj 2385 Apple Catching 基础dp

    Apple Catching   Description It is a little known fact that cows love apples. Farmer John has two ap ...

  2. POJ 2385 Apple Catching【DP】

    题意:2棵苹果树在T分钟内每分钟随机由某一棵苹果树掉下一个苹果,奶牛站在树#1下等着吃苹果,它最多愿意移动W次,问它最多能吃到几个苹果.思路:不妨按时间来思考,一给定时刻i,转移次数已知为j, 则它只 ...

  3. POJ 2385 Apple Catching ( 经典DP )

    题意 : 有两颗苹果树,在 1~T 的时间内会有两颗中的其中一颗落下一颗苹果,一头奶牛想要获取最多的苹果,但是它能够在树间转移的次数为 W 且奶牛一开始是在第一颗树下,请编程算出最多的奶牛获得的苹果数 ...

  4. POJ - 2385 Apple Catching (dp)

    题意:有两棵树,标号为1和2,在Tmin内,每分钟都会有一个苹果从其中一棵树上落下,问最多移动M次的情况下(该人可瞬间移动),最多能吃到多少苹果.假设该人一开始在标号为1的树下. 分析: 1.dp[x ...

  5. poj 2385 Apple Catching(记录结果再利用的动态规划)

    传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 有两颗苹果树,在每一时刻只有其中一棵苹果树会掉苹果,而Bessie可以在很短的时 ...

  6. POJ 2385 Apple Catching

    比起之前一直在刷的背包题,这道题可以算是最纯粹的dp了,写下简单题解. 题意是说cows在1树和2树下来回移动取苹果,有移动次数限制,问最后能拿到的最多苹果数,含有最优子结构性质,大致的状态转移也不难 ...

  7. POJ 2385 Apple Catching(01背包)

    01背包的基础上增加一个维度表示当前在的树的哪一边. #include<cstdio> #include<iostream> #include<string> #i ...

  8. 【POJ】2385 Apple Catching(dp)

    Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13447   Accepted: 6549 D ...

  9. 动态规划:POJ No 2385 Apple Catching

    #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> ...

随机推荐

  1. hdu 5422 Rikka with Graph(简单题)

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...

  2. iOS 原生二维码扫描,带扫描框和扫描过程动画

    在代码中使用了相对布局框架Masonry 准备两张图片,一张是扫描边框,一张是扫描时的细线分别命名 scanFrame.png和scanLine.png并提前放入工程 导入相对布局头文件 #defin ...

  3. qt 3d 绘图

    首先不得不说,要感谢北京邮电大学的阿科.感谢他慷慨的分享和极具科学态度的记录,将自己搜集到的众多资料收集整理发布,拯救众多苦逼寻找方案的程序员于苦海之中.因为最近接手新的项目,涉及到使用opengl做 ...

  4. 关闭归档提示:ORA-38774: cannot disable media recovery - flashback database is enabled

    SQL> select * from v$version; Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit P ...

  5. 虚拟现实,增强现实,VR,AR

    现在的热点不止VR,还有AR和披着MR.HR.CR外衣的各种高级AR们,所以比较着一起说.以下知乎上一网友观点,放几条结论:1.近期(未来两三年)看,VR能火,AR尚待成熟: 2.VR设备中,插片式是 ...

  6. Test failed.尝试加载Oracle客户端库时引发BadImageFormatException

    CodeSmith6.5不像前几个版本,需要用户手动添加oracle驱动,内部已经集成了oracle的驱动. 网上遇到很多win7 64位机子使用CodeSmith连接oracle的时候出现错误如下:

  7. html5新特性--音频视频,拖放

    1.音频 <audio controls> <source src="aaa.ogg" type="audio/ogg"> <so ...

  8. 在shell中运行以不同方式运行脚本

    在shell当中,可以有3中方式运行脚本: 1 . ./script_name 或者source ./script_name 2 直接./script_name 3 ./script_name &am ...

  9. CSS Reset方法

    CSS Reset 即重设浏览器的样式.在各种浏览器中,都会对CSS的选择器默认一些数值,譬如当h1没有被设置数值时,显示一定大小. 但并不是所有的浏览器都使用一样的数值,所以,有了CSS Reset ...

  10. ashx中session的使用

    在平常的页面上是用是很容易就的到request,response对像,从而对其进行一些操作,但在ashx(一般处理程序)中却是有一点的不同, 在ashx你无法正常的使用session,即 contex ...