POJ 2385 Apple catching DP

Source: Internet
Author: User

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


Serie A: There are two apples, and there will be an apple falling from a tree on the clock. You have to stand under this tree to be able to get the apples falling from this tree, you can walk back and forth between the two trees, and do not spend time, but do not walk more than w times, now tell you every minute from which tree will fall from the Apple, ask you how many can be followed, your starting position is under the first tree.

Idea: Use Dp[i][j][k] to denote the first minute. had gone back and forth for J times, and now under the K-Tree (k=0) under the first tree. K=1 said that under the second tree), the maximum number of apples that can be received, then suppose that I minute, is the first lesson the tree falls off the apple. So easy to think of Dp[i]][j][0]=max (dp[i-1][j][0]+1,dp[i-1][j-1][1]+1), because probably before the first tree, then do not need to move around, you can pick another apple, or before the second tree, Then it is necessary to move the talent to the first tree, and then another Apple; Dp[i][j][1]=dp[i-1][j][1], thought according to Greedy thought, it is impossible to know the first tree to fall apple. He also walked under the first tree under the second tree, so K=1 's situation was only introduced by Dp[i-1][j][1. So in the same vein, we can get the first minute, the second tree falls off the apple, and here is no longer described. If the hypothesis is not clear, look at the following code.

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 get the most apples without moving, so you have to find the maximum value in all cases.


#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.