POJ 2385 Apple catching connect Apple DP

Source: Internet
Author: User

Title Link: POJ 2385 Apple catching

Apple Catching
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7858 Accepted: 3846

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 2
2
1
1
2
2
1
1

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

Two trees, at a certain moment will fall Apple, a cow initially standing under the first tree, it can be moved back and forth under two trees, but the number of steps is limited. Ask how many apples it can receive at most.

Analysis:

In a moment the state of the cow is, it may be in the last moment it is also under this tree, or it is moved from another tree. In this way, we use dp[i][j] to denote the maximum number of apples that have been received by J step at the time of the first I. Regardless of whether the current moment has been received by Apple, dp[i][j] = max (Dp[i-1][j], dp[i-1][j-1]), this describes the two states I said earlier.

Look at the current second whether or not to receive an apple. Because the initial under the first tree, so if J is odd, that is, moving odd steps, then it should be under the first tree, at this time if the first tree will fall off an apple, then Dp[i][j] will add 1. Similarly, if you move even steps and the second tree falls off an apple, Dp[i][j] also adds 1.

Code:

 #include <iostream> #include <cstring> #include <cstdio> using namespace std;
const int max_t = 1010;
const int MAX_W = 33;
int T, W, a[max_t];
int Dp[max_t][max_w];
    void Solve () {for (int i = 0; I <= W; i++) dp[0][i] = 0;
        for (int i = 1; I <= T; i++) {dp[i][0] = Dp[i-1][0]+2-a[i];
            for (int j = 1; J <= W && J <= I; j + +) {Dp[i][j] = max (Dp[i-1][j], dp[i-1][j-1]);
            if (j% 2) dp[i][j] + = a[i]-1;
        else Dp[i][j] + = 2-a[i];
    }} int cnt = 0;
    for (int i = 0; I <= W; i++) if (Dp[t][i] > cnt) cnt = dp[t][i];
printf ("%d\n", CNT); } int main () {while (~scanf ("%d%d", &t, &w)) {for (int i = 1; I <= T; i++) scanf ("%d",
        &a[i]);
    Solve ();
} return 0; }

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.