1146. Maximum Sum
Time limit: 0.5 second
Memory limit: 64 MB
Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest sum. 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. A sub-rectangle is any contiguous sub-array of size 1 × 1 or greater located within the whole array.
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-hand corner and has the sum of 15.
Input
The input consists of an N × N array of integers. The input begins with a single positive integer Non a line by itself indicating the size of the square two dimensional array.
This is followed by N 2integers separated by white-space (newlines and spaces). These N 2 integers
make up the array in row-major order (i.e., all numbers on the first row, left-to-right, then all numbers on 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
The output is the sum of the maximal sub-rectangle.
Sample
input | output |
---|---|
4 0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -1 8 0 -2 |
15 |
题意:给个n*n的矩阵,所有子矩阵中 ,和最大等于多少。
做法:
首先要理解一个O(n)的算法 。
给一个数组 求连续和的最大值。 可以用一个sum来从下标0开始计算和,不断取最大值。当加和小于0的时候 初始化为0;
如 3 -4 5 1 -2 第一个步加和是3,再加上第二个-4 ,sum就变成-1了,所以要初始化sum为0,再加5 ,再加1, 最后得到最大值为6。
然后对于矩阵,我们可以先预处理,sum[ i ] [ j ]为 第 i 列的 前j项和;
然后枚举 两列 i,j ,然后k表示行 从1到n。 那么sum[ k ][ j ] -sum [ k ][ i - 1 ] 就是k行 i列到j列的和,可以看作是一个点的值,就和上面讲的一样,一点点加过来,然后遇到负值初始化为0 就可以了。最后的最大值 就是答案了。
暴力: 预处理了 sum数组,预处理 sum[ i ][ j ]= (0,0) 到(i,j)的和。然后枚举矩阵中任意两点,求最大和。 复杂度是10^8/4 也就是 2*10^7次,也可以ac。
#include<stdio.h> #include<string.h> int sum[110][110]; int main() { int n; while(scanf("%d",&n)!=EOF) { memset(sum,0,sizeof sum); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { int a; scanf("%d",&a); sum[i][j]=sum[i][j-1]+a; } } int ans=-1000000000; for(int i=1;i<=n;i++) for(int j=i;j<=n;j++)//j>=i for(int k=1,tem=0;k<=n;k++) { tem+=sum[k][j]-sum[k][i-1]; ans=ans>tem?ans:tem; if(tem<0) tem=0; } printf("%d\n",ans); } return 0; }
//暴力 #include <stdio.h> #include <math.h> #include <algorithm> using namespace std; int sum[110][110]; int a[110][110]; int main() { int n; //freopen("output.txt","w",stdout); while(~scanf("%d",&n)) { for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { scanf("%d",&a[i][j]); int tem=a[i][j]; if(i!=0) tem+=sum[i-1][j]; if(j!=0) tem+=sum[i][j-1]; if(i!=0&&j!=0) tem-=sum[i-1][j-1]; sum[i][j]=tem; //printf("%d ",tem); } //puts(""); } int flag=1; int maxx; for(int i=0;i<n;i++) for(int j=0;j<n;j++) for(int k=0;k<=i;k++) for(int l=0;l<=j;l++) { int tem=0; tem+=sum[i][j]; if(k!=0) tem-=sum[k-1][j]; if(l!=0) tem-=sum[i][l-1]; if(l!=0&&k!=0) tem+=sum[k-1][l-1]; if(tem==11) int aa=2; if(flag) { maxx=tem; flag=0; } else maxx=max(maxx,tem); } printf("%d\n",maxx); } return 0; } /* 4 0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -1 8 0 -2 5 1 1 1 1 1 1 1 1 1 1 1 1 -99 1 1 1 1 1 1 1 1 1 1 1 1 */