HDU 1081 to the max (maximum sub-matrix)

Source: Internet
Author: User
To the max

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 6612 accepted submission (s): 3159

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

Sourcegreater new york 2001Question:Calculate the maximum sum and of the Child matrices with the size of> = 1*1 in the matrix. Sum is the sum of each element in the Child matrix.
Analysis:This is equivalent to the two-dimensional Maximum substring sum. When HDU 1003 Max sum is one-dimensional, we can see the current sum + I number. If sum <0, sum = 0 and the number I + 1 is the first number to continue accumulating. This process is repeated every time it is negative.
Now let's think about it as a formula for state transfer:F [I] = max (0, F [I-1]) + A [I]
As for this question, it was time to find the optimal sub-structure. The first thought was to find four minimum structures for the first layer, and then expand them. However, in this way, it is easy to miss some situations, and the data processing is also very troublesome, even in the end I was confused.
The correct idea is:Matrix preprocessing. MakeMap [I] [J] stores the sum of the number of J before row I.. For example, in the sample, the matrix 0-2-7 0 9 2-6 2-4 1-4 1-1 8 0-2 is changed: 0-2-9-9 9 11 5 7-4-3-7-6-1 7 5 triple for loop,Sum + = map [k] [J]-map [k] [I-1]. (Map [k] [J]-map [k] [I-1] indicates the element and between column I and column J of row K)Note that K is the innermost loop. After determining the two columns each time, add the row edge and search for maxsum. The determination of the number of columns must also be in a certain order. First, I is fixed at 1, and J ranges from I to n. Then increase I... in turn, which is actually the dual loop outside.
In this way, we can find all the child matrices and find the largest sum.
Note: Max cannot be initialized to 0, because the maximum sum may be negative.
Code:

#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#define INF  0x3f3f3f3fusing namespace std;int map[101][101];int main(){    int tmp,i,j,k,n;    while(scanf("%d",&n)!=EOF)    {        memset(map,0,sizeof(map));        for(i=1;i<=n;i++)        {            for(j=1;j<=n;j++)            {                scanf("%d",&tmp);                map[i][j]=map[i][j-1]+tmp;            }        }        int maxsum=-INF,sum;        for(i=1;i<=n;i++)        {            for(j=i;j<=n;j++)            {                sum=0;                for(k=1;k<=n;k++)                {                    sum+=map[k][j]-map[k][i-1];                    //printf("yes:  %d\n",map[k][j]-map[k][i-1]);                    //printf("test: %d\n",sum);                    if(sum<0)                        sum=0;                    if(sum>maxsum)                        maxsum=sum;                    //printf("test: %d %d\n",sum,maxsum);                }            }        }        printf("%d\n",maxsum);    }    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.