My design status and Transfer Process
DP [I] [J] indicates that the maximum value of Apple can be obtained by taking up to J times in the previous minute.
You can
Pre-i-1 minutes up to J times
Previous I-1 minutes up to J-1 times
These two statuses are transferred.
NOTE: For the second type of transfer, you can choose to leave or not go for the J-time. Because it takes up to J times
Similar to the previous tree-like DP
View code
# Include <cstdio>
# Include <cstring>
Int DP [ 1010 ] [ 35 ];
Int Num [ 1010 ];
Int Max ( Int A, Int B ){
Return A> B? A: B;
}
Int Main (){
Int T, W, I, j;
While (Scanf ( " % D " , & T, & W )! = EOF ){
For (I = 1 ; I <= T; I ++) scanf ( " % D " , & Num [I]);
Memset (DP, 0 , Sizeof (DP ));
If (Num [ 1 ] = 1 ) DP [ 1 ] [ 0 ] = 1 ;
DP [ 1 ] [1 ] = 1 ;
For (I = 2 ; I <= T; I ++ ){
For (J = 0 ; J <= W; j ++ ){
If (J = 0 ){
DP [I] [J] = DP [I- 1 ] [J] + num [I] % 2 ;
Continue ;
}
DP [I] [J] = max (DP [I] [J], DP [I- 1 ] [J] + (J % 2 + 1 = Num [I]);
DP [I] [J] = max (DP [I] [J], DP [I- 1 ] [J- 1 ] + (J % 2 = Num [I]);
DP [I] [J] = max (DP [I] [J], DP [I- 1 ] [J- 1 ] + (J % 2 + 1 = Num [I]);
}
}
Printf ( " % D \ n " , DP [T] [W]);
}
}