poj_2385 Apple Catching (DP)

Source: Internet
Author: User

Please point me in the question.

Exercises

The topic conforms to the current optimal state from multiple optimal states, so it is a DP right, so the key is the definition of DP array and the recursive equation.

At the beginning, according to their own ideas, will be a succession of the same fruit drop merge,

DP Array Dp[i][j] Definition: The optimal result of reaching J position after I was transferred.

Recursion relationship: Dp[i][j] = max (Dp[i][j],dp[i-1][k]+get (k,j)) (k:i-1~j) Get function Gets the number of fruit corresponding to the current Apple tree between K,j.

The original intention for the fruit will be simple after the merger, three-layer for the loop, but the trouble, the data is weak, or too. After AC reference online ideas, found will be much simpler.

DP Array Dp[i][j] Definition: The optimal result of a J-time prior to I position (just the opposite ...). )

Recursive relationship: dp[i][j] = max (Dp[i-1][j],dp[i-1][j-1]). Well understood, there are two possible ways to reach position I, one is to stay and not move, the other is just turn around and take the optimal solution. If it's just under the corresponding Apple tree, + +. A lot easier, 0ms is over.

To summarize the definition of DP is to see how the state changes, the current state from which states come, find the appropriate DP definition.

Reference Blog

Code implementation:

(Own code)

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #define LL Long     longusing namespace Std;const int MAX = 1010;int t,w;int res;int num[max];int dp[35][max];int get (int x,int y); int main () {    scanf ("%d%d", &t,&w);    res = 0;    int Last,tag;    int sum = 0;    memset (num,0,sizeof (sum));        for (int i = 0; i < T; i++) {scanf ("%d", &tag);        if (i = = 0) {num[sum]++;            } else{if (last = = tag) {num[sum]++;                } else{sum++;            num[sum]++;    }} last = tag;    } memset (Dp,0,sizeof (DP));    for (int i = 0; I <= sum; i++) {Dp[0][i] = get ( -1,i); } for (int i = 1, i <= W; i++) {for (int j = i; j <= Sum; j + +) {for (int k = i-1; k <= j-1 ;            k++) {Dp[i][j] = max (Dp[i][j],dp[i-1][k]+get (k,j)); }} res = max (dp[i][sum],res);    } printf ("%d\n", res); return 0;}    The uniform definition starts at the next position int get (int x,int y) {int tmp1 = 0;    int TMP2 = 0;        for (int i = x+1; I <= y; i++) {if ((i-x)%2 = = 0) {TMP1 + = Num[i];        } else{TMP2 + = Num[i]; }} return Max (TMP1,TMP2);}

(Another way to change the idea of a code)

 #include <iostream> #include <cstdio> #include <cstdlib> #include < Cstring> #define LL Long longusing namespace Std;const int MAX = 1010;int t,w;int res;int num[max];int dp[max][35];int m    Ain () {scanf ("%d%d", &t,&w);    res = 0;    Memset (Dp,0,sizeof (DP));    memset (num,0,sizeof (num));    for (int i = 1; I <= T; i++) {scanf ("%d", &num[i]);        } if (num[1] = = 1) {dp[1][0] = 1;    DP[1][1] = 0;        } else{Dp[1][1] = 1;    Dp[1][0] = 0; } for (int i = 2; I <= T, i++) {for (int j = 0; J <= I; j + +) {if (j = = 0) {DP I                [j] = dp[i-1][j]+num[i]%2;            Continue            } Dp[i][j] = max (dp[i-1][j],dp[i-1][j-1]);            if (num[i] = = j%2+1) {dp[i][j]++;    }}} for (int i = 0; I <= W; i++) {res = max (res,dp[t][i]);    } printf ("%d\n", res); return 0;} 


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.