Apple Catching

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 14311 Accepted: 7000

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.


解题心得:

  1. 题意就是有两颗苹果树,每一秒钟某一颗树上会掉下来一颗苹果,一个人可以在两棵树下移动,移动时间不计,最多可以移动k次,问这个人最多可以获得多少颗苹果。
  2. 其实这个题最直观的做法就是用记忆化搜索,直接按照题意搜索就行了。当然也可以将记忆化搜索提炼出dp公式出来,写dp状态转移方程式。
  3. 提炼出来dp方程式可以这样表示,dp[i][j]为在i秒最多移动j次可以获得的最大苹果树。状态转移方程为dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]) + (j%2+1 == arr[i]),因为一开始在1位置,所以在移动j次之后所在的位置为j%2+1,代表在第i秒移动j次的状态只能够从第i-1秒移动j-1次和i-1秒移动j次转移而来,然后加上当前是否可以接到苹果的。
记忆化搜索代码:

#include <algorithm>
#include <stdio.h>
#include <cstring>
using namespace std;
const int maxn = 1010;
int dp[maxn][35][3],w,n,scor[maxn]; void init() {
memset(dp,-1,sizeof(dp));
scanf("%d%d",&n,&w);
for(int i=1;i<=n;i++) {
scanf("%d",&scor[i]);
scor[i] %= 2;
}
} int dfs(int times,int change,int pos) {
if(times > n || change > w)
return 0;
if(dp[times][change][pos] != -1)
return dp[times][change][pos];
return dp[times][change][pos] = ((scor[times] == pos) + max(dfs(times+1,change,pos),dfs(times+1,change+1,!pos)));
} int main() {
init();
int ans = max(dfs(1,0,1),dfs(1,0,0));
printf("%d\n",ans);
return 0;
}

dp方程式转移代码

#include <stdio.h>
#include <algorithm>
using namespace std;
const int maxn = 1010;
int dp[maxn][35];
int arr[maxn]; int main() {
int n,k;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
scanf("%d",&arr[i]);
for(int i=1;i<=n;i++) {
for(int j=0;j<=k;j++) {
if(j == 0) {
dp[i][j] = dp[i-1][j] + (j%2 +1 == arr[i]);
continue;
}
dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]);
if(j%2 + 1 == arr[i])
dp[i][j]++;
}
} int ans = 0;
for(int i=0;i<=k;i++)
ans = max(ans,dp[n][i]); printf("%d\n",ans);
return 0;
}

POJ:2385-Apple Catching(dp经典题)的更多相关文章

  1. poj 2385 Apple Catching(dp)

    Description It and ) in his field, each full of apples. Bessie cannot reach the apples when they are ...

  2. poj 2385 Apple Catching 基础dp

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

  3. POJ 2385 Apple Catching【DP】

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

  4. POJ 2385 Apple Catching ( 经典DP )

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

  5. POJ - 2385 Apple Catching (dp)

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

  6. POJ 2385 Apple Catching

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

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

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

  8. POJ 2385 Apple Catching(01背包)

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

  9. 【POJ】2385 Apple Catching(dp)

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

随机推荐

  1. LotusScript_批量更改数据库标识符(id)

    OA开发中经常要搭建测试环境,测试环境的数据库与原数据库不能有ID冲突现象,以防混淆.以下是一个批量修改数据库标识符的方法,其中,取得这些需要更改的数据库,需要导出源服务器上的数据库路径和名称,方法详 ...

  2. (C#) Handling and Raising Events

    Handling and Raising Events .NET Framework 4.5   Other Versions     6 out of 20 rated this helpful - ...

  3. react-native 视频播放器(很不错哦)

    第一步: npm i -S react-native-af-video-player(安装前:先安装: react-native-video.react-native-keep-awake.react ...

  4. MOSS2010中如何用代码给托管元数据类型的栏目赋值

    最近项目中遇到如何用代码给托管元数据类型的栏目赋值问题,经过折腾,现把我的思路和实现方法共享出来,让大家一起来学习学习.相互探讨下. /// <summary> /// 托管元数据 /// ...

  5. ASP.NET 页面之间传递参数方法

    1.通过URL链接地址传递 (1) send.aspx代码 protected void Button1_Click(object sender, EventArgs e) { Request.Red ...

  6. 关于VisualStudio2010发布项目问题

    VisualStudio2010速度还是很给力的,VS2015打开机器就双100%了:VS2010机器上跑起来还是很好用的. 今天编译一个MVC3.0项目,发布时候出现诡异现象:Content文件夹里 ...

  7. js从入门到精通到深入到就业

    本篇博客是我参看人家代码做的总结,个人感觉非常非常好,简单.步步深入,不用花大量时间来学完正本js,只需要把其中的代码理解透彻,上班无压力(上班无压力是指js部分,包括查看框架源代码都有很大帮助) / ...

  8. 【JavaScript 封装库】BETA 3.0 测试版发布!

    /* 源码作者: 石不易(Louis Shi) 联系方式: http://www.shibuyi.net =============================================== ...

  9. Android(java)学习笔记97:使用GridView以及重写BaseAdapter

    1. BaseAdapter: 对于ListView.GridView.Gallery.Spinner等等,它是它们的适配器,直接继承自接口类Adapter的,使用BaseAdapter时需要重写很多 ...

  10. HDU 3639 Hawk-and-Chicken(强连通分量+缩点)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013480600/article/details/32140501 HDU 3639 Hawk-a ...