poj 2385 Apple Catching 简单DP
来源:互联网 发布:ubuntu运行apk 编辑:程序博客网 时间:2024/05/22 08:11
Apple Catching
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7108 Accepted: 3469
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.
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.
* 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 22112211
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.
========================================================================
题目大意:很少有人知道母牛也爱吃苹果,John农场里有一只母牛Bessie,和两棵苹果树。Bessie不吃摔烂的苹果所以只能在空中接住苹果来吃。
两棵苹果树每分钟掉落一个苹果,一共掉落T分钟,T个苹果,两棵苹果树不远,Bessie一开始在树1下,如果另一棵树掉苹果,Bessie能赶过去接住。
但是Bessie体力有限,在两棵之间的奔波有次数限制W。提供T、W和每分钟掉苹果的树的编号,问Bessie最多接到多少苹果。
解题思路:就一组数据,猜也是DP了。
既然是DP就要打状态表,横轴是跑动的次数,纵轴掉落是第多少个苹果,状态值是到掉落第n个苹果时跑动 i 次能接到的最多苹果数。
更新行时,跑动次数为0的状态值可以按照哪棵树掉苹果直接填写,跑动次数为 j 的状态值 dp[ i ][ j ] = max( dp[ i-1 ][ j-1 ] , dp[ i-1 ][ j ] )+1。
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.
========================================================================
题目大意:很少有人知道母牛也爱吃苹果,John农场里有一只母牛Bessie,和两棵苹果树。Bessie不吃摔烂的苹果所以只能在空中接住苹果来吃。
两棵苹果树每分钟掉落一个苹果,一共掉落T分钟,T个苹果,两棵苹果树不远,Bessie一开始在树1下,如果另一棵树掉苹果,Bessie能赶过去接住。
但是Bessie体力有限,在两棵之间的奔波有次数限制W。提供T、W和每分钟掉苹果的树的编号,问Bessie最多接到多少苹果。
解题思路:就一组数据,猜也是DP了。
既然是DP就要打状态表,横轴是跑动的次数,纵轴掉落是第多少个苹果,状态值是到掉落第n个苹果时跑动 i 次能接到的最多苹果数。
更新行时,跑动次数为0的状态值可以按照哪棵树掉苹果直接填写,跑动次数为 j 的状态值 dp[ i ][ j ] = max( dp[ i-1 ][ j-1 ] , dp[ i-1 ][ j ] )+1。
//Memory: 448KTime: 32MS#include<cstdio>#include<algorithm> //找了找,c++的max函数不在cmath里也不在cstdlib里居然在这 //仔细查了下也对,c++的max函数的参数是类型通用的,就不需要c的max了int a[1001], dp[1001][31];int main(){ int t, w; scanf("%d%d", &t, &w); for( int i = 1 ; i <= t ; i++ ) scanf("%d", &a[i]); for( int i = 1 ; i <= t ; i++ ) { dp[i][0] = dp[i-1][0]+(a[i]&1); //掉落苹果的是1号树则不跑动能接到苹果 for( int j = 1 ; j <= w ; j++ ) { if( (j&1) != (a[i]&1) ) //如果掉苹果的树编号与跑动次数奇偶性不同,则更新 dp[i][j] = std::max(dp[i-1][j],dp[i-1][j-1])+1; else //如果掉苹果的树编号与跑动次数奇偶性相同,则保持上一行的状态值 dp[i][j] = dp[i-1][j]; } } int ans = 0; for( int i = 0 ; i <= w ; i++ ) //遍历dp数组最后一行找最优状态值 if( dp[t][i] > ans ) ans = dp[t][i]; printf("%d\n", ans); return 0;}
0 0
- poj 2385 Apple Catching 简单DP
- Apple Catching (poj 2385 简单dp)
- POJ 2385 Apple Catching(简单DP)
- Apple Catching poj 2385(简单dp)
- POJ 2385 Apple Catching DP
- POJ 2385 Apple Catching [DP]
- POJ 2385 Apple Catching DP
- poj 2385 Apple Catching DP
- POJ 2385 Apple Catching (DP)
- POJ 2385 Apple Catching DP
- poj 2385 Apple Catching DP
- poj 2385 Apple Catching dp
- poj 2385 Apple Catching DP
- POJ 2385 - Apple Catching [DP]
- POJ 2385 Apple Catching (DP)
- poj 2385 Apple Catching dp
- [POJ 2385]Apple Catching[DP]
- Poj 2385 Apple Catching【dp】
- 证明不是哈密顿图的几种方法归纳总结
- linux设备驱动中的并发控制
- 1.2.线程基础_中断线程
- 腾讯2014实习生招聘一道附加题 DP和分治两种解法
- Android NDK编译常见错误及解决方案
- poj 2385 Apple Catching 简单DP
- Unsupported compiler 'com.apple.compilers.llvmgcc42' selected for architectu
- SQL JOIN方法整理
- BIEE 11g 11.1.1.7.1跨事实指标不使用full outer join问题解决办法
- C++ 指针基本用法总结
- 兼容Firefox和IE的禁用Tab键JS代码
- 解决Nginx+Tomcat下客户端https请求跳转成http的问题
- Effective Objective C------ 1不要忘本
- 那一天