一、题目

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的更多相关文章

  1. poj 2385【动态规划】

    poj 2385 Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14007   Accepte ...

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

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

  3. 【POJ - 2385】Apple Catching(动态规划)

    Apple Catching 直接翻译了 Descriptions 有两棵APP树,编号为1,2.每一秒,这两棵APP树中的其中一棵会掉一个APP.每一秒,你可以选择在当前APP树下接APP,或者迅速 ...

  4. 【DP】POJ 2385

    题意:又是Bessie 这头牛在折腾,这回他喜欢吃苹果,于是在两棵苹果树下等着接苹果,但苹果不能落地后再接,吃的时间不算,假设他能拿得下所有苹果,但是这头牛太懒了[POJ另一道题目说它是头勤奋的奶牛, ...

  5. 二分+动态规划 POJ 1973 Software Company

    Software Company Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 1112   Accepted: 482 D ...

  6. [ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)

    Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...

  7. DP:Apple Catching(POJ 2385)

    牛如何吃苹果 问题大意:一个叫Bessie的牛,可以吃苹果,然后有两棵树,树上苹果每分钟会掉一个,这只牛一分钟可以在两棵树中往返吃苹果(且不吃地上的),然后折返只能是有限次W,问你这只叫Bessie的 ...

  8. POJ 2385 Apple Catching

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

  9. A-Apple Catching(POJ 2385)

    Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8759   Accepted: 4264 De ...

  10. POJ 2385 DP

    题意:在苹果树下,初始在第一棵树下,告诉你在第几秒的时候,那棵树下会落下苹果,告诉最多能移动的次数,然后来回移动,求能得到的最大的苹果数目. 思路:三维DP,d[第i秒][已经移动j次][当前在(1, ...

随机推荐

  1. 腾讯云centos7.2安装mysql5.7

    一.查看是否安装mysql rpm -qa | grep mysql 什么都没显示,说明没有安装 二.进入到opt目录下,使用wget下载官方yum源的rpm包 cd /opt wget https: ...

  2. 阿里云linux服务器打开端口号

    之前linux回滚了下,然后就连不上xshell和filezille了,后台安全配置哪里也都打开了端口号了,还是不行.然后我就想重启下ssh服务 ,执行service sshd restart 提示1 ...

  3. 使用canvas通过js制作一个小型英雄抓怪兽的2D小游戏

    首先,这是一个HTML: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  4. [agc008E]Next or Nextnext-[dp+思考题]

    Description 传送门 Solution 官方题解 然后我谈下个人理解.由于我们的两个条件只要任意满足,则在p的图中i有两种连边法:i->p[i],i->p[p[i]]. 我们考虑 ...

  5. 在CMD 中,如何切换python2.x 版本 和 python3.x版本?

    1.cmd中输入python,启动python2.x版本,如何切换呢? 2.找到python2.x安装路径,修改python.exe名称为:python27.exe 3. 打开cmd,输入python ...

  6. #2007. 「SCOI2015」国旗计划

    好久没更过博了.. 首先断环为链,因为线段互相不包含,所以对每个线段\(i\)可以找一个满足\(r_j\geq l_i\)的\(l_j\)最小的线段,dp的时候\(i\)就会从\(j\)转移过来 然后 ...

  7. vmware打开vmx文件不能创建虚拟机的问题

    这种情况一般结束 vmware-tray 进程,然后直接在文件管理器里打开vmx文件即可创建(打开方式为vmware), 直接在vmware里打开虚拟机文件可能会出现无反应(不创建虚拟机)的情况.

  8. android targetSdkVersion>=26收不到广播的处理

    背景:GP新政策,要求Google Player上架应用的targetSdkVersion>=26. 一. 为啥GP要求targetSdkVersion>=26? 1 targetSdkV ...

  9. C语言 知识点总结完美版

    本文采用思维导图的方式撰写,更好的表述了各知识点之间的关系,方便大家理解和记忆.这个总结尚未包含C语言数据结构与算法部分,后续会陆续更新出来,文中有漏掉的知识点,还请大家多多指正. 总体上必须清楚的: ...

  10. web测试通用要点大全(Web Application Testing Checklist)

    在测试工作中经常遇到测试同一控件功能的情景,这样几年下来也积累了各种测试功能控件的checklist,过年期间抽空整理分享出来.通过下面的清单,任何测试新手都可以快速写出媲美工作好几年的测试老鸟的测试 ...