P2690 Pick up apples

Source: Internet
Author: User
Tags cmath

Topic background

Usaco

Title Description

Few people know that cows love apples. Farmer John has two apple trees on his farm (numbered 1 and 2), and each tree is covered with apples. Bessie could not pluck the apples from the tree, so she could only wait for the apples to fall from the trees. But as the apples fell to the ground, Bessie would have to catch the apples in midair (no one would like to eat the smashed apples). Bessie was quick to eat, and she was able to finish it in just a few seconds after receiving the apple. Every minute, one of the two apple trees will drop an apple. Bessie had been trained enough to catch the apple falling from the tree as long as she stood under the tree. At the same time, Bessie was able to move quickly between two trees (moving for far less than 1 minutes), so when the apples fell, she would stand under one of the two trees. In addition, cows are reluctant to keep going between two trees, so they miss some apples. The Apples drop one per minute, a total of T (1<=t<=1000) minutes, and Bessie is willing to move W (1<=w<=30) a maximum number of times. Now gives the number of trees that drop apples per minute, asking for the maximum number of apples that Bessie can catch. At first, Bessie was under tree 1th.

Input/output format

Input format:

The first line is 2 digits, T and K. The next T-line, one number per line, represents the moment the T-Apple falls from the No. 1th Apple tree or the 2nd apple tree.

Output format:

For each test point, the output line, one number, is the maximum number of apples the cow receives.

Input and Output Sample input example # #:
7 22112211
Sample # # of output:
6
Description

Dp

Feel their DP has the director into the past even the transfer equation will not be listed, now incredibly can over three points.

Magic,(will not tell you that difficulty is universal-- )

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <queue>6 using namespacestd;7 voidReadint&N)8 {9     CharC='+';intx=0;BOOLflag=0;Ten      while(c<'0'|| C>'9') One{C=getchar ();if(c=='-') flag=1;} A      while(c>='0'&&c<='9') -{x=x*Ten+ (C- -); c=GetChar ();} -flag==1? n=-x:n=x; the } - intn,m; - inta[10001]; - intdp[10001][ to]; + intMain () - { + read (n); read (m); A      for(intI=1; i<=n;i++) at read (a[i]); -     if(a[1]==1) -dp[1][0]=1; -     Elsedp[1][1]=1; -      for(intI=2; i<=n;i++) -          for(intj=0; j<=m&&j<=i;j++) in             if(a[i]==a[i-1])//same without moving -Dp[i][j]=max (dp[i-1][j-1],dp[i-1][J]) +1; to             Else //not the same +Dp[i][j]=max (dp[i-1][j-1]+1, dp[i-1][j]); -     intans=0; the      for(intI=1; i<=m;i++)     *ans=Max (ans,dp[n][i]); $printf"%d", ans);Panax Notoginseng     return 0;
42, although do not know how wrong.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <queue>6 using namespacestd;7 voidReadint&N)8 {9 CharC='+';intx=0;BOOLflag=0;Ten while(c<'0'|| C>'9') One{C=getchar ();if(c=='-') flag=1;} A while(c>='0'&&c<='9') -{x=x*Ten+ (C- -); c=GetChar ();} -flag==1? n=-x:n=x; the } - intn,m; - inta[10001]; - intdp[10001][ to]; + intMain () - { + read (n); read (m); A for(intI=1; i<=n;i++) at read (a[i]); - /*if (a[1]==1) - dp[1][0]=1; - else dp[1][1]=1;*/ - for(intI=1; i<=n;i++) - for(intj=0; j<=m&&j<=n;j++) in { - if(j==0) todp[i][j]=dp[i-1][j]; + Else //same without moving -Dp[i][j]=max (dp[i-1][j-1],dp[i-1][j]); the if(a[i]==j%2+1) *dp[i][j]++; $ }Panax Notoginseng - //Else//not the same the //Dp[i][j]=max (Dp[i-1][j-1]+1,dp[i-1][j]); + intans=0; A for(intI=1; i<=m;i++) theans=Max (ans,dp[n][i]); +printf"%d", ans); - return 0; $}

P2690 Pick up apples

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.