POJ 2385 Apple Catching(簡單DP)

來源:互聯網
上載者:User

標籤:miss   second   for   end   str   pos   lin   out   details   

 

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 22112211

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. 題意:  奶牛愛吃蘋果是鮮為人知的事實。FJ在他的田地裡有兩棵蘋果樹(編號為1和2)。Bessie不會爬樹,所以她必須等著蘋果掉下來。然而,她必須在空中接住它們,因為蘋果落地時碰傷了(誰也不想吃碰傷了的蘋果)。Bessie吃得很快,吃的時間可以忽略不計。 每分鐘,兩棵蘋果樹中的一棵會掉落一個蘋果。Bessie做過很多練習,只要她站在一棵有蘋果落下的樹下,就能接住一個蘋果。Bessie可以很快地在兩棵樹之間行走,時間忽略不計。她可以隨時站在一棵樹下。此外,奶牛沒有得到大量的鍛煉,所以她不願意在樹間來回地來回行走(因此可能錯過了一些蘋果)。 蘋果下降時間T(1 < = T< = 1000)分鐘。Bessie最多隻願意來回走W(1 < = W< = 30)次。考慮哪棵樹每分鐘會掉落一個蘋果,求貝西能抓到的最大數量的蘋果。從樹1開始。 題解:   基礎的動態規劃。dp[i][j]表示第i分鐘最多轉換j次能得到蘋果的最大數量。轉換次數j為偶數時,肯定是在第1號樹下,為奇數時,在第2號樹下。
#include<iostream>#include<algorithm>using namespace std;int dp[3005][35],t[3005];int main(){    int n,w;    cin>>n>>w;    for(int i=1;i<=n;i++)        cin>>t[i],t[i]--;    for(int i=1;i<=n;i++)        for(int j=0;j<=w;j++)            if(j%2)            {                    dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+t[i];            }            else            {                if(j)//加判斷,防止j==0時,dp[i-1][j-1]非法訪問記憶體                    dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+!t[i];                else                    dp[i][j]=dp[i-1][j]+!t[i];            }    cout<<dp[n][w]<<endl;    return 0;}

 

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.