POJ-2385 Apple Catching---DP

來源:互聯網
上載者:User

標籤:第一個   turn   思路   次數   pac   href   ons   const   col   

題目連結:

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

題目大意:

兩顆蘋果樹每一分會有樹落下蘋果,有人去接,但是來回兩個樹之間的次數是一定的,所以求出在最大次數時最多能接到多少蘋果。

思路:

dp[i][j]表示在時間i,已經來回了j次時的最大蘋果數目。

初始化dp[1][0]和dp[1][1]得根據第一個蘋果是哪棵樹落下的來進行初始化

轉移方程:dp[i][j] = max(dp[i-1][j], dp[i-1][j-1]),並且如果在這個狀態下,如果有蘋果下落,得自加一

根據j的奇偶來判斷在哪棵樹下面,j為奇數在tree-2下面,j為偶數在tree-1下面

 1 #include<iostream> 2 #include<vector> 3 #include<queue> 4 #include<algorithm> 5 #include<cstring> 6 #include<cstdio> 7 #include<set> 8 #include<map> 9 #include<cmath>10 #include<sstream>11 using namespace std;12 typedef pair<int, int> Pair;13 typedef long long ll;14 const int INF = 0x3f3f3f3f;15 int T, n, m, minans;16 const int maxn = 1e3 + 10;17 const int mod = 1e9;18 int a[maxn];19 int dp[maxn][40];//dp[i][j]表示前i分鐘轉移j次的最大蘋果數目20 int main()21 {22     cin >> n >> m;23     for(int i = 1; i <= n; i++)cin >> a[i], a[i] %= 2;24     if(a[1] == 1)//初始狀態25     {26         dp[1][0] = 1;27         dp[1][1] = 0;28     }29     else30     {31         dp[1][0] = 0;32         dp[1][1] = 1;33     }34     for(int i = 2; i <= n; i++)35     {36         for(int j = 0; j <= m; j++)37         {38             if(j == 0)39             //一次都沒有轉移,一直在tree1,如果此時a[i]為1,tree1掉蘋果,可以加上,反之加上的也是040                 dp[i][j] = dp[i - 1][j] + a[i];41             else42             {43                 dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1]);44 45                 //此時轉移j次46                 //j為奇數,在2下,若此時有蘋果,則加一47                 if((j % 2 == 1) && (a[i] == 0))48                     dp[i][j]++;49                 //j為偶數,在1下,若此時有蘋果,則加一50                 if((j % 2 == 0) && (a[i] == 1))51                     dp[i][j]++;52             }53         }54     }55     int ans = 0;56     for(int i = 0; i <= m; i++)ans = max(ans, dp[n][i]);57     cout<<ans<<endl;58     return 0;59 }

 

POJ-2385 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.