Test instructions: Tree 1 tree 2 will lose apples, cows to pick up, can only move w times, at the beginning of the tree 1 asked the maximum number of apples can be picked up?
Idea: dp[i][j] means I minute move J times pick up the maximum value of the apple
Example analysis
0,1 ... description even in tree 1 odd in tree 2
for (int i = 1; I <= n; i++)
{
scanf ("%d", &t[i]);
T[i]-= 1;
}
for (int i = 1; I <= n; i++)
for (int j = 0; J <= W; j + +)
{
if (j% 2) dp[i][j] = max (Dp[i-1][j], dp[i-1][j-1]) + t[i];
else dp[i][j] = max (Dp[i-1][j], dp[i-1][j-1]) +!t[i];
}
Here's a little trick, not every time you ask for data like 1 2 2, we put them all--1 and then we can look better.
Explain the two-sentence DP statement
DP[I][J] = max (Dp[i-1][j], dp[i-1][j-1]) represents the last time either in tree 1 or in tree 2, but I only need the maximum value between them
Code to solve the problem:
#include <iostream>#include<cstdio>#include<algorithm>using namespacestd;intdp[1010][ *];intt[1010];intMain () {intN, W; scanf ("%d%d", &n, &W); for(inti =1; I <= N; i++) {scanf ("%d", &T[i]); T[i]-=1; } for(inti =1; I <= N; i++) for(intj =0; J <= W; J + +) { if(J%2) Dp[i][j] = max (Dp[i-1][J], Dp[i-1][j-1]) +T[i]; ElseDP[I][J] = max (Dp[i-1][J], Dp[i-1][j-1]) + !T[i]; } printf ("%d\n", Dp[n][w]);}
POJ 2385 tree off Apple problem DP algorithm