First, maximum sub-segments and
Problem
Given n number A1, A2, ... An, from which K (k is not fixed) a continuous number of Ai, ai+1, ... Ai+k-1, making ∑ i+ K−1iat Maximum, the maximum value is obtained.
Analysis
The maximum number of sub-segments can be solved by multiple algorithms.
(1) Direct enumeration
max = 0;for i in [1...N] for J in [I....N] sum = 0; For k in [I...J] sum + = A[k] if (Sum > Max) max = sum//time complexity O (n^3)
(2) when seeking SUM[I...J], direct use of sum[i...j] = Sum[i...j-1] + a[j] to optimize
max = 0;for i in [1...N] sum = 0 for j in [I....N] sum + = A[j] if (Sum > Max) max = sum//time complexity o (n^2)
(3) divide and conquer the law  
A1 ... The second method is divided into the left and right sides, then A1 ... The largest contiguous sub-segment in an and possibly three cases:  
"1" is A1 ... The largest contiguous sub-segments and in An/2,
"2" are the largest contiguous segments in an/2+1....an and  
"3" across the left and right sides
int Maxsum (int* A, int beg, int end) { if (beg = = end) { return A[beg] > 0? A[beg]: 0 ;} int mid = (beg + end)/2; int max_left = Maxsum (A, beg, mid); int max_right = Maxsum (A, mid + 1, end); int s1 = 0, s2 = 0, M_left = 0, m_right = 0; for (int i = mid; I <= beg; I-) { S1 + = A[i]; if (S1 > M_left) m_left = S1; } for (int i = mid+1; I <= end; i + +) { S2 + = a[i]; if (S2 > M_right) m_right = s2; } int max_sum = Max_left; if (Max_right > Max_sum) max_sum = max_right; if (M_right + m_left > max_sum) max_sum = M_left + m_right; return max_sum;} Time complexity of O (NLOGN)
(4) Dynamic programming algorithm
A recursive formula is used to represent the maximum value of the number of contiguous segments ending in AI with the dp[i array]:
Dp[i] = Max{dp[i-1] + a[i], A[i]}
int max = 0;for (int i = 1; I <= n; i + +) { if (Dp[i-1] > 0) { dp[i] = dp[i-1] + a[i]; } else{ Dp[i] = A[i]; } if (dp[i]> max) { max = dp[i];} } Time Complexity of O (n)
Second, maximum sub-matrices and
Problem
Given the matrix of MXN, its sub-matrix r{x1, y1, x2, y2} (x1, y1) are the coordinates of the upper-left corner of the matrix, (x2, y2) is the coordinate of the lower-right corner of the Matrix, S (X1,y1,x2,y2) represents the number of the sub-matrix R and the maximum value of all the sub-matrices.
analysis &NBSP;
Matrix has M-line, line1, Line2....linem. First limit the behavior from line I to line J, any two columns K and L, the sum of the elements in a sub-matrix formed: Since the line has been limited to line I to J, then the sum of the formed sub-matrix can be treated as line I to J Row to add a one-dimensional array T, Select the element and between the K elements in T and the L elements. This is also attributed to a maximum number of sub-segments and problems. &NBSP;
The row range is selected first (for example, from line I to line J). The selected row elements are added as columns to get a one-dimensional array, and then the largest child of the one-dimensional array and
int sum_line[m][m];for (int i =1; i <= n; i + +) { Sum_line[1][i] = a[1][i];} for (int i = 2; I <= m; i + +) {for (int k = 1;k <= N; k + +) { sum_line[i][k] = Sum_line[i-1][k] + a[i][k]; }}for (int i = 1; I <= m; i + +) {for (int j = i; J <= M; j + +) { int b = 0; for (int k = 1; k <= N; k + +) { if (b > 0) { b + = (Sum_line[j][k]-sum_line[i-1][k]); } else{ B = sum_line[j][k]-sum_line[i-1][k]; } if (b > Max) { max = b; } } } Time complexity of O (M*m*n)
the largest m sub-segment and
Problem
Given the number of n, select m non-overlapping contiguous sub-segments, so that the maximum value of these successive sub-segments, the maximum and value.
analysis &NBSP;
&NBSP;F[I][J] means the A[1...J] These numbers are divided into I disjoint contiguous sub-segments, and A[j] is the end of paragraph I, the maximum value of this I sub-segment. Then there is the equation of &NBSP; f[i][j] = max{f[i][j-1] + a[j], max{f[i-1][k]} k = i-1, i+1...j-1 } &NBSP;
The first case is a[1...j-1] Divided into I disjoint contiguous sub-segments, a[j-1] at the end of paragraph I, at which time plus a[j] so that a[j] and A[j-1] are located in paragraph I sub-paragraph ; The second case is to divide a[1...k] into i-1 distinct sub-segments, A[k] at the end of the i-1 sub-paragraph, at which point A[j] makes A[J] a separate paragraph I
auxiliary arrays for optimization
F[I][J] means that the a[1...i] These numbers are divided into J disjoint contiguous sub-segments ( Note that this is the number of the first I is divided into J disjoint sub-segments ), and A[i] is the end of the J-segment, the J-segment and the maximum value; G[i][j] means a[1...i] These numbers are divided into J disjoint contiguous sub-segments, and a[i] is not necessarily the end of the J segment, which is the maximum value of the J-segment.
Then there is a recurrence relationship:
f[i][j] = max{f[i-1][j] + A[i], g[i-1][j-1] + A[i]}
There are two cases: 1. A[i] At least one of the preceding digits in paragraph J; 2. A[i] himself in the section J Sub-paragraph
g[i][j] = max{g[i-1][j], f[i][j]}
There are two cases: 1. A[i] is not in paragraph J, which is equivalent to dividing a[1...i-1] into J-Segment 2. A[i] In sub-paragraph J
int max_sum (int m, int n) { int i, J, T; for (i = 1; I <= n; i + +) { t = min (i, m); for (j = 1; j <= T; j + +) { F[j] = max (F[j], g[j-1]) + a[i]; G[J-1] = max (g[j-1], f[j]); } } return g[m];}
Dynamic planning-Maximum sub-segments and