POJ 2385 Apple Catching

Source: Internet
Author: User

Apple Catching
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11144 Accepted: 5415

Description

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 Two. Test instructions: A cow eats apples between apple trees within a specified time, and every minute one of the trees will drop an apple, and the cow starts at the first tree, asking for the maximum number of apples to be eaten in the specified time: dp[i][j]//i minutes and a total of round-trip J-time to eat the largest amount of apples j>              0 O'Clock: dp[i][j]=max{dp[i-1][j],dp[i-1][j-1]}+ The current minute can eat apples; Max {Cow does not move, the cow moves to the new tree}+...j=0, dp[i][j]=dp[i-1][j]; AC Code:
#define_crt_secure_no_deprecate#include<iostream>#include<algorithm>using namespacestd;Const intT_max = +;Const intW_max = -;intdp[t_max+1][w_max+1];//The maximum number of apples that can be eaten by the I-minute round trip J Timeintfall_app[t_max+1];intMain () {intT, W;  while(Cin >> T >>W) { for(inti =1; I <= t;i++)//That corresponds to the first minute of the tree.scanf"%d",&Fall_app[i]);  for(inti =0; I <= w;i++) {dp[0][i] =0;//0 minutes and before the apple was not eaten//cout << dp[0][i] << "";        }        //cout << Endl;         for(inti =1; I <= t;i++) {             for(intj =0; J <= w;j++) {                if(J = =0) Dp[i][j] = dp[i-1][J];//no return to the outdated situation                ElseDP[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;        }        intmax=0;  for(inti =0; I <= w;i++)            if(Max < dp[t][i]) max =Dp[t][i]; cout<< Max <<Endl; }    return 0;}

POJ 2385 Apple Catching

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.