最大和子矩阵

Memory Limit

ZOJ1074 (最大和子矩阵 DP),zoj1074dp

F – 最大子矩阵和 Time
Limit:
1000MS     Memory Limit:10000KB     64bit IO
Format:
%I64d & %I64u  

Description

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. 

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

15

题解:怎么说呢。。就是第一行的一个序列加到第二行,找到他们的和序列的最大子序列,然后他们的和序列再加第三行,再找,每加一次找一次,加到第n行。
     然后从第二行开始,按照上面的进行,再从第三行开始..... 直到第n行....
     每次都要更新他们的子矩阵的的最大值,用一个变量更新
不说了,上代码,最下面有输出截图

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,sum,f,Max;
int b[20000];
int dp[150][150];
int main()
{
    f=-100;
    cin>>n;
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)
        {
            cin>>dp[i][j];
        }
    for(int k=0; k<n; k++)
    {
        memset(b,0,sizeof(b));      // 一定每次记得清零
        for(int i=k; i<n; i++)
        {
            for(int j=0; j<n; j++)
            {
                b[j]+=dp[i][j];
            }
            sum = Max =-100;         //赋很小的值
            for(int i=0; i<n; i++)
            {
                if (sum<0)
                    sum = b[i];
                else
                    sum += b[i];
                if (Max < sum)
                    Max = sum;
            }
            if(f<Max)                  // 每次都要比较更新最大子矩阵和
                f=Max;
        }
    }
    cout<<f<<endl;
}

88bifa必发唯一官网 1

88bifa必发唯一官网, (最大和子矩阵 DP),zoj1074dp F
-最大子矩阵和 Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format:
%I64d %I64u Description Given a two-dimensional array of…

POJ_1050_To the Max,poj_1050_tothemax

To the Max

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 49811   Accepted: 26400

Description

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. 

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

15

Source

Greater New York 2001  

  • 最大子矩阵和
  • 是一维的最大子串和的二维扩展
  • 那我们把每列做一个前缀和,O1得到从i行到j行单列的和,之后用最大子串和dp求解就行

 

 1 #include <iostream>
 2 #include <string>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <cmath>
 8 #include <vector>
 9 #include <queue>
10 #include <stack>
11 #include <set>
12 #include <map>
13 using namespace std;
14 typedef long long           LL ;
15 typedef unsigned long long ULL ;
16 const int    maxn = 1e2 + 10   ;
17 const int    inf  = 0x3f3f3f3f ;
18 const int    npos = -1         ;
19 const int    mod  = 1e9 + 7    ;
20 const int    mxx  = 100 + 5    ;
21 const double eps  = 1e-6       ;
22 const double PI   = acos(-1.0) ;
23 
24 int n, ans, a[maxn][maxn], b[maxn], c[maxn][maxn];
25 int main(){
26     // freopen("in.txt","r",stdin);
27     // freopen("out.txt","w",stdout);
28     while(~scanf("%d",&n)){
29         ans=-inf;
30         memset(c,0,sizeof(c));
31         for(int i=1;i<=n;i++)
32             for(int j=1;j<=n;j++){
33                 scanf("%d",&a[i][j]);
34                 c[i][j]=c[i-1][j]+a[i][j];
35             }
36         for(int i=1;i<=n;i++){
37             for(int j=i;j<=n;j++){
38                 b[0]=0;
39                 for(int k=1;k<=n;k++){
40                     if(b[k-1]>=0){
41                         b[k]=b[k-1]+c[j][k]-c[i-1][k];
42                     }else{
43                         b[k]=c[j][k]-c[i-1][k];
44                     }
45                     ans=max(ans,b[k]);
46                 }
47             }
48         }
49         printf("%d\n",ans);
50     }
51     return 0;
52 }

 

 

 

the Max,poj_1050_tothemax To the
Max Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 49811
Accepted: 26400 Description Given a two-dimensional array of
pos…