poj2385 Apple Catching(dp狀態轉移方程推導)

來源:互聯網
上載者:User

標籤:sdn   span   nbsp   namespace   article   lse   else   art   inf   

https://vjudge.net/problem/POJ-2385

猛刷簡單dp的第一天的第一題。

狀態:dp[i][j]表示第i秒移動j次所得的最大蘋果數。關鍵要想到移動j次,根據j的奇偶判斷人在哪裡。

想了挺久的,最後還是參考了一篇和自己思路最近的代碼52050207

我比他缺少的是特判dp[i][0]的狀態,後面的一切都以此為基礎的。

 1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<cstring> 5 #include<algorithm> 6 #include<cmath> 7 #include<set> 8 #define INF 0x3f3f3f3f 9 typedef long long ll;10 using namespace std;11 int a[1010], dp[1010][1010];12 int main()13 {14     memset(dp, 0, sizeof(dp));15     int n, m;16     cin >> n >> m;17     for(int i = 1; i <= n; i++){18         cin >> a[i];19     }20     for(int i = 1; i <= n; i++){21         dp[i][0] = dp[i-1][0];22         if(a[i] == 1){//蘋果在左邊 23             dp[i][0]++;24             for(int j = 1; j <= m; j++){25                 if(j&1)//人在右邊 26                     dp[i][j] = max(dp[i-1][j], dp[i-1][j-1]);27                 else//人在左邊 28                     dp[i][j] = max(dp[i-1][j], dp[i-1][j-1])+1; 29             }30         }31         else{//蘋果在右邊 32             for(int j = 1; j <= m; j++){33                 if(j&1)//人在右邊 34                     dp[i][j] = max(dp[i-1][j], dp[i-1][j-1])+1; 35                 else//人在左邊36                     dp[i][j] = max(dp[i-1][j], dp[i-1][j-1]); 37             }38         }39     }40     int maxm = -INF;41     for(int i = 0; i <= m; i++){42         maxm = max(maxm, dp[n][i]);43     } 44     cout << maxm << endl;45     return 0;46 } 

 

poj2385 Apple 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.