POJ1050

来源:互联网 发布:如何降低wifi网络延迟 编辑:程序博客网 时间:2024/05/16 07:35
To the Max
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 31864 Accepted: 16615

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

40 -2 -7 0 9 2 -6 2-4 1 -4  1 -18  0 -2

Sample Output

15

Source

Greater New York 2001
 
题意:
    很明了,就是求最大子矩阵和。
    现在讲一下最大子矩阵和的求法。
    2 维数组a[1 : m][1 : n]表示给定的mn列的整数矩阵。子数组a[i1 : i2][j1 : j2]表示左上角和右下角行列坐标分别为(i1, j1)(i2, j2)的子矩阵,其各元素之和记为:

1)s(i1,i2,j1,j2)=a[i1][i2]+a[i1][i2+1]+……+a[i][j]+……+a[j1][j2];(i1<=i<=i2;j1<=j<=j2)

推导后可以得到类似一维的最大字段和的问题。
下面为算法:
max_sum_submatrix(int m,int n,int a[][n]){    int sum=-99999999;    int max;    for(i=1;i<=n;i++)    {        for(j=1;j<=n;j++)        {            b[j]=0;        }        for(j=i;j<=n;j++)        {            for(k=1;k<=n;k++)            {                b[k]+=a[j][k];            }            max=max_sum_subseq(n,b);            sum=sum>max?sum:max;        }    }

下面举例说明:
如矩阵:
            -2    10    20
            100   -1    2
                  -2  -3

当i=1时,初始化数组b,使得

b:0 0 0

 

当j=1时,k从1递增到n,由算法的第15行可得数组b将首先存储矩阵的第一行的各值,即b为:

b:-2   10   20

 

由最大子序列和的函数max_sum_subseq()返回该序列的最大子序列的和值为max=30;

当j=2时,k从1递增到n,由算法的第15行可得数组b将矩阵a第二行的值分别加到原有各值上,可得数组b为

 

 

b:98       18

同理,返回该序列的最大子序列的和值为max=125。

当j=3时,k从1递增到n,由算法的第15行可得数组b将矩阵a第三行的值分别加到原有各值上,可得数组b为

b:98        15

 

同理,返回该序列的最大子序列的和值为max=120。

到此,i的第一次循环结束。

当i=2时,从新初始化数组b,使得

b:0      0

 

j=2时,k从1递增到n,由算法的第15行可得数组b将首先存储矩阵的第二行的各值,即b为:

b:100   -1   -2

 

返回该序列的最大子序列的和值为max=100。

j=3时,k从1递增到n,由算法的第15行可得数组b将矩阵a第三行的值分别加到原有各值上,可得数组b为

b:100    -3    -5

 

返回该序列的最大子序列的和值为max=92。

到此,i的第二次循环结束。

当i=3循环结束时,可求出该矩阵的最大子矩阵和值为125。




下面是poj1050的代码:

#include <cstdio>using namespace std;int n;int array[110][110];int temp[110];int max_sum_subseq(){    int i,res,tmp[110];    for(i=1;i<=n;i++)    {        tmp[i]=temp[i];    }    res=tmp[1];    for(i=2;i<=n;i++)    {        tmp[i]=tmp[i]>tmp[i]+tmp[i-1]?tmp[i]:tmp[i]+tmp[i-1];        res=res>tmp[i]?res:tmp[i];    }    return res;}int main(){    int i,j,k;    int sum=-99999999,max;    scanf("%d",&n);    for(i=1;i<=n;i++)    {        for(j=1;j<=n;j++)        {            scanf("%d",&array[i][j]);        }    }    for(i=1;i<=n;i++)    {        for(j=1;j<=n;j++)        {            temp[j]=0;        }        for(j=i;j<=n;j++)        {            for(k=1;k<=n;k++)            {                temp[k]+=array[j][k];            }            max=max_sum_subseq();            sum=sum>max?sum:max;        }    }    printf("%d\n",sum);    return 0;}


 

原创粉丝点击