Link: http://poj.org/problem? Id = 1050 http://acm.hust.edu.cn/vjudge/contest/view.action? Cid = 28508 # Problem/BTO the max
| Time limit:1000 ms |
|
Memory limit:10000 K |
| Total submissions:36968 |
|
Accepted:19480 |
Description
Given a two-dimenstmarray 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
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-dimen=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
40 -2 -7 0 9 2 -6 2-4 1 -4 1 -18 0 -2
Sample output
15
Source
Greater New York 2001
Question:
Returns the maximum continuous matrix and
Algorithm: DP [always greedy orz] idea: start
HDU 1003 Max sum [maximum continuous sum of one-dimensional arrays]. Later, I learned this idea after reading other people's ideas.
Solve the maximum continuity and problem of each row first: (this is equivalent to HDU 1003)
Number: A [0] ---- A [n-1]
State transition equation: DP [0] = A [0]
DP [I] = max (DP [I-1], 0) + A [I]
Finally, traverse DP [] to find the maximum DP [I], which is the maximum continuous sum of the number of this row.
Analysis of the state transition equation example:
A []: 6-1 3-10 8 7
DP []: 6 5 8 0 8 15
For this question:
You only need to enumerate by row in sequence, compress the required matrix into a row, and then use the above method.
For the first line:
Obtain the maximum continuous sum of the first row and update the maximum value.
Add the number of the second row to the first row, calculate the maximum continuous sum of the first row, and update the maximum value.
Add the number of the third row to the first row, calculate the maximum continuous sum, and update the max
.
.
.
Add the number of rows n to the first line...
At this time, the array of the first row is changed, and the following remains unchanged.
For the second row: Find the maximum continuous sum of the second row and update the maximum value.
Add the number of the third row to the second row, find the maximum continuous sum of the second row, and update the maximum value.
.
.
.
Add the number of rows n to the second row, calculate the maximum continuous sum of the second row, and update the maximum value.
...
For row N: calculate the maximum continuous sum of row N and update Max
Code:
/*************************************** * ****************** Baccepted192 kb16 MSC ++ 1113 B: find the maximum continuous matrix and algorithm: DP recommended first: HDU 1003 Max sum to find the maximum continuity and ideas: Start With HDU 1003, later, after reading other people's ideas on this topic, I learned how to first solve the maximum continuity and problem of each row: (this is equivalent to HDU 1003) Number: A [0] ---- A [n-1] state transition equation: DP [0] = A [0] DP [I] = max (DP [I-1], 0) + A [I] Finally, traverse DP [] to find the maximum DP [I], which is the maximum continuous and state transition equation of this row. Example Analysis: A []: 6-1 3-10 8 7 DP []: 6 5 8 0 8 15 then for this question: as long as you enumerate by row, compress the required matrix into a row [add] and then apply the above For the first row: Find the maximum continuous sum of the first row, update the maximum value to add the number of the second row to the first row, and find the maximum continuous sum of the first row, update the maximum value to add the number of the third row to the first row, calculate the maximum continuous sum, and update the max... Add the number of rows n to the first line... At this time, the array of the first row is changed, and the following will not change for the second row: Find the maximum continuous sum of the second row, update the maximum value, and add the number of the third row to the second row, find the maximum continuous sum of the second row and update the maximum value... Add the number of rows n to the second row, calculate the maximum continuous sum of the second row, and update the maximum value... For row N: returns the maximum continuous sum of row n, update Max ************************************** ***********************************/# include <stdio. h> # include <string. h >#include <algorithm> # include <iostream> using namespace STD; const int maxn = 110; int A [maxn] [maxn]; int DP [maxn]; int max_sum (int A [], int N) // obtain the maximum continuity of array a [] and {DP [0] = A [0]; for (INT I = 1; I <n; I ++) DP [I] = max (DP [I-1], 0) + A [I]; int max = DP [0]; for (INT I = 1; I <n; I ++) max = max (max, DP [I]); Return Max ;}int main () {int N; while (scanf ("% d ", & N )! = EOF) {for (INT I = 0; I <n; I ++) for (Int J = 0; j <n; j ++) scanf ("% d", & A [I] [J]); int max = 0; For (INT I = 0; I <n; I ++) // traverse each row {int M = max_sum (A [I], n); max = max (max, m); For (Int J = I + 1; j <n; j ++) // Add the number of Row J to row I, and then calculate the maximum matrix and [I, I + 1 ,... j] {for (int K = 0; k <n; k ++) {A [I] [k] + = A [J] [k]; // The row I is changed without affecting the result} M = max_sum (A [I], n); max = max (max, m );}} printf ("% d \ n", max);} return 0 ;} /* 40-2-7 09 2-6 2-4 1-4 1-1 8 0-2 */