1. 程式人生 > >ZOJ1074 最大和子矩陣 DP最大連續子序列

ZOJ1074 最大和子矩陣 DP最大連續子序列



To the Max

Time Limit:1 Second     Memory Limit: 32768 KB

Problem

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.

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.


Example

Input

4
0 -2 -7 0 9 2 -6 2
-4 1 -4 1 -1
8 0 -2

Output

15 

最多100行100列,對每個舉行都要考慮到2500*2500,

當然計算每個矩形的元素和之前要預處理

令每行的[j,k}]素之和為a區域,s[i][j][k]表示從第一行到第i行的a區域之和,

所有對於行從[i,k],列從[p,q]的矩形,其元素和=s[k][p][q]-s[i-1][p][q];

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<utility>
//#pragma comment(linker, "/STACK:102400000,102400000")
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   num<<1,l,mid
#define rson    num<<1|1,mid+1,r
#define MID   int mid=(l+r)>>1
#define zero(x)((x>0? x:-x)<1e-15)

using namespace std;
const int maxn=  100+10  ;
//const int maxm=    ;
//const int INF=    ;
//typedef long long ll;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
int a[maxn][maxn];
int s[maxn][maxn][maxn];
int row[maxn][maxn];
inline int ReadInt()
{
    int flag=0;
    char ch = getchar();
    int data = 0;
    while (ch < '0' || ch > '9')
    {
        if(ch=='-') flag=1;
        ch = getchar();
    }
    do
    {
        data = data*10 + ch-'0';
        ch = getchar();
    }while (ch >= '0' && ch <= '9');
        if(flag) data=-data;
        return data;
}

int main()
{
      int i,j;int n;
    while(~scanf("%d",&n))
{
    memset(row,0,sizeof row);
      FOR1(i,n)
      {
          FOR1(j,n)
          {
              a[i][j]=ReadInt();
              row[i][j]=row[i][j-1]+a[i][j];
          }
      }
      memset(s,0,sizeof s);


      for(int i=1;i<=n;i++)
      {

          for(int j=1;j<=n;j++)
          {
              for(int k=j+1;k<=n;k++)
              {
                  s[i][j][k]=s[i-1][j][k]+row[i][k]-row[i][j-1];
              }
          }
      }


      int maxi=-INF;
      for(int i=1;i<=n;i++)
      {
          for(int k=i+1;k<=n;k++)
          {

              for(int p=1;p<=n;p++)
              {
                  for(int q=p+1;q<=n;q++)
                  {

                    maxi=max(maxi,s[k][p][q]-s[i-1][p][q]);


                  }
              }


          }
      }
    printf("%d\n",maxi);





  }
  return 0;
}
/*
4
0 -2 -7 0
 9 2 -6 2
-4 1 -4 1
-1 8 0 -2
*/

 

後來發現,更好的辦法是利用最大連續子序列,對第從第i行到第j行的矩形,其長度用最大連續子序列來求

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<utility>
//#pragma comment(linker, "/STACK:102400000,102400000")
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   num<<1,l,mid
#define rson    num<<1|1,mid+1,r
#define MID   int mid=(l+r)>>1
#define zero(x)((x>0? x:-x)<1e-15)

using namespace std;
const int maxn=  100+20  ;
//const int maxm=    ;
//const int INF=    ;
//typedef long long ll;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
int n;
int col[maxn][maxn];
int a[maxn][maxn];
int main()
{
      int i,j;
      int n;
      while(~scanf("%d",&n))
      {
          for(int i=1;i<=n;i++)
          {
              col[i][0]=0;

          }
          FOR1(i,n)
          FOR1(j,n)
          {
              scanf("%d",&a[i][j]);
              col[j][i]=col[j][i-1]+a[i][j];
          }
          int maxi=-INF;
          for(int i=1;i<=n;i++)
          {
              for(int j=i+1;j<=n;j++)
              {
                  int sum=0;
                  for(int k=1;k<=n;k++)
                  {
                      int t=col[k][j]-col[k][i-1];
                      if(sum+t>=t)  sum+=t;
                      else sum=t;
                      maxi=max(sum,maxi);


                  }

              }
          }
          printf("%d\n",maxi);
      }




    return 0;
}