现在的位置: 首页 > 综合 > 正文

po1050 To the Max 最大子矩阵和

2014年02月27日 ⁄ 综合 ⁄ 共 3139字 ⁄ 字号 评论关闭
To the Max
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 33444   Accepted: 17495

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

/*
f[i][j]表示第i列从第一行到第j行的累加和
原矩阵
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8  0 -2
新矩阵
0 9 5 4
-2 0 1 9
-7 -13 -17 -17
0 2 3 1
把矩形压缩成宽为1的数,按一维的求最大连续字段和的求法即可。
压缩:f[k][j]-f[k][i-1]表示的是第k列从第i行到第j行的和,把它看成一个数。
*/
#include<iostream>
#include<cstdlib>
#include<stdio.h>
using namespace std;
int  a[105][105];
int  f[105][105];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            f[i][j]=0;
            scanf("%d",&a[i][j]);
        }
        for(int i=1;i<=n;i++)//表示原矩阵的列,新矩阵的行
        {
            for(int j=1;j<=n;j++)
            f[i][j]+=f[i][j-1]+a[j][i];
        }
        int maxn=-999999999;
        int p=-999999999;
        for(int i=1;i<=n;i++)
        {
            for(int j=i;j<=n;j++)
            {
               int sum=f[1][j]-f[1][i-1];
               for(int k=2;k<=n;k++)
               {
                   if(sum>0)
                   sum+=f[k][j]-f[k][i-1];
                   else
                   sum=f[k][j]-f[k][i-1];
                   if(sum>p) p=sum;//
               }
               maxn=max(maxn,p);
            }
        }
        cout<<maxn<<endl;
    }
}
 
 
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<memory.h>
using namespace std;
const int N=110;
int a[N][N];
int f[N][N];
int num[N][N];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        scanf("%d",&a[i][j]);
        memset(f,0,sizeof(f));
        memset(num,0,sizeof(num));
        for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            f[j][i]=f[j][i-1]+a[i][j];
            num[i][j]=f[j][i];
        }
      /*  for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            cout<<f[i][j]<<" ";
            if(j==n)
            cout<<endl;
        }

        cout<<endl;
        for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            cout<<num[i][j]<<" ";
            if(j==n)
            cout<<endl;
        }
*/
        int maxn=-999999999;
       /* for(int i=1;i<=n;i++)//列
        for(int j=i;j<=n;j++)//行
        {
            int s=f[j][1]-f[j][i-1];
            for(int k=2;k<=n;k++)
            {
                if(s>0)
                s+=f[j][k]-f[j][i-1];
                else
                s=f[j][k]-f[j][i-1];
                if(s>maxn)
                maxn=s;
            }
        }*/
        for(int i=1;i<=n;i++)
        for(int j=1;j<i;j++)
        {
            int s=num[i][1]-num[j][1];
            for(int k=2;k<=n;k++)
            {
                if(s>0)
                s+=num[i][k]-num[j][k];
                else
                s=num[i][k]-num[j][k];
                if(s>maxn)
                maxn=s;
            }
        }
        cout<<maxn<<endl;
    }
}

#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<memory.h>
using namespace std;
const int N=110;
int a[N][N];
int dp[N][N];
int f[N][N];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        scanf("%d",&a[i][j]);
        memset(f,0,sizeof(f));
        for(int j=1;j<=n;j++)
        for(int i=1;i<=n;i++)
        {
            f[i][j]=f[i-1][j]+a[i][j];
        }
        int maxn=-999999999;
        for(int i=1;i<=n;i++)
        for(int k=0;k<i;k++)
        {
            int m=0;
            for(int j=1;j<=n;j++)
            {
               m=max(f[i][j]-f[k][j],m+f[i][j]-f[k][j]);
               if(m>maxn)
               maxn=m;
            }
        }
        cout<<maxn<<endl;
    }
}

抱歉!评论已关闭.