1 /*2 Test Instructions: There are two apple trees, each apple tree drops every second of an apple, a person under the tree to catch apples, do not let Apple fall! 3 The movement of people between the two trees is fast! But the number of people moving is limited, ask how many apples can be caught! 4 5 idea: Dp[i][j] represents the number of apples that have been caught in the case of J when the number of moves is the first I Apple drops! 6 7 so Dp[i][j]=max (Dp[i-1][j], dp[i][j-1]) + a[i]==j%2+1? 1:0;8 9 The a[i]==j%2+1 indicates that the first J-Move moves exactly under the a[i] apple tree, and this apple tree falls off the apple, just to catch! Ten */ One#include <iostream> A#include <cstring> -#include <cstdio> -#include <algorithm> the #defineM 1005 - using namespacestd; - - intdp[m][ *]; + - intN, M; + intA[m]; A at intMain () { -scanf"%d%d", &n, &m); - for(intI=1; i<=n; ++i) -scanf"%d", &a[i]); - if(a[1]==1) dp[1][0]+=1; - for(intI=2; i<=n; ++i) { indp[i][0]=dp[i-1][0]; - if(a[i]==1) todp[i][0]+=1; + } - the for(intj=1; j<=m; ++j) * for(intI=j; i<=n; ++i) { $Dp[i][j]=max (dp[i][j-1], dp[i-1][j]);Panax Notoginseng intcc=j%2+1; - if(a[i]==cc) thedp[i][j]+=1; + } Aprintf"%d\n", Dp[n][m]); the return 0; +}
POJ 2385Apple catching (simple DP)