POJ 2385 Apple Catching (DP)

Source: Internet
Author: User
Tags printf

Description
It is a little known fact the cows love apples. Farmer John has two apple trees (which are, conveniently 1 and 2) in his field, each of the full numbered. Bessie cannot reach of the apples when they are in the tree and so she must wait for them to fall. However, she must catch them in the air since the apples bruise while they hit the ground (and no one wants to eat bruised Apples). Bessie is a quick eater, so an apple she does catch are eaten in just a few seconds.

Each minute, one of the two apple trees drops a apple. Bessie, having a very practice, can catch an apple if it 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 Time. Moreover, cows do don't get a lot of exercise, so she isn't willing to walk back and forth between the trees endlessly (and Thus misses some apples).

Apples Fall (one minute) for T (1 <= t <= 1,000) minutes. Bessie is willing to walk back and forth in most W (1 <= w <=) times. Given which tree would drop an apple each minute determine the maximum number of apples which can catch. Bessie starts at 1.

Input
* Line 1:two spaces 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 the more than W times.

Sample Input

9 |
2
1
2
1
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 into a row from tree 2, then two to a row from Tree 1. Bessie is willing to walk from one of the other twice.

OUTPUT DETAILS:

Bessie can catch six apples by staying under tree 1 until the ' the ' I two have dropped, then moving to Tree 2 for the next T Wo, then returning 1 for the final two.

Source
Usaco November

All the ideas are written in the comments:

/*16ms,388kb*/
     
#include <cstdio>  
#include <algorithm>  
using namespace std;  
     
int Dp[31];///dp[i] Represents the maximum number of apples that have been received (eaten) after I moved  
///even is the first tree, odd as the second tree  
///so open a one-dimensional array on the line  
     
int main ()  
{  
    int i, N, W, tree;  
    scanf ("%d%d", &n, &w);  
    while (n--)  
    {  
        scanf ("%d", &tree);  
        if (tree = = 1)  
        {for  
            (i = 2; I <= w. i = 2)///The first trees  
                dp[i] = max (dp[i), dp[i-1]) + 1;///Either change position or change location 
  ++dp[0];///can receive without moving any more  
        }  
        else {for  
            (i = 1; I <= w; i = 2)///The second tree  
                dp[i] = max (dp[i), DP[I-1]) + 1;  
        }  
        for (i = 0; I <= w; ++i)  
        //printf ("%d\t", Dp[i));  
        Putchar (ten);  
        Putchar (ten);  
    }  
    printf ("%d\n", Max (dp[w-1), dp[w]);  
    return 0;  
}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.