Apple Catching POJ - 2385(基础的动态规划算法)
題意:
給你兩個數字n和m;代表會有n個蘋果掉落,m次可以移動的機會;有兩棵樹,開始你站在樹1下面,一分鐘只能移動一次,下面的數值代表在哪一顆樹下會掉落蘋果;問你在可移動的范圍內,最多可以接到多少個蘋果?
題目:
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).基礎dp題,按照題意很容易看出有兩個變量,分鐘和移動機會,所以我們按照習慣寫出兩層for循環,簡單思考其中的關系,就可以得到dp定義,dp[i][j]dp[i][j]dp[i][j],表示在i時間內,用 jjj次轉移機會得到的最大蘋果數.
(2).因為開始的位置在第一棵樹下,那么我們就可以由移動的步數 jjj的奇偶性判斷現在在哪顆樹下;
(3).dp轉移如下,如果 j==0j==0j==0,則 dp[i][j]=dp[i?1][j]dp[i][j]=dp[i-1][j]dp[i][j]=dp[i?1][j],考慮j為0時,到第i分鐘,一步都沒有走動。
否則 dp[i][j]=max(dp[i?1][j],dp[i?1][j?1])dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])dp[i][j]=max(dp[i?1][j],dp[i?1][j?1]).就是當前從上一個狀態下,走還是不走獲得蘋果最多。
(4)如果現在所在的樹的編號和現在掉落的蘋果所在的位置相同,那么接到的蘋果的數量姐增加,由(2)這個我就放在最后判斷一下就行,也可以放在第三步,直接在推導式里面。
(5).最后在 dp[n][i]dp[n][i]dp[n][i]里找最大值就行了,(0<=i<=n).(0<=i<=n).(0<=i<=n).表示一共走i步時,n分鐘吃到蘋果最大值。
我今天真的閑的羅里吧嗦這么多,自戀的覺得只要跟著我的思路走,一定能會這道題,hhh
AC模板:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int M=1e3+10; int n,m,ans; int s[M],dp[M][35]; int main() {while(~scanf("%d%d",&n,&m)){ans=0;memset(dp,0,sizeof(dp));for(int i=1; i<=n; i++)scanf("%d",&s[i]);if(s[1]==1)dp[1][0]=1;elsedp[1][1]=1;for(int i=2; i<=n; i++)for(int j=0; j<=i&&j<=m; j++){if(j==0)//考慮j為0時,也就是說,到第i分鐘,一步都沒有走動。dp[i][j]=dp[i-1][j]+s[i]%2;else{dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]);//就是當前走還是不走獲得蘋果最多。if(j%2+1==s[i])//因為最開始站在樹1下面,可以用走多少步的奇偶來表示當前在那棵樹下。dp[i][j]++;}}for(int i=0; i<=m; i++)//表示一共走i步時,n分鐘吃到蘋果最大值。ans=max(ans,dp[n][i]);printf("%d\n",ans);}return 0; }備戰ccpc分站賽ing ,題目分析簡略,見諒,轉載請注明出處。。。。。
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Apple Catching POJ - 2385(基础的动态规划算法)的全部內容,希望文章能夠幫你解決所遇到的問題。