POJ 2385 Apple catching (simple DP)

Source: Internet
Author: User

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:    It is a little known fact that cows love to eat apples. FJ has two apple trees in his field (numbered 1 and 2). Bessie can't climb a tree, so she has to wait for the apple to fall. However, she had to catch them in the air because the apples were hurt when they landed (no one wanted to eat a bruised apple). Bessie eat fast, and the time to eat is negligible. Every minute, one of the two apple trees will drop an apple. Bessie did a lot of exercises, so long as she stood under a tree with apples falling, she could catch an apple. Bessie can quickly walk between two trees, and time is negligible. She can stand under a tree at any time. In addition, the cow did not get a lot of exercise, so she did not want to walk back and forth between the trees (so she may have missed some apples). Apple Fall Time T (1 < = t< = 1000) minutes. Bessie is only willing to go back and forth W (1 < = w< = 30) times. Consider which tree will drop an apple per minute and ask Bessie to catch the largest number of apples. Starting from tree 1. The following:Dynamic planning of the foundation. DP[I][J] indicates that the maximum number of apples can be obtained by converting J times for the first minute. Number of Conversions J is an even number, it must be under the 1th tree, for odd numbers, under the 2nd tree.   
#include <iostream>#include<algorithm>using namespacestd;intdp[3005][ *],t[3005];intMain () {intn,w; CIN>>n>>W;  for(intI=1; i<=n;i++) Cin>>t[i],t[i]--;  for(intI=1; i<=n;i++)         for(intj=0; j<=w;j++)            if(j%2) {Dp[i][j]=max (dp[i-1][j],dp[i-1][j-1])+T[i]; }            Else            {                if(j)//j==0, dp[i-1][j-1] unauthorized access to memoryDp[i][j]=max (dp[i-1][j],dp[i-1][j-1])+!T[i]; ElseDp[i][j]=dp[i-1][j]+!T[i]; } cout<<dp[n][w]<<Endl; return 0;}

POJ 2385 Apple catching (simple DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.