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 ...
随机推荐
- Unity3d中相应各平台Path
IOS: Application.dataPath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xx ...
- JPush 初始化失败,直接按照官方文档的格式写的,portal上的包名肯定不会错,mainfest里面直接指定${applicationId}
错误日志: 11-27 09:59:19.670 26124-26124/? D/dalvikvm: Late-enabling CheckJNI 11-27 09:59:20.008 26124-2 ...
- Kubernetes——自动扩展容器!假设你突然需要增加你的应用;你只需要告诉deployment一个新的 pod 副本总数即可
参考:http://kubernetes.kansea.com/docs/hellonode/ 现在你应该可以通过这个地址来访问这个service: http://EXTERNAL_IP:8080 或 ...
- 要自己当技术使用astgo运营网络电话系统,必须掌握的基本技术
知道什么是centos 知道怎么远程访问centos服务器 (常用工具 Secure Shell Client.WINSCP) 知道重启服务器的命令是 reboot 知道你的服务器是没有图形界面的,所 ...
- PCB SQL Server 代码创建DbLink
代码如下: ) ) ) ) ) SET @serverName = 'DbLinkName' --db链接名 SET @ip = '120.79.36.65' --需连接服务器的IP SET @dbN ...
- 湖南集训Day1
难度 不断网:☆☆☆ 断网:☆☆☆☆ /* 卡特兰数取模 由于数据范围小,直接做. 考试时断网.忘记卡特兰数公式,推错了只有5分. 数学公式要记别每次都现用现搜!!! */ #include<i ...
- 【转】 [MySQL 查询语句]——分组查询group by
group by (1) group by的含义:将查询结果按照1个或多个字段进行分组,字段值相同的为一组(2) group by可用于单个字段分组,也可用于多个字段分组 select * from ...
- 【洛谷3224/BZOJ2733】[HNOI2012]永无乡 (Splay启发式合并)
题目: 洛谷3224 分析: 这题一看\(n\leq100000\)的范围就知道可以暴力地用\(O(nlogn)\)数据结构乱搞啊-- 每个联通块建一棵Splay树,查询就是Splay查询第k大的模板 ...
- Android -----listView的重要属性
android:transcriptMode="alwaysScroll" android:cacheColorHint="#00000000" android ...
- post提交表单的数据查看方式(不是很理解,但要会看,可以找人商讨下,比如崔老师,自己再看一遍HTTP基础)