Reference: http://blog.sina.com.cn/s/blog_61034ad90100encg.html
F[I][J] Indicates that I move J times in the case, f[i][j] = Max (F[i-1][j], f[i-1][j-1]) + int (A[i] = = (j + 1)% 2) indicates that in this case, received the most Apple for the case at the last moment, this does not move And the maximum number of moves, plus whether the current position is under the new fruit tree.
Study.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream> #include <vector> #include <unordered_map> #include < unordered_set> #include <queue> #include <string> #include <algorithm> #include <sstream># Include <set> #include <stack> #define INT_MAX 2147483647//Maximum (Signed) INT value#define int_min (-214748 3647-1)//Minimum (signed) int value, #define MIN (x) < (y)? (x):(Y) using namespace Std;int N, K, f[1004][34] = {0}, a[1004], ans = 0;int Max (int a, int b) {return a > B? a:b;} void Init () {cin >> n >> k;for (int i = 1; I <= n; i++) {cin >> a[i]; A[i]%= 2;} The number of the processing tree. }void DP () {int I, J, t;for (i = 1; I <= n; i++) {f[i][0] = f[i-1][0] + int (a[i] = = 1);} Initializes a 0-time jump. for (i = 1; l <= N; i++) for (j = 1; J <= min (i, k), J + +)//j<=i; {//t = f[i-1][j];//if (f[i-1][j-1]>t) t = f[i-1][j-1];//f[i][j] = t + int (a[i] = = (j + 1)% 2); F[i][j] = Max (f [I-1] [j], F[i-1][j- 1]); F[i][j] + = Int (A[i] = = (j + 1)% 2);} for (i = 1; I <= n; i++) {for (j = 1; J <= min (i, k); + j)//j<=i;{ cout << F[i][j] << "";} cout << Endl;} for (i = 0; I <= k; i++) if (f[n][i]>ans) ans = f[n][i];cout << ans;} int main () {init ();dp(); system ("pause"); return 0;}
Dynamic programming-cows pick apples