杭电1081--二维最大子序列

来源:互联网 发布:中国债务危机 知乎 编辑:程序博客网 时间:2024/06/08 08:54
                            To The Max

Problem Description
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1 x 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 x 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

分析:

给你一个矩阵,求它子矩阵和的最大值EX:  0 -2  -7  0  9  2  -6  2 -4  1  -4  1 -1  8   0 -2则其最大和的子矩阵为:  9  2 -4  1 -1  8其和为15。这题的思想是求最大连续子串和的思想(杭电 1003 MAX SUM),不过这题是2维的,我们的可以将它转换为一维,然后再运用该思想求它的最大值!子矩阵必定也是由行和列组成,如上这个矩阵,它的行的组合有 1,1-2,1-3,1-4,2,2-3,2-4,3,3-4,4,无非这10种组合,这样,我们就可以将行进行压缩,比如说:1-2,我们将1,2行数据进行压缩,及进行合并   0 -2 -7 0 + 9  2 -6 2-------------  9 0  -13 2那么这样我们就可以通过求最大连续子串和的思想求其最大值,为9再看:2-4这个组合     9 2 -6  2    -4 1 -4  1 +  -1 8  0 -2----------------    4  11 -10 1所以它的最大和为15这样通过压缩行,即将这个矩形的宽变为1,可以迅速的求出子矩阵的值,并求出最大值,因为求最大连续子串和思想是线性的,复杂度为O(N),所以可以快速求出压缩后矩阵的和!

代码1:

 include <iostream># include <cstdio># include <cstring>int dp[1009][1009],a[1009][1009];int main(){    int n,m;    int i,j,k;    while(scanf("%d",&n)!=EOF){    for(i=0;i<n;i++)        for(j=0;j<n;j++)            scanf("%d",&a[i][j]);    memset(dp,0,sizeof(dp));    //注意dp的行列与a的行列发生转置    for(j=0;j<n;j++)//控制dp的列   //这两层之后得到的dp转置了,记住         for(i=0;i<n;i++)//控制dp的行            if(i==0)                dp[j][i] = a[i][j];            else                dp[j][i] = dp[j][i-1]+a[i][j]; /*    for(j=0;j<n;j++){        for(i=0;i<n;i++)            printf("%d  ",dp[j][i]);        printf("\n");     } */    int rise = 0;    //注意这里通过i1,i2可以遍历出所有的情况    for(int i1=0;i1<n;i1++){//i1,i2控制行        for(int i2=i1;i2<n;i2++){//这两层的目的是得到多余的组合             int max=0,sum=0;            for(j=0;j<n;j++){//这里相当于是dp的某一行                    if(i1==0)                        sum+=dp[j][i2];                    else                        sum+=dp[j][i2] - dp[j][i1-1];                 if(sum>=max)                    max = sum;                if(sum<0)                    sum = 0;            }            if(max>=rise)                rise = max;         }    }    printf("%d\n",rise);        }             return 0;}

方法2:

# include<stdio.h># include<string.h>int dp[105][105];int map[105][105];int main(){    int i,j,n,i1,i2,sum,temp,max,res;    while(scanf("%d",&n)!=EOF)    {      for(i=1;i<=n;i++)       for(j=1;j<=n;j++)          scanf("%d",&map[i][j]);       memset(dp,0,sizeof(dp));       for(j=1;j<=n;j++)             //控制列         for(i=1;i<=n;i++)          //控制行            dp[j][i]=dp[j][i-1]+map[i][j];   //dp[j][i]代表第j列从第1行开始的数累加到到第i行的和       res=0;       for(i1=1;i1<=n;i1++)           //i1,i2控制行的组合         for(i2=i1;i2<=n;i2++)         {            max=sum=0;           for(j=1;j<=n;j++)      //j控制在该行组合下的列           {              sum+=dp[j][i2]-dp[j][i1-1]; //表示第 j 列从第i1到i2行数字之和                if(sum>=0)    //下面即为求最大连续子串和思想                                  {               if(sum>=max)                  max=sum;             }             else                 sum=0;            }            if(max>=res)               res=max;         }      printf("%d\n",res);    }    return 0;}
0 0