NOJ1060 Apple two-dimensional DP

Source: Internet
Author: User

Title Description

Few people know that cows love apples. Farmer John has two apple trees on his farm (numbered 1 and 2), and each tree is covered with apples. Bessie could not pluck the apples from the tree, so she could only wait for the apples to fall from the trees. But as the apples fell to the ground, Bessie would have to catch the apples in midair (no one would like to eat the smashed apples). Bessie was quick to eat, and she was able to finish it in just a few seconds after receiving the apple. Every minute, one of the two apple trees will drop an apple. Bessie had been trained enough to catch the apple falling from the tree as long as she stood under the tree. At the same time, Bessie was able to move quickly between two trees (moving for far less than 1 minutes), so when the apples fell, she would stand under one of the two trees. In addition, cows are reluctant to keep going between two trees, so they miss some apples. The Apples drop one per minute, a total of T (1<=t<=1000) minutes, and Bessie is willing to move W (1<=w<=30) a maximum number of times. Now gives the number of trees that drop apples per minute, asking for the maximum number of apples that Bessie can catch. At first, Bessie was under tree 1th.

Input

The first line is 2 digits, T and K. The next T-line, one number per line, represents the moment the T-Apple falls from the No. 1th Apple tree or the 2nd apple tree.

Output

For each test point, the output line, one number, is the maximum number of apples the cow receives.


Input sample

7 2
2
1
1
2
2
1
1

Output sample

6 (Output note: Bessie does not move until it receives two apples dropped from the 1th tree, and then moves to the 2nd tree until it receives two apples from the 2nd tree, and finally moves to the 1th tree to catch the last two apples falling from the 1th tree. So Bessie caught 6 apples altogether. )


Thinking of solving problems

This is a two-dimensional DP we use DP[I][J] to express to the first I apple, changed the maximum number of J times .

Suppose we have now considered the situation of i-1.

So if I apple we move: have dp[i][j] = dp[i-1][j-1] + current position whether there is an apple (0 or 1)

If I apple we do not move: have dp[i][j] = Dp[i-1][j] + current position whether there is an apple (0or1)

(Current position: Changed J if J is odd then the cow must be in number 2nd if J is even then the cow must be in the number 1th tree)

Let's consider the initialization of DP. After doing a few DP problems I found that the DP problem is the most difficult place to set up a DP array and the most need to be careful of the place is the initialization of the DP array, this problem jumped into a trap ... Fortunately, we found it in time.

From the state transition equation we know to initialize dp[x][0] and dp[1][x]

Dp[x][0] That does not move always under the number 1th tree can receive the number of their own brain repair ...

Dp[1][x] That is, just consider the first Apple cow to move down below ... Own Brain Repair


Put the code below

#include <cstdio> #include <cstring> #include <algorithm>const int maxpingguo= 1010;const int Maxyidong = 35;using namespace Std;int dp[maxpingguo][maxyidong];int apple[maxpingguo][3];//apple[i][j]=    1 for the I-minute J Tree has apple such design apple array for the following DP create convenient int main () {int pingguo,yidong;    scanf ("%d%d", &pingguo,&yidong);        for (int i = 1; I <= Pingguo; i + +) {int t;        scanf ("%d", &t);    Apple[i][t] = 1;            }//DP initialize for (int i = 1; I <= yidong; i + +) {if (i%2) {if (apple[1][2]) dp[1][i] = 1;        else dp[1][i] = 0;            }else {if (apple[1][1]) dp[1][i] = 1;        else dp[1][i] = 0;                                            }} for (int i = 1; I <= Pingguo; i + +) {/* Here's a trap, at first I thought it had been initialized dp[1][1] so I went straight from i=2.                                            * But in fact, if the above initialization is not performed in yidong=0 case!!    */dp[i][0] = dp[i-1][0] +apple[i][1];   }//DP for (int j = 1; J <= Yidong; j + +) {     for (int i = 2; I <= Pingguo; i + +) {if (j%2) {dp[i][j] = max (dp[i-1][j],dp[i-1][j-1])            +APPLE[I][2];            }else {Dp[i][j] = max (dp[i-1][j],dp[i-1][j-1]) +apple[i][1];    }}} int _max =-1;    for (int i = 0; I <= yidong; i + +) {if (_max < dp[pingguo][i]) _max = Dp[pingguo][i];    } printf ("%d\n", _max); return 0;}





NOJ1060 Apple two-dimensional 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.