A-apple catching (POJ 2385)

Source: Internet
Author: User

Apple Catching
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8759 Accepted: 4264

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 Novembersimple DP. the game has not been changed, the main feeling is that the sample has been, it should be a small problem, not actually, the example of the pit AH. In fact, the first idea is correct, but did not think clearly, began to knock, knocked out after the feeling of a small problem, has been put that look, to the end did not wake up. Compared to after a period of time to do, unexpectedly still made the same mistake, I really good memory ah (usually do not feel thick to AH). But this time has finally changed.
#include <cstdio>#include<iostream>#include<sstream>#include<cmath>#include<cstring>#include<cstdlib>#include<string>#include<vector>#include<map>#include<Set>#include<queue>#include<stack>#include<algorithm>using namespacestd;#definell Long Long#define_cle (M, a) memset (M, A, sizeof (m))#defineRepu (I, A, b) for (int i = A; I < b; i++)#defineMAXN 1005intd[maxn][ *][2];intTREE[MAXN];intMain () {intT, W; memset (d,0,sizeof(d)); scanf ("%d%d", &t, &W);  for(inti =1; I <= t; i++) {scanf ("%d", &Tree[i]); Tree[i]--; }     for(inti =1; I <= t; i++) {d[i][0][tree[i]] = d[i-1][0][tree[i]] +1, d[i][0][!tree[i]] = d[i-1][0][!Tree[i]];  for(intj =1; J <= W; J + +) D[i][j][tree[i]]= Max (D[i-1][j-1][!tree[i]], d[i-1][j][tree[i]]) +1, d[i][j][!tree[i]] = d[i-1][j][!Tree[i]]; }    intMAXN =-1;  for(inti =0; I <= W; i++) MAXN = max (MAXN, Max (d[t][i][0], d[t][i][1])); printf ("%d", MAXN); return 0;}
View Code

A-apple catching (POJ 2385)

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.