poj2385 - Apple Catching【动态规划】
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.
Source
思路:首先,我们定义了一个num数组,其中num[0][i]表示第i分钟时第一颗树掉苹果,num[1][i]表示第i分钟时第二颗树掉苹果。
我们可以推导
1. 当j==0时,也就是说没有移动一次,dp[i][j]=dp[i-1][j]+num[0][i];
2. 当j!=0时, dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+num[j%2==1][i];其中num数组是判断他要去的那棵树是否有苹果掉下来。
大概思路就是这样,具体细节看代码。
#include<cstdio>
#include<cstring>
#include <iostream>
using namespace std;
const int maxn=1005;
int num[2][maxn],dp[maxn][35];
int main()
{
int t,w;
scanf("%d%d",&t,&w);
memset(num,0,sizeof(num));
for(int i=1;i<=t;++i)
{
int x;
scanf("%d",&x);
if(x==1) //第一颗树掉苹果
num[0][i]=1;
else //第二颗树掉苹果
num[1][i]=1;
}
memset(dp,0,sizeof(dp));
dp[1][0]=num[0][1],dp[1][1]=num[1][1]; //初始化,临界条件
for(int i=2;i<=t;++i)
{
for(int j=0;j<=w && j<=i;++j)
{
if(j==0)
dp[i][j]=dp[i-1][j]+num[0][i];
else
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+num[j%2==1][i];
} //转移与不转移比较,选择较大值
}
printf("%d\n",dp[t][w]);
return 0;
}
poj2385 - Apple Catching【动态规划】的更多相关文章
- POJ2385——Apple Catching
$Apple~Catching$ Time Limit: 1000MS Memory Limit: 6553 ...
- poj2385 Apple Catching (线性dp)
题目传送门 Apple Catching Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 154 ...
- poj2385 Apple Catching(dp状态转移方程推导)
https://vjudge.net/problem/POJ-2385 猛刷简单dp的第一天的第一题. 状态:dp[i][j]表示第i秒移动j次所得的最大苹果数.关键要想到移动j次,根据j的奇偶判断人 ...
- poj2385 Apple Catching
思路: 简单dp. 实现: #include <iostream> #include <cstdio> #include <cstring> using names ...
- 【POJ - 2385】Apple Catching(动态规划)
Apple Catching 直接翻译了 Descriptions 有两棵APP树,编号为1,2.每一秒,这两棵APP树中的其中一棵会掉一个APP.每一秒,你可以选择在当前APP树下接APP,或者迅速 ...
- Apple Catching(POJ 2385)
Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9978 Accepted: 4839 De ...
- Apple Catching(dp)
Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9831 Accepted: 4779 De ...
- BZOJ 3384: [Usaco2004 Nov]Apple Catching 接苹果( dp )
dp dp( x , k ) = max( dp( x - 1 , k - 1 ) + *** , dp( x - 1 , k ) + *** ) *** = 0 or 1 ,根据情况 (BZOJ 1 ...
- 3384/1750: [Usaco2004 Nov]Apple Catching 接苹果
3384/1750: [Usaco2004 Nov]Apple Catching 接苹果 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 18 Solv ...
随机推荐
- ClipboardEvent.clipboardData
ClipboardEvent.clipboardData https://developer.mozilla.org/en-US/docs/Web/API/ClipboardEvent/clipboa ...
- 机器视觉: LBP-TOP
之前介绍过机器视觉中常用到的一种特征:LBP http://blog.csdn.net/matrix_space/article/details/50481641 LBP可以有效地处理光照变化,在纹理 ...
- ngRoute (angular-route.js) 和 ui-router (angular-ui-router.js) 模块有什么不同呢?
ngRoute (angular-route.js) 和 ui-router (angular-ui-router.js) 模块有什么不同呢? 很多文章中都有说道:当时ngRoute在路由配置时用$r ...
- shell脚本-数组
shell脚本-数组 数组 变量:存储单个元素的内存空间. 数组:存储多个元素的连续的内存空间,相当于多个变量的集合. 数组索引:编号从0开始,属于数值索引.索引可支持使用自定义的格式,而不仅是数值格 ...
- 【WIP】Ruby CSV文件操作
创建: 2017/09/30 ...
- Akka源码分析-Remote-网络链接生命周期
remote模式下,网络链接的生命周期往往影响着对应Actor的生命周期,那么网络链接的生命周期是怎么样的呢? 每一个与远程系统的链路都是四个状态之一:空闲.活跃.被守护.被隔离.远程系统的某个地址没 ...
- EF 新增数据时提示it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element
it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionM ...
- Python--10、生产者消费者模型
生产者消费者模型(★) 平衡生产线程和消费线程的工作能力来提高程序的整体处理数据的速度.程序中有两类角色:生产数据.消费数据实现方式:生产->队列->消费. 通过一个容器来解决生产者和消费 ...
- Android开发初体验
本文通过开发一个应用来学习Android基本概念及构成应用的UI组件. 开发的应用名叫GeoQuiz,它能给出一道道地理知识问题.用户点击true或false按钮回答问题,应用即时做出反馈 第一步请先 ...
- android shape stroke只绘制一边或者某几边
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android=" ...