poj 2385 Apple Catching(dp)
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.
Source
设dp[i][j]表示找到第i个苹果时,走了j步时 苹果的最大值。
首先要初始化,见代码
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]);表示走或不走取最大值。然后判断是否能够dp[i][j]++。最后找出最大值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<cmath> 7 using namespace std; 8 #define W 36 9 #define N 1006 10 int dp[N][W]; 11 int n,w; 12 int a[N]; 13 int main() 14 { 15 while(scanf("%d%d",&n,&w)==2){ 16 //int sum=0; 17 for(int i=1;i<=n;i++){ 18 scanf("%d",&a[i]); 19 } 20 memset(dp,0,sizeof(dp)); 21 if(a[1]==1){ 22 dp[1][0]=1; 23 dp[1][1]=0; 24 } 25 if(a[1]==2){ 26 dp[1][0]=0; 27 dp[1][1]=1; 28 } 29 30 for(int i=2;i<=n;i++){ 31 for(int j=0;j<=w;j++){ 32 if(j==0){ 33 dp[i][j]=dp[i-1][j]+(j%2+1==a[i]); 34 continue; 35 } 36 dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]); 37 if(j%2+1==a[i]){ 38 dp[i][j]++; 39 } 40 } 41 } 42 int ans=dp[n][0]; 43 for(int i=1;i<=w;i++){ 44 ans=max(ans,dp[n][i]); 45 } 46 printf("%d\n",ans); 47 48 } 49 return 0; 50 }
还有一种方法:
设dp[i][j]表示找到第i个苹果时,最多走了j步 苹果的最大值
则可以由
前i-1分钟最多走j次
前i-1分钟最多走j-1次
这两个状态转移过来
注意,第二种的转移第j次可以选择走或者不走。因为是最多走j次
跟以前做过的一个树形DP神似
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<cmath> 7 using namespace std; 8 #define W 36 9 #define N 1006 10 int dp[N][W]; 11 int n,w; 12 int a[N]; 13 int main() 14 { 15 while(scanf("%d%d",&n,&w)==2){ 16 //int sum=0; 17 for(int i=1;i<=n;i++){ 18 scanf("%d",&a[i]); 19 } 20 memset(dp,0,sizeof(dp)); 21 if(a[1]==1) dp[1][0]=1; 22 dp[1][1]=1; 23 for(int i=2;i<=n;i++){ 24 for(int j=0;j<=w;j++){ 25 if(j==0){ 26 dp[i][j]=dp[i-1][j]+(j%2+1==a[i]); 27 continue; 28 } 29 30 dp[i][j]=max(dp[i][j],dp[i-1][j]+(j%2+1==a[i])); 31 dp[i][j]=max(dp[i][j],dp[i-1][j-1]+(j%2==a[i])); 32 dp[i][j]=max(dp[i][j],dp[i-1][j-1]+(j%2+1==a[i])); 33 } 34 } 35 printf("%d\n",dp[n][w]); 36 } 37 return 0; 38 }