to the Max
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 45915 |
|
Accepted: 24282 |
Description
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 o R greater located within the whole array. The sum of a rectangle is the sum of the "the elements in" that rectangle. The problem the sub-rectangle with the largest sum are 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 in a line by itself, indicating the size of the square two-dimensional a Rray. This was followed by n^2 integers separated by whitespace (spaces and newlines). These is the n^2 integers of the array, presented in Row-major order. That's, all numbers on the first row, left-to-right, then all numbers-second row, left-to-right, etc. N may be as large as 100. The numbers in the array would 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
1 /*This problem n^3 not to time out, violence is good*/2#include <iostream>3 using namespace std;4#include <cstdio>5#define N 1016 intSum[n][n]={0},n,x;7 intMain ()8 {9scanf ("%d",&n);Ten for(inti=1;i<=n;++i) One for(intj=1;j<=n;++j) A { -scanf ("%d",&x); -Sum[i][j]=sum[i][j-1]+x;/*Sum[i][j] Represents the number of J before the first line and*/ the } - intans=-(1<<30); - for(inti=1;i<=n;++i) - for(intJ=I;J<=N;++J)/*violence enumeration per line interval*/ + { - intTmp=0;/*TMP is the size of the current matrix*/ + for(intK=1;K<=N;++K)/*Enumerate each row*/ A { at intQUE=SUM[K][J]-SUM[K][I-1];/*Take out this line .*/ - if(tmp>0) Tmp+=que;/*if the size of the current matrix has been <0, then add to discard the previous matrix, the waiver will certainly be more excellent*/ - Elsetmp=que; -Ans=max (ANS,TMP);/*because we will discard the matrix at any time, so the maximum value is updated in the loop*/ - } - } incout<<ans; - return0; to}
Simple dp+ Violence POJ 1050