Topic Links:
https://vjudge.net/problem/POJ-2385
Main topic:
Two apple trees each chapter has a tree falling apple, someone to pick up, but back and forth two trees between the number of times is certain, so find out the maximum number of times to receive the maximum number of apples.
Ideas:
DP[I][J] Indicates the maximum number of apples in time I, which has been back and forth for J times.
Initialize dp[1][0] and dp[1][1] According to which tree the first Apple is falling down to initialize
Transfer equation: dp[i][j] = max (Dp[i-1][j], dp[i-1][j-1]), and if there is an apple drop in this state, you have to add a
According to J's parity to determine under which tree, J is odd under Tree-2, J is even under tree-1
1#include <iostream>2#include <vector>3#include <queue>4#include <algorithm>5#include <cstring>6#include <cstdio>7#include <Set>8#include <map>9#include <cmath>Ten#include <sstream> One using namespacestd; Atypedef pair<int,int>Pair; -typedefLong Longll; - Const intINF =0x3f3f3f3f; the intT, N, M, Minans; - Const intMAXN = 1e3 +Ten; - Const intMoD =1e9; - intA[MAXN]; + intdp[maxn][ +];//Dp[i][j] Indicates the maximum number of apples for the first I-minute transfer J - intMain () + { ACIN >> N >>m; at for(inti =1; I <= N; i++) Cin >> A[i], A[i]%=2; - if(a[1] ==1)//Initial State - { -dp[1][0] =1; -dp[1][1] =0; - } in Else - { todp[1][0] =0; +dp[1][1] =1; - } the for(inti =2; I <= N; i++) * { $ for(intj =0; J <= M; J + +)Panax Notoginseng { - if(J = =0) the //once did not transfer, has been in the tree1, if at this time a[i] for 1,tree1 off Apple, can add, conversely Plus is also 0 +DP[I][J] = dp[i-1][J] +A[i]; A Else the { +DP[I][J] = max (Dp[i-1][J], Dp[i-1][j-1]); - $ //Transfer J at this time $ //J is odd, at 2, if there is an apple at this time, add a - if((j%2==1) && (a[i] = =0)) -dp[i][j]++; the //J is even, at 1, if there is an apple at this time, add a - if((j%2==0) && (a[i] = =1))Wuyidp[i][j]++; the } - } Wu } - intAns =0; About for(inti =0; I <= m; i++) ans =Max (ans, dp[n][i]); $cout<<ans<<Endl; - return 0; -}
POJ-2385 Apple Catching---DP