to the Max
problem DescriptionGiven A two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1 X 1 or greater located within the whole array. The sum of a rectangle is the sum of the "the elements in" that rectangle. The problem the sub-rectangle with the largest sum are 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.
InputThe input consists of an n x n array of integers. The input begins with a single positive integer N in a line by itself, indicating the size of the square two-dimensional a Rray. This was followed by N 2 integers separated by whitespace (spaces and newlines). These is the N 2 integers of the array, presented in Row-major order. That's, all numbers on the first row, left-to-right, then all numbers-second row, left-to-right, etc. N may be as large as 100. The numbers in the array would be in the range [ -127,127].
OutputOutput The sum of the maximal sub-rectangle.
Sample Input40-2-7 09 2-6 2-4 1-4 1-18 0-2
Sample Output theis to give a n*n matrix and find its maximum sub-matrix .we've all done the maximal subsequence of a sequence. The complexity of O (n). This problem is the problem of transformation. violence enumerates the sum of the K1 rows to each element of each column of row K2 and Sum[i], and then the maximum subsequence summation for Sum[i] and keep updating the answers .
1#include <cstdio>2#include <cstring>3#include <algorithm>4 5 using namespacestd;6 7 Const intmaxn= the;8 9 intA[MAXN][MAXN];Ten intSUM[MAXN]; One intDP[MAXN]; A - intMain () - { the intN; - while(SCANF ("%d", &n)! =EOF) - { - for(intI=1; i<=n;i++) + for(intj=1; j<=n;j++) -scanf"%d",&a[i][j]); + A intans=-1000000; at - for(intI=1; i<=n;i++) - { -Memset (DP,0,sizeof(DP)); -memset (SUM,0,sizeof(sum)); - for(intj=i;j<=n;j++) in { - for(intk=1; k<=n;k++) to { +sum[k]+=A[j][k]; - } the * for(intk=1; k<=n;k++) $ {Panax NotoginsengDp[k]=max (dp[k-1],0)+Sum[k]; - if(dp[k]>ans) theans=Dp[k]; + } A the } + } - $printf"%d\n", ans); $ } - - return 0; the}View Code
HDU 1081 to the Max basic DP Good question