Poj 1050 to the max (dynamic)

Source: Internet
Author: User
To the max
Time limit:1000 ms
Memory limit:10000 K
Total submissions:40302
Accepted:21329

Description

Given a two-dimenstmarray of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. the sum of a rectangle is the sum of all the elements in that rectangle. in this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle.
As an example, the maximal sub-rectangle of the array:

0-2-7 0
9 2-6 2
-4 1-4 1
-1 8 0-2
Is in the lower left corner:

9 2
-4 1
-1 8
And has a sum of 15.

Input

The input consists of an n * n Array of integers. the input begins with a single positive integer n on a line by itself, indicating the size of the square two-dimen=array. this is followed by N ^ 2 integers separated by whitespace (spaces and newlines ). these are the N ^ 2 integers of the array, presented in row-Major Order. that is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc. N may be as large as 100. the numbers in the array will be in the range [-127,127].

Output

Output the sum of the maximal sub-rectangle.

Sample Input

40 -2 -7 0 9 2 -6 2-4 1 -4  1 -18  0 -2

Sample output

15

Source

Greater New York 2001

In the case of one-dimensional conditions, the maximum continuous sub-segment and the method are to scan data sequentially from left to 0, with 0 as the boundary. When the sum is less than 0, the value is reset to 0. the state transition equation for dynamic planning is

S = max {Si-1 + AI, AI}, the equation is equivalent to the previous description. This question is an extension of the largest subsegment and in one dimension. The idea is to find all consecutive rows from top to bottom (for example, from row I to row J ), then calculate the sum of each column from row I to row J, then calculate the sum of the n columns and the largest subsegment in one dimension, and find the maximum value.

The largest submatrix. First, a row of columns is very simple to find the largest vertex and we want to convert the matrix into a row of columns, that is, to get the sum from top to bottom during input, map [I] [J] indicates the sum of numbers from top to bottom in column J. In this way, a column is converted into a vertex, and then dual, cyclic, the largest sum of the numbers in the first row of column J in any row is the final largest sum.

Assume that the maximum sub-matrix is a sub-matrix from row R to row K, from column I to column J, as shown below (Ari represents a [r] [I], suppose the array subscript starts from 1 ):
| A11 ...... A1i ...... A1j ...... A1N |
| A21 ...... A2i ...... A2j ...... A2n |
| ...... |
| ...... |
| AR1 ...... Ari ...... ARJ ...... Arn |
| ...... |
| ...... |
| Ak1 ...... Aki ...... Akj ...... Akn |
| ...... |
| An1 ...... Ani ...... Anj ...... Ann |

Then, we add the same columns in each row from row R to row K. We can get a one-dimensional array as follows:
(AR1 + ...... + Ak1, AR2 + ...... + Ak2 ,......, Arn + ...... + Akn)
From this we can see that the last thing we want is the maximum sub-break and problem of this one-dimensional array. Now we have converted the problem into the solved problem above.

Code:
# Include <iostream> using namespace STD; # define M 110int main () {int A [m] [m] = {0 }, c [m] [m] = {0}; // A [m] [m] is used to store data, c [m] [m] is the sum that stores this row into this column. Int I, j, N, max = 0, sum, K; scanf ("% d", & N); for (I = 0; I <n; I ++) for (j = 1; j <= N; j ++) {scanf ("% d", & A [I] [J-1]); c [I] [J] = C [I] [J-1] + A [I] [J-1]; // The sum of the next number in a row and all the above numbers. } For (I = 0; I <n; I ++) for (j = I; j <= N; j ++) {sum = 0; for (k = 0; k <n; k ++) {sum + = C [k] [J]-C [k] [I]; If (sum <0) sum = 0;// If it is smaller than 0, it is equivalent to removing it directly.Else if (sum> MAX) max = sum ;}} printf ("% d \ n", max); return 0 ;}

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.