POJ 2385Apple Catching 递推
来源:互联网 发布:初中视频讲课软件 编辑:程序博客网 时间:2024/06/05 21:16
Apple Catching
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 11546 Accepted: 5565
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.
Source
USACO 2004 November
也是一个很简单的递归题,我这里设置了三个参量,其实第三个可以用第二个表示的,但是为了方便牺牲一点空间其实也没啥...
#include <iostream>#include <cstring>using namespace std;int main(){ int dp[1005][3][35],t,w,ap[1005],ma; while(cin>>t>>w) { memset(dp,-0x3f3f3f,sizeof(dp)); for(int i=1;i<=t;i++) cin>>ap[i]; if(ap[1]==1) { dp[1][1][0]=1; dp[1][2][1]=0; } else { dp[1][1][0]=0; dp[1][2][1]=1; } for(int i=2;i<=t;i++) { if(ap[i]==1) { for(int j=0;j<=w;j++) { dp[i][1][j]=max(dp[i][1][j],dp[i-1][1][j]+1); dp[i][2][j+1]=max(dp[i][2][j+1],dp[i-1][1][j]); dp[i][1][j+1]=max(dp[i][1][j+1],dp[i-1][2][j]+1); dp[i][2][j]=max(dp[i][2][j],dp[i-1][2][j]); } } else { for(int j=0;j<=w;j++) { dp[i][1][j]=max(dp[i][1][j],dp[i-1][1][j]); dp[i][1][j+1]=max(dp[i][1][j+1],dp[i-1][2][j]); dp[i][2][j]=max(dp[i][2][j],dp[i-1][2][j]+1); dp[i][2][j+1]=max(dp[i][2][j+1],dp[i-1][1][j]+1); } } } ma=0; for(int i=0;i<=w;i++) { if(dp[t][1][i]>ma) ma=dp[t][1][i]; if(dp[t][2][i]>ma) ma=dp[t][2][i]; } cout<<ma<<endl; } return 0;}
0 0
- POJ 2385 Apple Catching(递推)
- POJ 2385Apple Catching 递推
- POJ 2385 Apple Catching
- POJ-2385-Apple Catching
- poj 2385 Apple Catching
- POJ 2385 Apple Catching
- poj 2385 Apple Catching
- POJ 2385 Apple Catching
- Apple Catching.(POJ-2385)
- poj 2385 Apple Catching
- poj 2385 Apple Catching
- POJ 2385Apple Catching
- poj 2385 Apple Catching
- POJ 2385 Apple Catching
- POJ 2385 Apple Catching
- POJ 2385 Apple Catching
- POJ 2385 Apple Catching
- POJ 2385 Apple Catching
- 深度学习(DL)与卷积神经网络(CNN)学习笔记随笔-04-基于Python的LeNet之MLP
- 对p标签嵌套块级元素的思考
- TCP加速锐速SS(ServerSpeeder)破解版一键安装
- 【JAVA】 Android开发之JAVA代码中FloatMath.floor方法过时的最新替代方案。
- c#-全局键盘钩子
- POJ 2385Apple Catching 递推
- IOS7、IOS10设置CISCO ANYCONNECT VPN教程
- http://blog.sina.com.cn/s/blog_1525b95370102wg59.html
- 378. Kth Smallest Element in a Sorted Matrix
- C#-CMD
- HDU5933 (贪心)
- 第九周项目3-稀疏矩阵的三元组表示的实现及应用(1)
- JAVA基础:多态和抽象注意细节
- 深度学习(DL)与卷积神经网络(CNN)学习笔记随笔-02-基于Python的卷积运算