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> ...
随机推荐
- Android Studio新手全然指引
Android Studio新手全然指引 @author ASCE1885的 Github 简书 微博 CSDN Android Studio的下载及安装 假设你的电脑能够FQ,那么请直接到Andro ...
- 执行curl -sSL 提示curl: (35) SSL connect error
今天,添加容器节点报错,执行如下 curl -sSL https://shipyard-project.com/deploy| ACTION=node DISCOVERY=etcd://192.168 ...
- linux提取指定行至指定位置
grep查找ERROR,定位位置 awk打印到指定行数 sed打印到文本末尾 awk打印到文本末尾 方法一 #!/bin/csh -f if(-f errorlog.rpt) then rm -rf ...
- KVO的概述的使用
一,概述 KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知.简单的说就是每次指定的被观察的对象的属性被修改后,KVO就会自动通知相应 ...
- (二)CodeMirror - 配置项
theme: string theme:'monokai' 引入对应的css, <link rel="stylesheet" href="../theme/mono ...
- SATA接口硬盘加密器
加密卡置于主板与硬盘.光驱之间,透明实时地对写入数据进行加密,对读出数据进行解密,有效防止信息被窃.未经授权的阅读和修改,以及硬盘.光盘丢失.被盗.废弃.非法用户访问而引发的敏感信息泄密问题,为用户打 ...
- ubuntu12.04 安装 ruby1.9.3
sudo apt-add-repository ppa:brightbox/ruby-ng sudo apt-get update sudo apt-get install ruby rubygems ...
- mongodb的连接问题,绑定IP惹的祸
刚刚安装好了 mongodb .对着 mongodb in action上的例子敲了下面的代码: public class TestDBConnect { @Test public void test ...
- kafka相关应用
一.kafka官网地址 http://kafka.apache.org 下载地址: http://kafka.apache.org/downloads.html 二.版本 0.9.0.1 is the ...
- 在Wince模拟器接入网络的方法
我第一次使用wince调用WCF服务的时候总是报错,找了半原因发现程序部署在模拟器中,而模拟器没有连接到网络,所以无法连接到WCF服务器. 以下是wince接入网络的方法: 1.点击模拟 ...