poj 2385Apple Catching(簡單dp)

來源:互聯網
上載者:User

標籤:style   blog   color   os   io   for   div   log   amp   

 1 /* 2     題意: 有兩棵蘋果樹,每一棵蘋果樹每一秒間隔的掉落下來一個蘋果,一個人在樹下接住蘋果,不讓蘋果掉落! 3     人在兩棵樹之間的移動是很快的!但是這個人移動的次數是有限制的,問最多可以接住多少個蘋果! 4      5     思路:dp[i][j]表示的是前 i個蘋果掉落之後, 移動次數是j的情況下的最多接住的蘋果的個數! 6      7     那麼dp[i][j]=max(dp[i-1][j], dp[i][j-1]) + a[i]==j%2+1 ? 1 : 0; 8      9     a[i]==j%2+1 表明第j次移動恰好移動到 第 a[i]棵蘋果樹下,此時這棵蘋果樹這號掉落下了蘋果,正好接住! 10 */11 #include<iostream>12 #include<cstring>13 #include<cstdio>14 #include<algorithm>15 #define M 100516 using namespace std;17 18 int dp[M][35];19 20 int n, m;21 int a[M];22 23 int main(){24    scanf("%d%d", &n, &m);25    for(int i=1; i<=n; ++i)26       scanf("%d", &a[i]);27    if(a[1]==1) dp[1][0]+=1;28    for(int i=2; i<=n; ++i){29        dp[i][0]=dp[i-1][0];30        if(a[i]==1)31           dp[i][0]+=1;32    }33       34    for(int j=1; j<=m; ++j)35       for(int i=j; i<=n; ++i){36              dp[i][j]=max(dp[i][j-1], dp[i-1][j]);37            int cc=j%2+1;38            if(a[i]==cc)39               dp[i][j]+=1;40       } 41    printf("%d\n", dp[n][m]);42    return 0;43 } 

 

poj 2385Apple Catching(簡單dp)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.