Given a two-dimensional array 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.
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-dimensional 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
4 0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -1 8 0 -2
Sample Output
Question 15 means to find the maximum submatrix sum. Analyze the extension of one-dimensional maximal consecutive subsegment sums. First, when inputting the traversal, you can find the max (the sum of the largest consecutive sub-segments of each row, which is equivalent to a sub-matrix with a width of one), and then traverse, adding the elements of the corresponding columns of each row after the i-th row to the corresponding column of the i-th row Elements, each time a row is added, the maximum field sum is calculated once, so that the multiple rows of the sub-matrix are compressed into one row, and after it becomes one row, the maximum field sum is obtained! clever!
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<algorithm> #include<cstring> #include <queue> #include <vector> #include<bitset> #include<map> #include<deque> using namespace std; typedef long long LL; const int maxn = 1e4+5; const int mod = 77200211+233; typedef pair<int,int> pii; #define X first #define Y second #define pb push_back //#define mp make_pair #define ms(a,b) memset(a,b,sizeof(a)) const int inf = 0x3f3f3f3f; #define lson l,m,2*rt #define rson m+1,r,2*rt+1 typedef long long ll; #define N 100010 int a[105][105]; int main(){ #ifdef LOCAL freopen("in.txt","r",stdin); #endif // LOCAL int n,tmp; scanf("%d",&n); int ans = -inf; for(int i=1;i<=n;i++){ tmp=0; for(int j=1;j<=n;j++){ scanf("%d",&a[i][j]); if(tmp>0) tmp+=a[i][j]; else tmp=a[i][j]; years = max(years,tmp); } } for(int i=1;i<n;i++){ for(int j=i+1;j<=n;j++){ tmp=0; for(int k=1;k<=n;k++){ a[i][k]+=a[j][k]; if(tmp>0) tmp+= a[i][k]; else tmp = a[i][k]; years = max(years,tmp); } } } cout<<ans<<endl; return 0; }
By the way, give the code and idea of finding the maximum sub-segment sum:
int maxsum ( int x [], int n) { int i,b = 0,k = -10000000; for(i = 0 ; i < n ; ++i) { if (b > 0 ) b += x[i]; // If the accumulated sum is positive, continue to add /* If b <= 0, then there must be x[i-1]<0, x[i] is pending, then if x[i]>= 0, b=x[i] for granted; what if x[i]<0? Is b=x[i] appropriate? The answer is appropriate. Because b is still less than 0 in the next loop, you can definitely find a number greater than 0 There is one more question: b = x[i], why don't you just discard all the fields just now? What if the sub-segments of the sub-segment just now (the first few are negative numbers) are greater than 0? But this is not possible. Because the first number of a field must be a positive number, because if the first number is negative, Then b<0, the else will be executed, and the accumulation of a sub-segment will not start until a positive number appears. */ else b = x[i]; // If the accumulated sum is negative, assign this value to b if (b > k) k = b; // update max field sum } return k; }