1. 程式人生 > >poj 2385【動態規劃】

poj 2385【動態規劃】

ostream bmi 。。 set row include urn get 定義

poj 2385

Apple Catching
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14007 Accepted: 6838

Description

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.

Input

* 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.

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 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,可在兩棵樹間移動的最大次數W,初始時站在樹1下面。求能接到蘋果數的最大值。 題解:定義狀態dp[i][j]表示在i時刻移動了j次時能得到的最大蘋果數。狀態的含義很重要!則dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+(移動j次後對應的樹正好落蘋果?1:0)   方程想出來了,但是細節,邊界處理,循環上還不會。。。顯然移動次數為0時狀態只能由上一個時間不移動的狀態轉移過來,即dp[i][0]=dp[i-1][0]+app[i]%2(初始時在樹1下),而且有在第一時刻時若樹1落蘋果則dp[1][0]=1,否則dp[1][1]=1。由於是最多能移動W次並不是一定要移動W次,所以最後在0~W次結果中取最大值。
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 int dp[1050][50];
 8 int app[1050];
 9 
10 int main()
11 {
12     int T,W;
13     scanf("%d%d",&T,&W);
14     memset(dp,0,sizeof(dp));
15     for(int i=1;i<=T;i++){
16         scanf("%d",&app[i]);
17     }
18     if(app[1]==1) dp[1][0]=1;
19     else dp[1][1]=1;
20     for(int i=2;i<=T;i++){
21         dp[i][0]=dp[i-1][0]+app[i]%2;
22         for(int j=1;j<=W;j++){
23             dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]);
24             if(j%2+1==app[i]) dp[i][j]++;
25         }
26     }
27     int ans=0;
28     for(int i=0;i<=W;i++)
29         ans=max(ans,dp[T][i]);
30     printf("%d\n",ans);
31     return 0;
32 }

poj 2385【動態規劃】