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[i][j]表示在第i分钟时,已经移动了j次后得到的苹果数量

接着来看状态转移方程:dp[i][j] = max(dp[i-1][j], dp[i-1][j-1]) ,然后判断当前是否在第i分钟掉苹果的那颗树下,是的话,dp[i][j]++

解释(以下提供几种描述):

1.对于第i秒移动j次的状态,可以由两种前驱状态转移得来,一种是i-1秒时就已经移动了j次,然后第i秒就不移动了,另一种是i-1秒移动了j-1次,然后她还可以再移动一次得到移动j次。

2.第i分钟能得到的苹果数量,等于在第i-1分钟时,在树1和树2下得到苹果的最大值(j为偶数则在树1下面,奇数则在树2下面)

3.在第i分钟奶牛到某棵树下有两种状态:1.从另一棵树走过来(dp[i-1][j-1])    2.本来就呆在这棵树下(dp[i-1][j])

由于移动偶数次可以回到树1,移动奇数次可以回到树2,移动j次时,若j%2+1==a[i]就表明当前位置恰好有苹果落下。

最后须注意下初始化与边界计算

附上AC代码:

#include <cstdio>
#include <algorithm>
using namespace std; const int maxn = 1005; int a[maxn], dp[maxn][40]; int main()
{
int t, w;
while(~scanf("%d%d", &t, &w)) {
for(int i = 1; i <= t; i++)
scanf("%d", &a[i]);
if(a[1] == 1) {
dp[1][0] = 1, dp[1][1] = 0;
}
else {
dp[1][0] = 0, dp[1][1] = 1;
}
for(int i = 2; i <= t; i++) {
for(int j = 0; j <= w; j++) {
if(j == 0) dp[i][j] = dp[i-1][j] + a[i]%2;
else {
dp[i][j] = max(dp[i-1][j], dp[i-1][j-1]);
if(j%2+1 == a[i]) dp[i][j]++;
}
}
} int ans = 0;
for(int i = 0; i <= w; i++)
ans = max(ans, dp[t][i]);
printf("%d\n", ans);
} return 0;
}

刚发现有按照我那种思路来的,但是明天要考试了,得看下C语言准备下,先放上博客地址,回头再看

https://www.cnblogs.com/Philip-Tell-Truth/p/4815021.html

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(dp)

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

  6. poj 2385 Apple Catching(dp)

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

  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了,写下简单题解. 题意是说cows在1树和2树下来回移动取苹果,有移动次数限制,问最后能拿到的最多苹果数,含有最优子结构性质,大致的状态转移也不难 ...

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

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

随机推荐

  1. UE5 射线检测排除隐藏的Actor

    0x00 Unreal Engine 5(UE5)以其卓越的性能和直观的开发工具在游戏开发领域占据了重要地位.本系列将深入探讨UE5中射线检测的关键概念,着重介绍处理隐藏Actor的技巧. 0x01. ...

  2. iOS开发基础102-后台保活方案

    iOS系统在后台执行程序时,有严格的限制,为了更好地管理资源和电池寿命,iOS会限制应用程序在后台的运行时间.然而,iOS提供了一些特定的策略和技术,使得应用程序可以在特定场景下保持后台运行(即&qu ...

  3. 带你学习通过GitHub Actions如何快速构建和部署你自己的项目,打造一条属于自己的流水线

    本文主要讲解通过github的actions来对我们项目进行ci/cd 一.actions简介 GitHub Actions 是一种持续集成和持续交付 (CI/CD) 平台,可用于自动执行生成.测试和 ...

  4. 假期小结8XML之LXML

    这桌我初步学习了爬虫相关知识的python库LXML的一些基本用法 以下是我的部分总结 lxml是Python中一个流行的第三方库,用于处理XML和HTML数据.它提供了高效且易于使用的工具,使你能够 ...

  5. ELK多租户方案

    一.前言 日志分析是目前重要的系统调试和问题排查的重要手段之一,而目前分布式系统由于实例和机器众多,所以构建一套统一日志系统是非常必要的:ELK提供了一整套解决方案,并且都是开源软件,之间互相配合使用 ...

  6. 我用Awesome-Graphs看论文:解读Naiad

    Naiad论文:<Naiad: A Timely Dataflow System> 前面通过文章<论文图谱当如是:Awesome-Graphs用200篇图系统论文打个样>向大家 ...

  7. application.properties配置文件存储参数

    配置文件存储参数 当我们需要很多的参数时,项目很大,文件很多,每涉及一个技术,每涉及一个第三方的参数时,当这些参数数据发生变化,修改会相当的麻烦.这时候把参数配置到application.proper ...

  8. douyin 今日头条 巨量登录滑块和douyin详情滑块分析

    声明(lianxi a15018601872) 本文章中所有内容仅供学习交流使用,不用于其他任何目的,抓包内容.敏感网址.数据接口等均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均 ...

  9. 神州笔记本 —— HASEE神州 —— 用户手册(使用功能键)—— 笔记本电脑功能键

    功能键功能: FN+f1 启动/关闭 触摸板 FN+f2 启动/关闭 屏幕背光 FN+f3 启动/关闭 喇叭和外接耳机 FN+f5 减低音量 FN+f6 提高音量 FN+f7 切换屏幕 FN+f8 降 ...

  10. 第7期(大连站)—— OpenHarmony城市技术论坛:边缘智能

    PS. 为了进一步的推动国产信息化,国内的各个高校也是踊跃参与呢.