Link:
Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 113 & page = show_problem & problem = 44
Original question:
Background
A problem that is simple to solve in one dimension is often much more difficult to solve in 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 of the problem.
The Problem
Given a 2-dimenstmarray of positive and negative integers, find the sub-rectangle with the largest sum. 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 asMaximal 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 IntegerNOn a line by itself indicating the size of the square two dimen1_array. This is followed by Integers
Separated by white-space (newlines and spaces). These integers make up the array in row-Major Order (I. e., all numbers on the first
Row, left-to-right, then all numbers on the second row, left-to-right, etc .).NMay be as large as 100. The numbers in the array will be in the range [-127,127].
The output is 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
Question:
A matrix of N * n is given. Each grid has a number. Evaluate the minimum number of the numbers in the submatrix.
Analysis and Summary:
A classic question. It can also be said that the question "Maximum continuity and"
507-upgraded version of Jill rides again) O (n) algorithm.
The key to this question is to convert the numbers above the matrix into "Maximum continuity and".
See (I'm sorry for the audience picture =)
Think of those grids as numbers, and the red diagonal lines shadow on them represent a child matrix with a height of 2. We can think of these two layers as one layer, and the sum of the upper and lower layers of numbers represents a new number, you can use the O (n) method of "Maximum continuity and" to obtain the sum of the two X matrices of the maximum diagonal line shadow.
The maximum submatrix number and of the entire large matrix are required. When the number of layers needs to be enumerated, a two-layer for loop is required. With the linear time scanning of O (n), the final complexity is O (n ^ 3 ).
Code:
/** Ultraviolet A: 108-maximum sum * Time: 0.012 S * 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] indicates that the diagonal vertex is (1, 1), (I, j) 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 ;}
Appendix: This is another upgraded version of the image below.
10827-maximum sum on a Torus
-- The meaning of life is to give it meaning.
Original
Http://blog.csdn.net/shuangde800
,
D_double (reprinted please mark)