poj 2385

Apple Catching
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14007   Accepted: 6838

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.

 
题意:有树1和树2,他们在每一时刻只有一棵树落苹果,给定时间T,可在两棵树间移动的最大次数W,初始时站在树1下面。求能接到苹果数的最大值。
题解:定义状态dp[i][j]表示在i时刻移动了j次时能得到的最大苹果数。状态的含义很重要!则dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+(移动j次后对应的树正好落苹果?1:0)
  方程想出来了,但是细节,边界处理,循环上还不会。。。显然移动次数为0时状态只能由上一个时间不移动的状态转移过来,即dp[i][0]=dp[i-1][0]+app[i]%2(初始时在树1下),而且有在第一时刻时若树1落苹果则dp[1][0]=1,否则dp[1][1]=1。由于是最多能移动W次并不是一定要移动W次,所以最后在0~W次结果中取最大值。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; int dp[][];
int app[]; int main()
{
int T,W;
scanf("%d%d",&T,&W);
memset(dp,,sizeof(dp));
for(int i=;i<=T;i++){
scanf("%d",&app[i]);
}
if(app[]==) dp[][]=;
else dp[][]=;
for(int i=;i<=T;i++){
dp[i][]=dp[i-][]+app[i]%;
for(int j=;j<=W;j++){
dp[i][j]=max(dp[i-][j],dp[i-][j-]);
if(j%+==app[i]) dp[i][j]++;
}
}
int ans=;
for(int i=;i<=W;i++)
ans=max(ans,dp[T][i]);
printf("%d\n",ans);
return ;
}
 

poj 2385【动态规划】的更多相关文章

  1. poj 2385 Apple Catching(记录结果再利用的动态规划)

    传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 有两颗苹果树,在每一时刻只有其中一棵苹果树会掉苹果,而Bessie可以在很短的时 ...

  2. 【POJ - 2385】Apple Catching(动态规划)

    Apple Catching 直接翻译了 Descriptions 有两棵APP树,编号为1,2.每一秒,这两棵APP树中的其中一棵会掉一个APP.每一秒,你可以选择在当前APP树下接APP,或者迅速 ...

  3. nyoj 17-单调递增最长子序列 && poj 2533(动态规划,演算法)

    17-单调递增最长子序列 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:21 submit:49 题目描述: 求一个字符串的最长递增子序列的长度 如 ...

  4. 【DP】POJ 2385

    题意:又是Bessie 这头牛在折腾,这回他喜欢吃苹果,于是在两棵苹果树下等着接苹果,但苹果不能落地后再接,吃的时间不算,假设他能拿得下所有苹果,但是这头牛太懒了[POJ另一道题目说它是头勤奋的奶牛, ...

  5. poj 3034 动态规划

    思路:这是一道坑爹的动态规划,思路很容易想到,就是细节. 用dp[t][i][j],表示在第t时间,锤子停在(i,j)位置能获得的最大数量.那么只要找到一个点转移到(i,j)收益最大即可. #incl ...

  6. poj 2498 动态规划

    思路:简单动态规划 #include<map> #include<set> #include<cmath> #include<queue> #inclu ...

  7. poj 2287 动态规划

    用贪心简单证明之后就是一个从两头取的动态规划 #include <iostream> #include <cstring> #include <cstdio> #i ...

  8. POJ 2533 动态规划入门 (LIS)

    Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42914 Accepte ...

  9. DP:Apple Catching(POJ 2385)

    牛如何吃苹果 问题大意:一个叫Bessie的牛,可以吃苹果,然后有两棵树,树上苹果每分钟会掉一个,这只牛一分钟可以在两棵树中往返吃苹果(且不吃地上的),然后折返只能是有限次W,问你这只叫Bessie的 ...

随机推荐

  1. Dock镜像初探索

    一.安装CentOS版DockerCE 1.1 卸载旧的版本 yum remove docker \ docker-client \ docker-client-latest \ docker-com ...

  2. 在rabbitmq操作页面上添加队列、交换器及绑定示图

    1.添加队列 2.添加交换器 3.绑定

  3. 基于MaxCompute打造轻盈的人人车移动端数据平台

    摘要: 2019年1月18日,由阿里巴巴MaxCompute开发者社区和阿里云栖社区联合主办的“阿里云栖开发者沙龙大数据技术专场”走近北京联合大学,本次技术沙龙上,人人车大数据平台负责人吴水永从人人车 ...

  4. Unknown command: crawl

    Use "scrapy" to see available commands 1.使用命令行方式cmd,是因为没有cd到项目的根目录,crawl会去搜索cmd目录下的scrapy. ...

  5. 前端(jQuery)(8)-- jQuery元素遍历

    1.向下遍历 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  6. LINQ(语言集成查询)

    LINQ,语言集成查询(Language Integrated Query)是一组用于c#和Visual Basic语言的扩展.它允许编写C#或者Visual Basic代码以查询数据库相同的方式操作 ...

  7. message d:\WEB_APP_QuChongFu\file\五月.xlsx (文件名、目录名或卷标语法不正确。)

    原因是 文件名或文件夹名中不能出现以下字符:\   /   :   *   ?  "  <  >   | 但是后台读取到的附件的文件路径就是这样的 网上大佬说了,这样处理 rep ...

  8. Eviews9.0---软件安装

    EViews是Econometrics Views的缩写,直译为计量经济学观察,通常称为计量经济学软件包.它的本意是对社会经济关系与经济活动的数量规律,采用计量经济学方法与技术进行“观察”.计量经济学 ...

  9. 2019-4-16-C#-在-8.0-对比-string-和-string_-的类型

    title author date CreateTime categories C# 在 8.0 对比 string 和 string? 的类型 lindexi 2019-04-16 10:16:56 ...

  10. R语言可视化--qplot函数

    ggplot绘图系统 使用动词名词形容词构造句子的过程,将数据映射到美学属性上的过程,由层组成 绘图函数 qplot() :参数包括美学属性.几何体.面.统计.坐标系.主题这些层次. ggplot() ...