<span id="Label3"></p>Apple Catching <table align="center"> <tbody> <tr> <td>time <strong>Limit:</strong> <span class="Apple-converted-space">1000MS</span></td> <td width="10"></td> <td><strong>Memory Limit:</strong> <span class="Apple-converted-space">65536K</span></td> </tr> <tr> <td><strong>Total Submissions:</strong> <span class="Apple-converted-space">9831</span></td> <td width="10"></td> <td><strong>Accepted:</strong> <span class="Apple-converted-space">4779</span></td> </tr> </tbody> </table><p class="pst"><p class="pst">Description</p></p>It is a little known fact, cows love Apples. Farmer John has the apple trees (which is conveniently numbered 1 and 2) in the his field, each of the Apples. Bessie cannot reach the apples when they is 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 hits the ground (and no one wants to eat bruised Apples). Bessie is a-a quick eater, so an apple she does catch was eaten in just a few seconds.<span class="Apple-converted-space"><span class="Apple-converted-space"><br>Each minute, one of the trees drops Apple. bessie, has much practice, can catch an apple if She's standing under a tree from which one falls. While Bessie can walk between the both trees quickly (in much less than a minute), she can stand under only one tree at any Time. moreover, cows do don't get a lot of exercise, so she's not willing to walk back and forth between the trees endlessly (and Thus misses some apples). <span class="Apple-converted-space"><br>Apples Fall (one Minute) for T (1 <= T <=) minutes. Bessie is willing to walk back and forth at most W (1 <= w <=) times. Given which tree would drop an apple each minute, determine the maximum number of apples which Bessie can Catch. Bessie starts at Tree 1.</span> </span></span><p class="pst"><p class="pst">Input</p></p>* Line 1:two Space separated Integers:t and W<span class="Apple-converted-space"><span class="Apple-converted-space"><br>* Lines 2..t+1:1 or 2:the tree that would drop an apple each Minute.</span></span><p class="pst"><p class="pst">Output</p></p>* Line 1:the maximum number of apples Bessie can catch without walking more than W Times.<p class="pst"><p class="pst">Sample Input</p></p><pre class="sio"><pre class="sio">7 22112211</pre></pre><p class="pst"><p class="pst">Sample Output</p></p><pre class="sio"><pre class="sio">6</pre></pre><p class="pst"><p class="pst">Hint</p></p>INPUT DETAILS:<span class="Apple-converted-space"><span class="Apple-converted-space"><br>Seven apples fall-one from tree 2, then both in a row from tree 1, then both in a row from tree 2, then both in a row from Tree 1. Bessie is willing to walk from one tree to the other twice. <span class="Apple-converted-space"><br>OUTPUT DETAILS: <span class="Apple-converted-space"><br>Bessie can catch six apples by staying under tree 1 until the first has dropped, then moving to Tree 2 for the next T wo, then returning back to Tree 1 for the final.</span> </span> </span></span>A puzzle: an apple tree can drop apples in 1 and 2 places, asking up to the maximum number of apples that can be moved m times in the street of t-minute, the DP is processed by the number of moves per minute, the odd number of times can be transferred to the Apple at position 1, the even opposite, and then from 1~ T minutes per minute on the number of moves to determine the current time movement J can receive the maximum number of apples; if (j&1)<br>Dp[j][i]=max (dp[j-1][i-1],dp[j][i-1]) +x-1;<br>Else<br>Dp[j][i]=max (dp[j-1][i-1],dp[j][i-1]) + (x&1); code:<pre class="brush:csharp;gutter:true;"><pre class="brush:csharp;gutter:true;">#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include < cmath> #include <queue> #include <stack> #include <vector>using namespace Std;const int inf= 0x3f3f3f3f;const Double Pi=acos ( -1.0), #define MEM (x, y) memset (x,y,sizeof (×)) #define SI (x) scanf ("%d", &x) #define SL (x) scanf ("%lld", &x) #define PI (x) printf ("%d", x) #define PL (x) printf ("%lld", x) #define P_ printf (""); #define Puts Puts (""); typedef long LONG Ll;const int maxn=1010;int dp[35][maxn];int main () {int t,w;while (~scanf ("%d%d",&t,& W) {mem (dp,0); int x;for (int i=1;i<=t;i++) {SI (x);dp [0][i]=dp[0][i-1]+ (x&1); for (int j=1;j<=w;j++) {if (j &1) Dp[j][i]=max (dp[j-1][i-1],dp[j][i-1]) +x-1;else dp[j][i]=max (dp[j-1][i-1],dp[j][i-1]) + (x&1);}} int ans=0;for (int i=0;i<=w;i++) ans=max (ans,dp[i][t]);p rintf ("%d\n", ans);} Return 0;}</pre></pre><p><p> </p></p><p><p>Apple Catching (dp)</p></p></span>
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