The sum of the maximum child matrices of HDU 1081 and the DP

Source: Internet
Author: User
To the max Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 8210 accepted submission (s): 3991


Problem descriptiongiven a two-dimen=array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1x1 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.
 
Inputthe input consists of an n x 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].
 
Outputoutput 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 The question is clear: give an n * n matrix and find its sub-matrix (element and maximum); (1) first, we need to know how to calculate the maximum child segment and of a one-dimensional array;
Int sum = 0, max =-9999999; For (INT I = 1; I <= N; I ++) // n elements are used in the array, after traversing it, You can {If (sum <0) sum = 0; // If sum is smaller than 0, you can skip the previous steps and start from scratch, initialize 0 sum + = A [I]; If (sum> MAX) max = sum; // update Max continuously. The final Max is the maximum field and}
(2) understand the maximum field and solution of one dimension, and then look at the two-dimensional. The classic method is to compress the two-dimensional data into one-dimensional data. If this compression process is clear, it will be easy; for example, the matrix of 4*4 is 0-2-7 0 9 2-6 2-4 1-4 1-1 8 0-2 1. regard the first column as one-dimensional to find the maximum Max; 2. merge the first column and the second column as one-dimensional Max 3. merge the first, second, and third columns as one-dimensional maxversions. 4. merge the first, second, third, and fourth columns as one-dimensional maxversions;
5. Think of the second column as the one-dimensional maximum value 6. merge the second column and the third column into one-dimensional maximum value 7. merge the second column, third column, and fourth column into one-dimensional Maximum Value
8. Think of the third column as one-dimensional, evaluate the maximum value 9. Merge the third column and the fourth column to obtain the maximum value;
10. Consider the fourth column as one-dimensional, and find the final value ~~~~~~ The compression process is like this ~~ It should be understandable !!! Before compression, perform the following operations: DP [I] [J] indicates the sum of elements in column J before row I. For example, when merging the second and third columns, the following operations are performed: DP [1] [3]-DP [1] [2-1] indicates the sum of the first three elements of the first row and the sum of the first element of the first row. one-dimensional Processing ~~~ And so on
# Include <iostream> # include <stdio. h> # include <string. h ># include <string> const int maxn = 110; using namespace STD; int DP [maxn] [maxn]; int main () {int N; while (CIN> N) {memset (DP, 0, sizeof (DP); For (INT I = 1; I <= N; I ++) // DP [I] [J] indicates the first J elements of row I for (Int J = 1; j <= N; j ++) {CIN> DP [I] [J]; DP [I] [J] + = DP [I] [J-1];} int max =-99999999; for (INT I = 1; I <= N; I ++) for (Int J = I; j <= N; j ++) {int sum = 0; for (int K = 1; k <= N; k ++) {If (sum <0) sum = 0; sum + = DP [k] [J]-DP [k] [I-1]; // DP [k] [J]-DP [k] [I-1] indicates and if (sum> MAX) of elements from column I to column J of row K) max = sum ;}} cout <max <Endl ;}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.