Dynamic planning [Getting Started]1-max Sub-matrices and

Source: Internet
Author: User

Analysis we have solved one-dimensional problems (the largest sub-segments and problems in the foundation), and now we are two-dimensional, and we see if we can turn this problem into one-dimensional problem. The last sub-matrix must be between some two rows.  Suppose we think that the sub-matrix is between line I and column J, how do we get I and J, yes, enum. Enumerates all 1<=i<=j<=m, representing the row range selected by the final sub-matrix. We take each row from line I to line J and find out, form an array C, and then a row I to line J between the largest sub-matrix and corresponding to this and the largest child of the array C and. As a result, our algorithm becomes:
For i = 1 to m dofor j = i to M do//calculates the first row I to column j and for k = 1 to N doc[k] = (j = = i)? A[i][k]: (C[k] + a[j][k]) endfor The maximal sub-segments of C and the global optimal results of endforendfor are obtained.
Let's look at the marked red part of the sum of all the numbers between rows I and J, and we don't use a loop again, but as J grows, each time the result of line J is superimposed on the previous sum. In addition, the largest subarray of C and a linear time algorithm can actually be combined with that K's for loop, but without affecting the complexity of the time. The complexity of Time is O (m^2n). Finally, we provide input and output data, you write a program, the implementation of this algorithm, only write the correct program, to continue the course behind. input
Line 1th: M and N, separated by spaces (2 <= m,n <= 500). 2-n + 1 lines: The elements in the matrix, the number of m per line, separated by a space in the middle. ( -10^9 <= m[i] <= 10^9)
Output
The maximum value of the output and. If all the numbers are negative, the output is 0.
Input Example
3 3-1 3-12-1 3-3 1 2

Sample Output
7

Dynamic planning [Getting Started]1-max Sub-matrices and

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.