首页 > 代码库 > poj 2385 Apple Catching
poj 2385 Apple Catching
Apple Catching
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11144 | Accepted: 5415 |
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.
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.
题意:一头牛在规定时间内在两棵苹果树间吃苹果,每一分钟其中一棵树都会掉一个苹果,牛起始位置在第一棵树,求在规定时间内最多能吃到的苹果数
思路:dp[i][j]//i分钟时且总共往返j次能吃到的苹果的最大数
j>0时:dp[i][j]=max{dp[i-1][j],dp[i-1][j-1]}+当前分钟能否吃到苹果;
max {牛没有移动,牛移动到了新树}+...
j=0时,dp[i][j]=dp[i-1][j];
AC代码:
#define _CRT_SECURE_NO_DEPRECATE#include<iostream>#include<algorithm>using namespace std;const int T_MAX = 1000;const int W_MAX = 30;int dp[T_MAX+1][W_MAX+1];//第i分钟往返j次能吃到的苹果的最大数int fall_app[T_MAX+1];int main() { int T, W; while (cin >> T >> W) { for (int i = 1;i <= T;i++)//分别对应着第i分钟在哪棵树掉苹果 scanf("%d",&fall_app[i]); for (int i = 0;i <= W;i++) { dp[0][i] = 0;//0分钟及前规定没吃到苹果 //cout << dp[0][i] << " "; } //cout << endl; for (int i = 1;i <= T;i++) { for (int j = 0;j <= W;j++) { if (j == 0)dp[i][j] = dp[i - 1][j];//没往返过时的情况 else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1]); if (j & 1) { if (fall_app[i] == 2)dp[i][j] += 1; } else if (fall_app[i] == 1)dp[i][j] += 1; //cout << dp[i][j]<<" "; } //cout << endl; } int max=0; for (int i = 0;i <= W;i++) if (max < dp[T][i])max = dp[T][i]; cout << max << endl; } return 0;}
poj 2385 Apple Catching
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。