POJ_1050_To the Max

2018-06-17 21:47:08来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

To the Max
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 49811   Accepted: 26400

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

Greater New York 2001
 
  • 最大子矩阵和
  • 是一维的最大子串和的二维扩展
  • 那我们把每列做一个前缀和,O1得到从i行到j行单列的和,之后用最大子串和dp求解就行

 

 1 #include <iostream>
 2 #include <string>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <cmath>
 8 #include <vector>
 9 #include <queue>
10 #include <stack>
11 #include <set>
12 #include <map>
13 using namespace std;
14 typedef long long           LL ;
15 typedef unsigned long long ULL ;
16 const int    maxn = 1e2 + 10   ;
17 const int    inf  = 0x3f3f3f3f ;
18 const int    npos = -1         ;
19 const int    mod  = 1e9 + 7    ;
20 const int    mxx  = 100 + 5    ;
21 const double eps  = 1e-6       ;
22 const double PI   = acos(-1.0) ;
23 
24 int n, ans, a[maxn][maxn], b[maxn], c[maxn][maxn];
25 int main(){
26     // freopen("in.txt","r",stdin);
27     // freopen("out.txt","w",stdout);
28     while(~scanf("%d",&n)){
29         ans=-inf;
30         memset(c,0,sizeof(c));
31         for(int i=1;i<=n;i++)
32             for(int j=1;j<=n;j++){
33                 scanf("%d",&a[i][j]);
34                 c[i][j]=c[i-1][j]+a[i][j];
35             }
36         for(int i=1;i<=n;i++){
37             for(int j=i;j<=n;j++){
38                 b[0]=0;
39                 for(int k=1;k<=n;k++){
40                     if(b[k-1]>=0){
41                         b[k]=b[k-1]+c[j][k]-c[i-1][k];
42                     }else{
43                         b[k]=c[j][k]-c[i-1][k];
44                     }
45                     ans=max(ans,b[k]);
46                 }
47             }
48         }
49         printf("%d\n",ans);
50     }
51     return 0;
52 }

 

 

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:C++中setiosflags()的用法

下一篇:【luogu 3805】【模板】manacher算法