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> ...
随机推荐
- loadrunner java 缺少必要的导入包报错
loadrunner 运行从eclipse中做好的脚本,ctrl + A 复制到loadrunner中来, 添加参数化的的语句:verifyCode = lr.eval_string (&quo ...
- Swift字符串常用操作总结
转自:http://www.jianshu.com/p/52e7580166ff 1.string转换为Int/Long/Float/Double/Bool等 var str1="100&q ...
- [Javascript] Redirect the browser using JavaScript
Three methods to preform redirection in browser: widnow.location.href window.location.assign window. ...
- WebApi2官网学习记录---Content Negotiation
Content Negotiation的意思是:当有多种Content-Type可供选择时,选择最合适的一种进行序列化并返回给client. 主要依据请求中的Accept.Accept-Charset ...
- (转)SQL中的ISNULL函数介绍
SQL中有多种多样的函数,下面将为您介绍SQL中的ISNULL函数,包括其语法.注释.返回类型等,供您参考,希望对您学习SQL能够有所帮助. ISNULL 使用指定的替换值替换 NULL. 语法ISN ...
- 《第一行代码》学习笔记7-活动Activity(5)
1.Intent中只能指定一个action,但却能指定多个category. 2.使用隐式Intent,不仅可以启动自己程序内的活动,还可以启动其他程序的活动,使得Android应用程序之间 的功能共 ...
- HTTP get、post请求
Post请求: string postData = "user=123&pass=456"; // 要发放的数据 byte[] byteArray = Encoding.U ...
- java内部类实现多继承
class Example1 { public String name() { return "liutao"; } } class Example2 { public int a ...
- 1、Spark 通过api,hfile两种形式获取hbase数据,简单样例
pom内容: <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-se ...
- nyoj 素数环
算法:搜索 描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简便起见,我们规定每个素数环都从1开始.例如,下图就是6的一个素数环. 输 ...