poj 2385 Apple Catching(dp)
Description
It is a little known fact that cows love apples. Farmer John has two apple trees (which are conveniently numbered and ) 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 ( <= T <= ,) minutes. Bessie is willing to walk back and forth at most W ( <= W <= ) times. Given which tree will drop an apple each minute, determine the maximum number of apples which Bessie can catch. Bessie starts at tree .
Input
* Line : Two space separated integers: T and W * Lines ..T+: or : the tree that will drop an apple each minute.
Output
* Line : The maximum number of apples Bessie can catch without walking more than W times.
Sample Input
Sample Output
Hint
INPUT DETAILS: Seven apples fall - one from tree , then two in a row from tree , then two in a row from tree , then two in a row from tree . Bessie is willing to walk from one tree to the other twice. OUTPUT DETAILS: Bessie can catch six apples by staying under tree until the first two have dropped, then moving to tree for the next two, then returning back to tree for the final two.
Source
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<cmath>
using namespace std;
#define W 36
#define N 1006
int dp[N][W];
int n,w;
int a[N];
int main()
{
while(scanf("%d%d",&n,&w)==){
//int sum=0;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
memset(dp,,sizeof(dp));
if(a[]==){
dp[][]=;
dp[][]=;
}
if(a[]==){
dp[][]=;
dp[][]=;
} for(int i=;i<=n;i++){
for(int j=;j<=w;j++){
if(j==){
dp[i][j]=dp[i-][j]+(j%+==a[i]);
continue;
}
dp[i][j]=max(dp[i-][j],dp[i-][j-]);
if(j%+==a[i]){
dp[i][j]++;
}
}
}
int ans=dp[n][];
for(int i=;i<=w;i++){
ans=max(ans,dp[n][i]);
}
printf("%d\n",ans); }
return ;
}
还有一种方法:
设dp[i][j]表示找到第i个苹果时,最多走了j步 苹果的最大值
则可以由
前i-1分钟最多走j次
前i-1分钟最多走j-1次
这两个状态转移过来
注意,第二种的转移第j次可以选择走或者不走。因为是最多走j次
跟以前做过的一个树形DP神似
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<cmath>
using namespace std;
#define W 36
#define N 1006
int dp[N][W];
int n,w;
int a[N];
int main()
{
while(scanf("%d%d",&n,&w)==){
//int sum=0;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
memset(dp,,sizeof(dp));
if(a[]==) dp[][]=;
dp[][]=;
for(int i=;i<=n;i++){
for(int j=;j<=w;j++){
if(j==){
dp[i][j]=dp[i-][j]+(j%+==a[i]);
continue;
} dp[i][j]=max(dp[i][j],dp[i-][j]+(j%+==a[i]));
dp[i][j]=max(dp[i][j],dp[i-][j-]+(j%==a[i]));
dp[i][j]=max(dp[i][j],dp[i-][j-]+(j%+==a[i]));
}
}
printf("%d\n",dp[n][w]);
}
return ;
}
poj 2385 Apple Catching(dp)的更多相关文章
- poj 2385 Apple Catching 基础dp
		
Apple Catching Description It is a little known fact that cows love apples. Farmer John has two ap ...
 - POJ 2385 Apple Catching【DP】
		
题意:2棵苹果树在T分钟内每分钟随机由某一棵苹果树掉下一个苹果,奶牛站在树#1下等着吃苹果,它最多愿意移动W次,问它最多能吃到几个苹果.思路:不妨按时间来思考,一给定时刻i,转移次数已知为j, 则它只 ...
 - POJ 2385 Apple Catching ( 经典DP )
		
题意 : 有两颗苹果树,在 1~T 的时间内会有两颗中的其中一颗落下一颗苹果,一头奶牛想要获取最多的苹果,但是它能够在树间转移的次数为 W 且奶牛一开始是在第一颗树下,请编程算出最多的奶牛获得的苹果数 ...
 - POJ - 2385 Apple Catching (dp)
		
题意:有两棵树,标号为1和2,在Tmin内,每分钟都会有一个苹果从其中一棵树上落下,问最多移动M次的情况下(该人可瞬间移动),最多能吃到多少苹果.假设该人一开始在标号为1的树下. 分析: 1.dp[x ...
 - poj 2385 Apple Catching(记录结果再利用的动态规划)
		
传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 有两颗苹果树,在每一时刻只有其中一棵苹果树会掉苹果,而Bessie可以在很短的时 ...
 - POJ 2385 Apple Catching
		
比起之前一直在刷的背包题,这道题可以算是最纯粹的dp了,写下简单题解. 题意是说cows在1树和2树下来回移动取苹果,有移动次数限制,问最后能拿到的最多苹果数,含有最优子结构性质,大致的状态转移也不难 ...
 - POJ 2385 Apple Catching(01背包)
		
01背包的基础上增加一个维度表示当前在的树的哪一边. #include<cstdio> #include<iostream> #include<string> #i ...
 - 【POJ】2385 Apple Catching(dp)
		
Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13447 Accepted: 6549 D ...
 - 动态规划:POJ No 2385 Apple Catching
		
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> ...
 
随机推荐
- App上线流程全攻略(续)-iOS8之后的改动与所遇日常错误
			
随着iOS8的公布,iTunes Connect的界面也是发生了非常大的改变,App 上传到 Store上面的步骤也是发生了些改变.以下继续用图说话: /*********************** ...
 - TTB 基本
			
中文名 ,线程构建模块 外文名 Thread Building Blocks 缩 写 TBB 开 发 intel 目录 1线程构建模块 2黑体亮温 3斜交载重轮胎 4串联球轴承 1 ...
 - Sass函数--map
			
MapSass 的 map 常常被称为数据地图,也有人称其为数组,是以 key:value 成对的出现. $map: ( $key1: value1, $key2: value2, $key3: va ...
 - Katana概述
			
OWIN owin是web services和framework组件之间的抽象.抽象包括两个核心要素: environment dictionary 这个数据结构存储处理HTTP请求必须的状态和相关的 ...
 - (转)div+css 布局经验 - 最简单的 = 最不变形的(原创技巧)
			
站酷几年了 一直饱受其恩泽 尤为感激 一直想奉献些什么 但是苦于水平 苦于奔波 今天静下心来 为大家奉献下 自己的div+css 经验 ,以下观点只代表 深海个人立场 希望为初学者提供一条" ...
 - ubuntu终端命令
			
整个电脑都划成ubuntu用. 装软件时的一个明显感觉就是很多事情,用终端的命令行去做很容易,用图形界面往往很复杂,而且很多时候还会出现权限的问题,对于ubuntu的用户权限,现在的唯一感觉就是权限在 ...
 - meta便签的用法
			
1.定义编码规则,<meta http-equiv="Content-Type" content="text/html; charset=utf-8" / ...
 - eclipse添加xsd
			
图片参考 http://wenku.baidu.com/link?url=DFHWF_yD-M-GCt2tfjs1npPs1xhNlyxik7i_pCBjw3oVlbssYrMvLNucuUpKg75 ...
 - Lua绑定C++类
			
原文:http://blog.csdn.net/chenee543216/article/details/12074771 以下是代码: Animal.h文件 #pragma once #ifndef ...
 - 实验八 sqlite数据库操作
			
实验报告 课程名称 基于Android平台移动互联网开发 实验日期 2016年5月3日 实验项目名称 SQLite数据库操作 实验地点 S30010 实验类型 □验证型 √设计型 □综合型 ...