Apple Catching
| Time Limit: 1000MS |
|
Memory Limit: 65536K |
| Total Submissions: 13234 |
|
Accepted: 6437 |
Description 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.
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).
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.Input * Line 1:two Space separated Integers:t and W
* Lines 2..t+1:1 or 2:the tree that would drop an apple each minute.Output * Line 1:the maximum number of apples Bessie can catch without walking more than W times.Sample Input 7 22112211
Sample Output 6
Hint INPUT DETAILS:
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.
OUTPUT DETAILS:
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.Source Usaco 2004 November |
1#include <iostream>2#include <cmath>3#include <cstring>4#include <cstdio>5#include <cstdlib>6#include <algorithm>7 using namespacestd;8 inta[1010];9 intf[1010];Ten intdp[1010][1010]; One intMain () A { - intt,w; -scanf"%d%d",&t,&W); the for(intI=1; i<=t;i++) -scanf"%d",&a[i]); - intPre=0; - for(intI=1; i<=t;i++)//first, close the same apples together, so that the adjacent i,i+1 refers to the apples on different trees. + { - if(a[i]==a[i-1]) f[pre]++; + Else{pre++;f[pre]=1;} A } at for(intI=1; i<=pre;i++) - { -dp[i][0]=f[i];//init: If you don't move, you can eat the fruit of your current position. - for(intj=1; j<=w;j++) - { -Dp[i][j]=max (dp[i-2][j],dp[i-1][j-1]) +f[i];//at this time the adjacent is a different tree, in the middle of the same tree is separated by the in } - } to intans=0; + for(intI=0; i<=w;i++)//Note here: Not necessarily the number of steps are exhausted to eat the most -ans=Max (ans,dp[pre][i]); theprintf"%d", ans); *System"Pause"); $ return 0;Panax Notoginseng}poj2385
POJ 2385--Apple catching