P2690 is connected to Apple and p2690 is connected to Apple

Source: Internet
Author: User

P2690 is connected to Apple and p2690 is connected to Apple
Background

USACO

Description

Few people know that cows love apple. There are two apple trees (numbers 1 and 2) on Farmer John's farm, each of which is covered with apples. Besie, the cow, cannot pick the apples on the tree, so she can only wait for the apples to fall from the tree. However, because an Apple will crash when it falls to the ground, Bessie must catch it in the air (no one loves it ). Bessie eats fast, and it takes only a few seconds to finish eating after receiving an apple. One of the two apple trees will drop one apple every minute. Bessie has been trained enough to stand under the tree to catch the fallen apple. At the same time, Bessie was able to quickly move between the two trees (moving for less than 1 minute), so when the apple fell, she must be standing under one of the two trees. In addition, the cows do not want to keep traveling between two trees, so they will miss some apples. One apple drops per minute. T (1 <=t <= 1000) minutes in total. Bessie is willing to move W (1 <=w <= 30) times at most. The number of the trees that drop the apple every minute is given, and the maximum number of apples that Bessie can catch is required. At the beginning, Bessie was under the No. 1 tree.

Input/Output Format

Input Format:

 

The number of 2 in the first row, t and k. In the next t row, each row has a number, which indicates that at the moment t apple fell from the No. 1 apple tree or the No. 2 apple tree.

 

Output Format:

 

For each test point, one row and one number are output, which is the maximum number of apples that cows receive.

 

Input and Output sample input sample #1:
7 22112211
Output sample #1:
6
Description

DP

 

I feel that my DP has grown. In the past, even the transfer equation was not listed. Now I can have three points ..

Magic, (I won't tell you the difficulty is popularity -)

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <queue> 6 using namespace std; 7 void read (int & n) 8 {9 char c = '+'; int x = 0; bool flag = 0; 10 while (c <'0' | c> '9') 11 {c = getchar (); if (c = '-') flag = 1 ;} 12 while (c> = '0' & c <= '9') 13 {x = x * 10 + (c-48); c = getchar ();} 14 flag = 1? N =-x: n = x; 15} 16 int n, m; 17 int a [10001]; 18 int dp [10001] [31]; 19 int main () 20 {21 read (n); read (m); 22 for (int I = 1; I <= n; I ++) 23 read (a [I]); 24 if (a [1] = 1) 25 dp [1] [0] = 1; 26 else dp [1] [1] = 1; 27 for (int I = 2; I <= n; I ++) 28 for (int j = 0; j <= m & j <= I; j ++) 29 if (a [I] = a [I-1]) // same do not move 30 dp [I] [j] = max (dp [I-1] [J-1], dp [I-1] [j]) + 1; 31 else // not the same 32 dp [I] [j] = max (dp [I-1] [J-1] + 1, dp [I-1] [j]); 33 int ans = 0; 34 for (int I = 1; I <= m; I ++) 35 ans = max (ans, dp [n] [I]); 36 printf ("% d", ans); 37 return 0;42. I don't know how to make a mistake ..

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <queue> 6 using namespace std; 7 void read (int & n) 8 {9 char c = '+'; int x = 0; bool flag = 0; 10 while (c <'0' | c> '9') 11 {c = getchar (); if (c = '-') flag = 1 ;} 12 while (c> = '0' & c <= '9') 13 {x = x * 10 + (c-48); c = getchar ();} 14 flag = 1? N =-x: n = x; 15} 16 int n, m; 17 int a [10001]; 18 int dp [10001] [31]; 19 int main () 20 {21 read (n); read (m); 22 for (int I = 1; I <= n; I ++) 23 read (a [I]); 24/* if (a [1] = 1) 25 dp [1] [0] = 1; 26 else dp [1] [1] = 1; */27 for (int I = 1; I <= n; I ++) 28 for (int j = 0; j <= m & j <= n; j ++) 29 {30 if (j = 0) 31 dp [I] [j] = dp [I-1] [j]; 32 else // same do not move 33 dp [I] [j] = max (dp [I-1] [J-1], dp [I-1] [j]); 34 if (a [I] = j % 2 + 1) 35 dp [I] [j] ++; 36} 37 38 // else // not the same 39 // dp [I] [j] = max (dp [I-1] [J-1] + 1, dp [I-1] [j]); 40 int ans = 0; 41 for (int I = 1; I <= m; I ++) 42 ans = max (ans, dp [n] [I]); 43 printf ("% d", ans); 44 return 0; 45}

 

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.