题意:

给你两个数字n和m;代表会有n个苹果掉落,m次可以移动的机会;有两棵树,开始你站在树1下面,一分钟只能移动一次,下面的数值代表在哪一颗树下会掉落苹果;问你在可移动的范围内,最多可以接到多少个苹果?

题目:

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).基础dp题,按照题意很容易看出有两个变量,分钟和移动机会,所以我们按照习惯写出两层for循环,简单思考其中的关系,就可以得到dp定义,dp[i][j]dp[i][j]dp[i][j],表示在i时间内,用 jjj次转移机会得到的最大苹果数.
(2).因为开始的位置在第一棵树下,那么我们就可以由移动的步数 jjj的奇偶性判断现在在哪颗树下;
(3).dp转移如下,如果 j==0j==0j==0,则 dp[i][j]=dp[i−1][j]dp[i][j]=dp[i-1][j]dp[i][j]=dp[i−1][j],考虑j为0时,到第i分钟,一步都没有走动。
否则 dp[i][j]=max(dp[i−1][j],dp[i−1][j−1])dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])dp[i][j]=max(dp[i−1][j],dp[i−1][j−1]).就是当前从上一个状态下,走还是不走获得苹果最多。
(4)如果现在所在的树的编号和现在掉落的苹果所在的位置相同,那么接到的苹果的数量姐增加,由(2)这个我就放在最后判断一下就行,也可以放在第三步,直接在推导式里面。
(5).最后在 dp[n][i]dp[n][i]dp[n][i]里找最大值就行了,(0<=i<=n).(0<=i<=n).(0<=i<=n).表示一共走i步时,n分钟吃到苹果最大值。
我今天真的闲的罗里吧嗦这么多,自恋的觉得只要跟着我的思路走,一定能会这道题,hhh

AC模板:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int M=1e3+10;
int n,m,ans;
int s[M],dp[M][35];
int main()
{while(~scanf("%d%d",&n,&m)){ans=0;memset(dp,0,sizeof(dp));for(int i=1; i<=n; i++)scanf("%d",&s[i]);if(s[1]==1)dp[1][0]=1;elsedp[1][1]=1;for(int i=2; i<=n; i++)for(int j=0; j<=i&&j<=m; j++){if(j==0)//考虑j为0时,也就是说,到第i分钟,一步都没有走动。dp[i][j]=dp[i-1][j]+s[i]%2;else{dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]);//就是当前走还是不走获得苹果最多。if(j%2+1==s[i])//因为最开始站在树1下面,可以用走多少步的奇偶来表示当前在那棵树下。dp[i][j]++;}}for(int i=0; i<=m; i++)//表示一共走i步时,n分钟吃到苹果最大值。ans=max(ans,dp[n][i]);printf("%d\n",ans);}return 0;
}

备战ccpc分站赛ing ,题目分析简略,见谅,转载请注明出处。。。。。

Apple Catching POJ - 2385(基础的动态规划算法)相关推荐

  1. poj2385 基础的动态规划算法 挑战程序设计竞赛

    2018-2-5 基本的动态规划算法,不知道自己为什么WA,很是绝望,于是只是把代码贴一下? #include<iostream> #include<cstring> usin ...

  2. Cow Bowling POJ - 3176(基础的动态规划算法)

    题意: 杨辉三角,让从顶部开始走到底部,所经过的每一层的点数相加,使得实现最高和. 题目: The cows don't use actual bowling balls when they go b ...

  3. poj3616 基础的动态规划算法 《挑战程序设计竞赛》

    2018-2-5 一开始在考虑这个R要怎么处置,后来突然想到直接把他加到结束时间e后面即可,然后对endtime进行排序,然后找出状态转移方程即可,由于给的数字比较大,我们可以先写成二维的,然后再对二 ...

  4. poj2229 基础的动态规划算法 挑战程序设计竞赛

    2018-2-2 首先我们不难看出: 当n为奇数时,dp[n]=dp[n-1],因为它无论如何都会有1在里面. 当n为偶数时,我们通过题意可以看出,它的序列是无序的,换言之,1,1,2和2,1,1是相 ...

  5. poj3176 基础的动态规划算法 挑战程序设计竞赛

    2018-2-2 最容易想到的一种,直接求解,后面会进行优化. #include<iostream> #include<cstring> using namespace std ...

  6. Apple Catching

    Apple Catching - POJ 2385 - Virtual Judge https://vjudge.net/problem/POJ-2385 题意:有两棵树,每分钟树上会掉下一颗苹果,问 ...

  7. Apple Catching经典dp

    Apple Catching - POJ 2385 - Virtual Judge 状态:第i分钟移动j次能吃多少个苹果 决策:第i分钟动不动 转移:如果移动,dp[i][j]=dp[i-1][j-1 ...

  8. 算法基础知识——动态规划

    算法基础知识--动态规划 目录: 基础知识 分治法和动态规划的区别 动态规划算法设计步骤 最优子结构性质定义 动态规划两种等价的实现方法(自顶向下带备忘.自底向上) 子问题图 经典问题 钢条切割 矩阵 ...

  9. 1. 通用基础算法(1.7动态规划算法/1.8模拟算法/1.9其他算法)

    7  动态规划算法 动态规划(Dynamic Programming)是求多阶段决策过程(Multistep Decision Process)最优化的一种数学方法,它将问题的整体按时间或空间的特征分 ...

最新文章

  1. Python游戏开发,Pygame模块,Python从零开始带大家实现一个魔塔小游戏
  2. js循环解析html标签,riot.js教程【六】循环、HTML元素标签
  3. PAT甲级1083 List Grades:[C++题解]结构体、排序
  4. MM夏天化妆不掉妆的技巧
  5. sort+参数+linux,linux sort下令参数及用法详解
  6. leetcode刷题 74.搜索二维矩阵
  7. 解决 idea 运行 Spring Boot 项目启动慢的问题
  8. Eclipse 工具栏不见了
  9. python float 精度_谈谈关于Python里面小数点精度控制的问题
  10. 程序员面试技巧:如何讲解自己做过的项目
  11. RadiAnt DICOM Viewer 2021 绿色版
  12. Redis缓存——快速入门
  13. 信号处理之CFAR恒虚警检测
  14. 常用的人脸表情数据库简介及其论文出处
  15. Unity一键更换TextMeshPro的字体
  16. 【计算机网络】网络层 : 无分类编址 CIDR ( 编址发展 | CIDR 优点 | CIDR 相关计算 | 构成超网 | 最长前缀匹配 | 计算示例 )★
  17. 【笔记本Windows的两个ctrl键失效解决办法大全解】
  18. 养一只”无限猴子”帮你测试
  19. 三国演义-(01不打不相识)
  20. 绩效反馈评语:如何评估团队合作

热门文章

  1. Android之如何让Android studio编译更快
  2. Android之CheckBox复选框控件使用inelayout.xml Xml代码
  3. python闭环最短路径_深度学习经典算法 | 蚁群算法解析
  4. 我的世界服务器物品属性,属性 - Minecraft Wiki,最详细的官方我的世界百科
  5. 超级计算机适用于科学计算,中国科学院
  6. 放寒假的硕博研究生将经历什么?
  7. 堪称经典!这部由苏联最杰出数学家编写的数学教材,为何能大受推崇?
  8. 有些图,只要看错一眼就再也回不去了!
  9. 来自女朋友的灵魂拷问!| 今日最佳
  10. 连锁反应装置积木好玩到尖叫!