POJ 2385 Apple catching DP

Source: Internet
Author: User

http://poj.org/problem?id=2385


Test instructions: There are two apple trees, every minute a tree will fall off an apple, you have to stand under the tree to receive the apple falling from the tree, you can walk back and forth between the two trees, and do not spend time, but the number of trips not more than w times, now tell you every minute from which tree will fall from the Apple, Ask you how many you can go, your position is actually under the first tree.

Thought: With Dp[i][j][k] for the first minute, has gone back and Forth J Times, and now under the K tree (K=0 said under the first tree, K=1 said under the second tree), can receive the largest number of apples, then if I minute, is the lesson tree falling apples, so easy to think Dp[i]] [j] [0]=max (dp[i-1][j][0]+1,dp[i-1][j-1][1]+1), because it may be under the first tree, then do not need to move around, you can pick another apple, or before the second tree, then you need to move to the first tree, then another apple ; dp[i][j][1]=dp[i-1][j][1], thought according to the greedy thought, cannot know the first tree to fall the apple, also from the first tree under the second tree, so the k=1 situation only by dp[i-1][j][1] launches. So in the same vein, we can get the first minute, the second tree falls off the apple, and here it is no longer described. If you don't understand, just look at the code below. It is important to note that the final answer is not necessarily the maximum number of moves, such as all the first apple tree in the apple, so you can not move to receive the most apples, so you must be in all cases to find the maximum value.


#include <cstdio> #include <cstring> #include <iostream>using namespace Std;int dp[1010][35][2];int A    [1010];int Main () {int n,m;        while (cin>>n>>m) {memset (dp,0,sizeof (DP));        for (int i=1;i<=n;i++) cin>>a[i];                    for (int i=1;i<=n;i++) {for (int j=0;j<=m;j++) if (j<=i) {if (a[i]==1) {                    if (j) Dp[i][j][0]=max (dp[i-1][j][0]+1,dp[i-1][j-1][1]+1);                    else dp[i][j][0]=dp[i-1][j][0]+1;                DP[I][J][1]=DP[I-1][J][1];                    } else{dp[i][j][0]=dp[i-1][j][0];                    if (j) Dp[i][j][1]=max (dp[i-1][j][1]+1,dp[i-1][j-1][0]+1);                else dp[i][j][1]=dp[i-1][j][1]+1;        }}} int ans=0;            for (int i=0;i<=m;i++) {Ans=max (ans,dp[n][i][0]);        Ans=max (ans,dp[n][i][1]); } cout<<ans<<endl; } return 0;}





POJ 2385 Apple catching DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.