POJ 2385 Apple Catching

Source: Internet
Author: User

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.

Source

Usaco 2004 November Test instructions not difficult to understand, two trees, every minute or from a number or second fall apples, can shuttle two trees between, of course, can not receive two trees at the same time, and the number of times limited shuttle, initially under a tree, asked how many apples can be received, Every minute can have a different choice, actually need to find a shuttle of the best route, so we need to record, a certain time, shuttle several times the state, the first minute, shuttle J times to get the number of apples dp[i][j], can only be undertaken in two States, or he has been in the last minute shuttle J time, Either the last minute shuttle j-1 times, and then determine the first minute shuttle to the location of the Apple, it is very good to judge, if J is odd, then under the number second tree, if J is even, is in a tree, if J and the tree number and are odd, so two modulo can be. Code:
#include <iostream>#include<cstdio>using namespacestd;intdp[1001][ to];///number of apples in the first few steps of the first few minutesintapple[1001];intMain () {intN,m,ans =0; scanf ("%d%d",&n,&m);  for(inti =0; i < N;i + +) {scanf ("%d",&Apple[i]); } dp[1][apple[0] -1] =1;///First minute     for(inti =2; I <= N;i + +) {dp[i][0] = Dp[i-1][0] + apple[i-1] %2;///number of apples in 0 steps of the first minute         for(intj =1; J <= M;j + +) {Dp[i][j]= Max (Dp[i-1][j],dp[i-1][j-1]); DP[I][J]+ = (Apple[i-1] + j)%2;///whether to receive Apple        }    }     for(inti =0; I <= M;i + +) {ans=Max (ans,dp[n][i]); } printf ("%d", ans);}

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.