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

  1. 7 2
  2. 2
  3. 1
  4. 1
  5. 2
  6. 2
  7. 1
  8. 1

Sample Output

  1. 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次结果中取最大值。
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6.  
  7. int dp[][];
  8. int app[];
  9.  
  10. int main()
  11. {
  12. int T,W;
  13. scanf("%d%d",&T,&W);
  14. memset(dp,,sizeof(dp));
  15. for(int i=;i<=T;i++){
  16. scanf("%d",&app[i]);
  17. }
  18. if(app[]==) dp[][]=;
  19. else dp[][]=;
  20. for(int i=;i<=T;i++){
  21. dp[i][]=dp[i-][]+app[i]%;
  22. for(int j=;j<=W;j++){
  23. dp[i][j]=max(dp[i-][j],dp[i-][j-]);
  24. if(j%+==app[i]) dp[i][j]++;
  25. }
  26. }
  27. int ans=;
  28. for(int i=;i<=W;i++)
  29. ans=max(ans,dp[T][i]);
  30. printf("%d\n",ans);
  31. return ;
  32. }
 

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. 浓缩版 《C和指针》基础篇(Chpt.1~Chpt.9)

    导语 近日,笔者在课业之余阅读了<C和指针(Pointers on C)> (by Kenneth A.Reek)一书,从中记录了关于C语言的诸多知识点,包括在C语言基础特性的学习过程中没 ...

  2. 数字统计类题目的非数位DP解法

    ZJOI2010 数字统计 上题题意为求[l,r]区间中每个数字(0~9)出现的次数 一般的做法为将区间当成[0,r]-[0,l-1],然后进行数位DP 但事实上将区间当成[0,r]-[0,l-1]后 ...

  3. bzoj 1179 [Apio2009]Atm——SCC缩点+spfa

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1179 显然SCC缩点. 然后准备倒着拓扑序推到st,结果WA. 听TJ说dj求最长路会发生不 ...

  4. Python 爬取高清桌面壁纸

    今天写了一个脚本用来爬取ZOL桌面壁纸网站的高清图片: 链接:http://desk.zol.com.cn/1920x1080/ 本程序只爬了美女板块的图片,若要下载其他板块,只需修改程序中的&quo ...

  5. IE9没有内置鼠标手势,还要自己写

    写了个IE插件,然后获取鼠标,信息, 模拟了鼠标手势,在虚拟机里面测试,完全好使,但是现在又不敢在Win7上用了. 愁死了... 为了实现一个鼠标手势. 写的那破玩意,竟然50多K.....太大了.. ...

  6. java-多线程的入门_进阶总结

    多线程 概述图 1.概述 进程:正在执行中的程序,其实时应用程序在内存中运行的那片空间. 线程:进程中的一个执行单元,负责进程中的程序的运行,一个进程中至少要有一个线程. (进程可以理解为是一个QQ程 ...

  7. 【洛谷P1207】双重回文数 【USACO1.2】

    P1207 [USACO1.2]双重回文数 Dual Palindromes 题目描述 如果一个数从左往右读和从右往左读都是一样,那么这个数就叫做"回文数".例如,12321就是一 ...

  8. linux(centos) 下安装phpstudy 如何命令行进入mysql

    配置了phpstudy 可是进不去MySQL 老是报-bash: mysqld: command not found 解决方法:在Linux环境下运行:ln -s /phpstudy/mysql/bi ...

  9. bzoj 2705 [SDOI2012]Longge的问题——欧拉函数大水题

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2705 撕逼题.不就是枚举gcd==d,求和phi[ n/d ]么. 然后预处理sqrt (n ...

  10. Laravel 安装登录模块

    cmd打开项目目录,执行如下代码即可 php artisan make:auth url访问