It is a little known fact, cows love apples. Farmer John has the apple trees (which is conveniently numbered 1 and 2) in the His field, each of the apples. Bessie cannot reach the apples when they is 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 hits the ground (and no one wants to eat bruised Apples). Bessie is a-a quick eater, so an apple she does catch was eaten in just a few seconds.
Each minute, one of the trees drops Apple. Bessie, has much practice, can catch an apple if she's standing under a tree from which one falls. While Bessie can walk between the both trees quickly (in much less than a minute), she can stand under only one tree at any Time. Moreover, cows do don't get a lot of exercise, so she's not willing to walk back and forth between the trees endlessly (and Thus misses some apples).
Apples Fall (one minute) for T (1 <= T <=) minutes. Bessie is willing to walk back and forth at most W (1 <= w <=) times. Given which tree would 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 would 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 both in a row from tree 1, then both in a row from tree 2, then both 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 has dropped, then moving to Tree 2 for the next T Wo, then returning back to Tree 1 for the final. Test instructions: There are two Trees 1, 2, every minute one of the trees will drop an apple and ask how many apples you can get in T minutes. Note: Start at 1, move up to K times, and stay in front of a tree for any length of time. The obvious state representation, dp[I [j] [K], indicates that I minute is at J position, using the maximum value obtained by K-movement. In fact, the second dimension can not open, because the starting position in 1, so moved K times, the current position is k%2+1.
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5 using namespacestd;6 7 intt,k;8 intmp[1005],dp[1005][ +];9 Ten voidSolve () One{intans=0; A for(intI=1; i<=t;i++){ - for(intj=0; j<=k;j++){ - if(j==0) dp[i][j]=dp[i-1][j]; the ElseDp[i][j]=max (dp[i-1][j],dp[i-1][j-1]); - if(j%2+1==mp[i]) dp[i][j]+=1; -ans=Max (ans,dp[i][j]); - } + } -cout<<ans<<Endl; + } A at intMain () -{cin>>t>>K; - for(intI=1; i<=t;i++) scanf ("%d",&mp[i]); -Memset (DP,0,sizeof(DP)); - - solve (); in}
Apple Catching POJ-2385