Link:
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_ Problem&problem=44
Original title:
Background
A problem is the simple to solve in one dimension are often much more difficult to solve in the more than one dimension. Consider satisfying a Boolean expression in conjunctive normal form in which each conjunct consists of exactly 3 disjuncts . This problem (3-SAT) is np-complete. The problem 2-sat is solved quite efficiently, however. In contrast, some problems belong to the same complexity class regardless of the dimensionality.
The Problem
Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest sum. The sum of a rectangle is the sum of the elements in that rectangle. In this problem the Sub-rectangle and the largest sum is referred to as the maximal sub-rectangle. A Sub-rectangle is any contiguous sub-array of size or greater located within the whole array. As an example, the maximal sub-rectangle of the array:
is in the Lower-left-hand corner:
and has the sum of 15.
Input and Output
The input consists of an array of integers. The input begins with a single positive integer N in a line by itself indicating the size of the square two dimen Sional Array. This is followed by integers separated by white-space (newlines and spaces). These are integers make up the array in row-major order (i.e., all numbers on the "the", left-to-right, then all numbers o n the second row, left-to-right, etc. N May is as large as 100. The numbers in the range [-127, 127].
The output is 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
The main effect of the topic:
Give a n*n size matrix with a number on each lattice. Find the number of the sum of the numbers and the smallest number in the matrix.
Analysis and Summary:
A classic question. It can also be said to be an upgraded version of the "Max Continuity and" (UVa 507-jill rides Again) O (n) algorithm.
The key to this problem is the conversion, which converts those numbers above the matrix into the "Maximum continuity and".
Look at the picture (the picture is really a little sorry to the audience = =)
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
Think of those squares as numbers, with a red slash shadow representing a 2-degree matrix, we can think of these two layers as a layer, the sum of the upper and lower numbers representing a new number, you can use the "maximum continuous and" O (n) method to find the diagonal shadow part of the largest 2*x of the sum of the matrix.
Requires the largest matrix number of the entire large matrices and, in the case of enumerating the number of layers, need to use two-tier for loops. Plus the linear time scan inside that O (n), the final complexity is O (n^3).
Code:
* * Uva:108-maximum Sum * time:0.012s * author:d_double * */#include <iostream> #include <
cstdio> #include <cstring> #define MAXN 102 using namespace Std;
int ARR[MAXN][MAXN],SUM[MAXN][MAXN];
int mx,sm;
int main () {int t,m,n,x,y;
int i,j;
while (~SCANF ("%d", &n)) {memset (sum,0,sizeof (sum));
memset (arr,0,sizeof (arr));
For (I=1 i<=n; ++i) {for (j=1; j<=n; ++j) {scanf ("%d", &arr[i][j]); SUM[I][J] means that the diagonal vertex is (1,1), (i,j) The sum of all the original numbers within this matrix sum[i][j] = sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+arr[
I][J];
} int maxsum=-2147483645;
for (int i=1; i<=n; ++i) {for (int j=0; j<i; ++j) {int t, min;
min=0; for (int k=1; k<=n; ++k) {//Linear time scan T=sum[i][k]-sum[j][k]-min;
if (t>maxsum) maxsum=t;
if (Sum[i][k]-sum[j][k] < min) min=sum[i][k]-sum[j][k];
} printf ("%d\n", maxsum);
return 0; }
Attached: Another upgrade version of this question UVa 10827-maximum sum on a torus
Author: csdn Blog shuangde800