It is a little known fact that cows love apples. Farmer John has two apple trees (which are conveniently numbered 1 and 2) in his field, each full of apples. Bessie cannot reach the apples when they are 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 hit the ground (and no one wants to eat bruised apples). Bessie is a quick eater, so an apple she does catch is eaten in just a few seconds.
Each minute, one of the two apple trees drops an apple. Bessie, having much practice, can catch an apple if she is 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 only one tree at any time. Moreover, cows do not get a lot of exercise, so she is not willing to walk back and forth between the trees endlessly (and thus misses some apples).
Apples fall (one each minute) for T (1 <= T <= 1,000) minutes. Bessie is willing to walk back and forth at most W (1 <= W <= 30) times. Given which tree will drop an apple each minute, determine the maximum number of apples which Bessie can catch. Bessie starts at tree 1.
Inputios
* Line 1: Two space separated integers: T and W
* Lines 2..T+1: 1 or 2: the tree that will drop an apple each minute.
Outputapp
* Line 1: The maximum number of apples Bessie can catch without walking more than W times.
Sample Inputless
7 2
2
1
1
2
2
1
1
Sample Outputui
6
Hintspa
INPUT DETAILS:
Seven apples fall - one from tree 2, then two in a row from tree 1, then two in a row from tree 2, then two 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 two have dropped, then moving to tree 2 for the next two, then returning back to tree 1 for the final two.
百度翻譯:牛愛蘋果是不爲人知的事實。農夫約翰在他的田裏種了兩棵蘋果樹(很方便地編號爲1和2),每棵樹都長滿了蘋果。貝西夠不到樹上的蘋果,因此她必須等它們掉下來。然而,她必須在空中抓住它們,由於當它們落地時蘋果會碰傷(沒有人願意吃碰傷的蘋果)。貝西吃得很快,因此她抓到的蘋果只需幾秒鐘就能夠吃了。每分鐘,兩棵蘋果樹中的一棵會掉一個蘋果。貝西常常練習,若是她站在一棵樹下,能夠摘到一個蘋果。雖然貝西能夠在兩棵樹之間快速行走(不到一分鐘),但她在任什麼時候候都只能站在一棵樹下。此外,奶牛沒有獲得大量的鍛鍊,因此她不肯意在樹之間來回走動(所以錯過了一些蘋果)。蘋果落下(每分鐘一個)持續t(1<=t<=1000)分鐘。貝西最多願意來回走W(1<=W<=30)次。考慮到哪棵樹每分鐘會掉一個蘋果,肯定貝西能接住的蘋果的最大數量。貝西從一號樹開始。
思路:dp[i][j] 表示第i分鐘移動第j次所能獲得的最大蘋果數。
狀態轉移方程:dp[i][j] = max(dp[i-1][j], dp[i-1][j-1])。表示第i分鐘能獲得的蘋果數量,等於在第i-1分鐘時,在樹1和樹2下獲得蘋果的最大值。j爲偶數則在樹1下面,奇數則在樹2下面。
而後判斷當前是否在第i分鐘掉蘋果的那顆樹下,是的話,dp[i][j]++。
1 #include <cstdio>
2 #include <fstream>
3 #include <algorithm>
4 #include <cmath>
5 #include <deque>
6 #include <vector>
7 #include <queue>
8 #include <string>
9 #include <cstring>
10 #include <map>
11 #include <stack>
12 #include <set>
13 #include <sstream>
14 #include <iostream>
15 #define mod 1000000007
16 #define ll long long
17 using namespace std;
18
19 int m,n;
20 int dp[1005][35];
21 int num[1005];
22 int main()
23 {
24 while(scanf("%d %d",&n,&m)!=EOF)
25 {
26 for(int i=1;i<=n;i++)
27 {
28 scanf("%d",&num[i]);
29 }
30 if(num[1]==1)
31 {
32 dp[1][0]=1;
33 dp[1][1]=0;
34 }
35 else
36 {
37 dp[1][0]=0;
38 dp[1][1]=1;
39 }
40 for(int i=2;i<=n;i++)
41 {
42 for(int j=0;j<=m;j++)
43 {
44 if(j==0)
45 {
46 dp[i][j]=dp[i-1][j]+num[i]%2;
47 }
48 else
49 {
50 dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]);
51 if(j%2+1==num[i])
52 {
53 dp[i][j]++;
54 }
55 }
56 }
57 }
58 int ans=0;
59 for(int i=0;i<=m;i++)
60 {
61 ans=max(ans,dp[n][i]);
62 }
63 printf("%d\n",ans);
64 }
65 }