首页 > 代码库 > hdu-------1081To The Max

hdu-------1081To The Max

To The Max

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7681    Accepted Submission(s): 3724


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
 

 

Source
Greater New York 2001
这道题个人觉得是到灰常好的题目,适合不同层次的人做,求的是最大连续子矩阵和....
方法一:
简单的模拟即可。首先求出每个阶段只和,然后得到这个状态,然后进行矩阵规划,求最大值即可
代码浅显易懂:
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<iostream>
 5 using namespace std;
 6 int arr[101][101];
 7 int sum[101][101];
 8 int main()
 9 {
10     int nn,i,j,ans,temp;
11    // freopen("test.in","r",stdin);
12     while(scanf("%d",&nn)!=EOF)
13     {
14        ans=0;
15       memset(sum,0,sizeof(sum));
16       for(i=1;i<=nn;i++)
17       {
18           for(j=1;j<=nn;j++)
19           {
20            scanf("%d",&arr[i][j]);
21            sum[i][j]+=arr[i][j]+sum[i][j-1];   //求出每一层逐步之和
22           }
23       }  
24       for(i=2;i<=nn;i++)
25       {
26           for(j=1;j<=nn;j++)
27           {
28             sum[i][j]+=sum[i-1][j];   //在和的基础上,逐步求出最大和
29           }
30       }
31         ans=0;
32     for(int rr=1;rr<=nn;rr++)
33     {
34       for(int cc=1;cc<=nn;cc++)
35       {
36        for(i=rr;i<=nn;i++)
37        {
38          for(j=cc;j<=nn;j++)
39          {
40            temp=sum[i][j]-sum[i][cc-1]-sum[rr-1][j]+sum[rr-1][cc-1];  
41            if(ans<temp)  ans=temp;
42          }
43        }
44       }
45     }
46     printf("%d\n",ans);
47  }
48     return 0;
49 }
View Code

 方法二:

采用状态压缩的方式进行DP求解最大值......!

代码:

 

 1     /*基于一串连续数字进行求解的扩展*/
 2     /*coder Gxjun 1081*/
 3     #include<iostream>
 4     #include<cstdio>
 5     #include<cstring>
 6     #include<cstdlib>
 7     using namespace std;
 8     const int nn=101;
 9     int arr[nn][nn],sum[nn][nn];
10     int main()
11     {
12        int n,i,j,maxc,res,ans;
13        //  freopen("test.in","r",stdin);
14        while(scanf("%d",&n)!=EOF)
15        {
16           memset(sum,0,sizeof(sum));
17          for(i=1;i<=n;i++)
18            for(j=1;j<=n;j++)
19            {
20              scanf("%d",&arr[i][j]);
21              sum[j][i]=sum[j][i-1]+arr[i][j];
22            }
23              ans=0;
24          for(i=1;i<=n;i++){
25 
26            for(j=i;j<=n;j++)
27            {
28                res=maxc=0;
29               for(int k=1;k<=n;k++)
30               {
31                  maxc+=sum[k][j]-sum[k][i-1];
32                 if(maxc<=0)  maxc=0;
33                 else
34                  if(maxc>res) res=maxc;
35               }
36                 if(ans<res)  ans=res;
37            }
38          }
39          printf("%d\n",ans);
40        }
41         return 0;
42     }
View Code