This question is clearly a dynamic programming problem, but we found that if we want to use a dynamic programming to do this question, then the state and the transfer will be very troublesome, this time we need two dynamic programming in order to the completion of a complex process, we see only ask this question if the best results for each row, then the transfer will be very simple, so can we convert this problem into each line of paint to seek the optimal solution k times again seeking the optimal solution before the i-th row of the k-th operation.
Since the idea is determined, then we can set the state: 1- disposed g i-th row in the array represents, paint j times k is the optimal solution to the brush, 2- f provided a front row i, j times the optimal brush solution.
Write transfer equation is also good: 1- g [i] [j] [k] = max (g [i] [j] [k], max (with the prefix p and calculates the maximum number k between the right a) + g [i] [j-1] [p]); p j-1 from the start of the enumeration, specific comment on why the codes, k is from the beginning of the j, j as j-th conducted after painting j point to at least paint.
FOR f is f [i] [j] = max (f [i-1] [jk] + g [i] [k] [m]).
On the following codes:
#include <the iostream> #include <cstdio> the using namespace STD; int F [ 101 ] [ 3001 ], SUM [ 101 ] [ 3001 ]; // F denotes the front i-th row j times the maximum value of the brush. int G [ 101 ] [ 3001 ] [ 101 ]; // i-th row, j-th brush to paint obtained by k times the maximum value. int n-, m, T; char S [ 250 ]; int main () { iOS :: sync_with_stdio ( 0 ); CIN >> >> m >> n- T; for ( int I =1;i<=n;i++){ cin>>s; sum[i][0]=0; for(int j=1;j<=m;j++){ if(s[j-1]=='1') sum[i][j]=sum[i][j-1]+1; else sum[i][j]=sum[i][j-1]; } } for(int i=1;i<=n;i++){ for(int j=. 1 ; J <= m; J ++ ) { for ( int K = J; K <= m; K ++ ) { for ( int P = J- . 1 ; P <K; P ++) { // since q is the first j-1 times painting, each time paint of at least one cell, so q minimum of J-. 1. G [I] [J] [K] = max (G [I] [J] [K], max (SUM [I] [K ] -sum [I] [P], KP-SUM [I] [K] + [I SUM] [P]) + G [I] [J- . 1 ] [P]); } } } } for ( int = I . 1 ; I <= n-; I ++ ) { for ( int J = . 1 ; J <= T; J ++ ) { for (int k=0;k<=min(j,m);k++){ f[i][j]=max(f[i][j],f[i-1][j-k]+g[i][k][m]); } } } int ans=0; for(int i=1;i<=t;i++){ ans=max(ans,f[n][i]); } cout<<ans; }