P2690 Pick up applesTopic Background
Usaco
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/output formatInput Format:
The first line is 2 digits, T and W. 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 Format:
For each test point, the output line, one number, is the maximum number of apples the cow receives.
input/Output sampleInput Sample # #: Copy
7 22112211
Output Example # #: Replication
6
IDEA: DP Dynamic programming
F[I][J] represents the maximum number of apples that can be obtained for J times by the time I have been exchanged.
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;intT,w,ans;intnum[1010];intf[1010][ to];intMain () {scanf ("%d%d",&t,&W); for(intI=1; i<=t;i++) scanf ("%d",&Num[i]); for(intI=1; i<=t;i++) for(intj=0; J<=min (i,w); J + +) {F[i][j]=max (f[i-1][j],f[i-1][j-1]); if(num[i]==j%2+1) f[i][j]+=1; } for(intI=0; i<=w;i++) ans=Max (ans,f[t][i]); cout<<ans;}
Rokua P2690 pick up apples