To the Max
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 56579 | Accepted: 29921 |
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.
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
ID-OJ:
POJ-1050
author:
Caution_X
DATE of submission:
20,191,117
Tags:
two-dimensional array prefix and
description modelling:
calculating a two-dimensional array of weights and the sub-matrix largest
major steps to solve it:
enumeration i (i∈ [1 , n-]) to j (j∈ [i + 1, n]) for each one-dimensional array of columns and rows of the elements, and the maximum value is calculated and updated answer to the one-dimensional array of continuous sections right
warnings:
a two-dimensional array into a one-dimensional array to handle, reduce the time overhead
AC code:
#include <stdio.h> #define the MAXSIZE 101 // determined row and the largest sub-segment int MaxArray ( int n-, int arr_ []) { int I, sum_ = 0 , max_ = 0 ; for (I = . 1 ; I <= n-; I ++ ) { IF (sum_> 0 ) { sum_ + = arr_ [I]; } the else { sum_ = arr_ [I]; } IF (sum_> max_) { max_ = {sum_; } } return max_; } // find the maximum and the sub-matrix. int MaxMatrix ( int n-, int ARR _ [] [the MAXSIZE]) { int max_ = arr_ [ . 1 ] [ . 1 ]; int sum_; int I, J, K; int temp_arr [the MAXSIZE]; for (I = . 1 ; I < n =; I ++) // starting from the first row, until the n-th row { for (J = . 1 ; J <= n; J ++) // only changes the start line, temp_arr initializing the array before temp_arr [J] = 0 ; } for (J = i; J <= n; J ++) // from the i-th row to the n-th row { for (K = . 1 ; K <= n; K ++ ) { temp_arr [K] + arr_ = [J] [k]; // temp_arr [k] represents the sum of the k-th column of the i-th row to the n-th row. } Sum_ = MaxArray (n-, temp_arr); // determine the largest sub-segment row and IF (sum_> max_) { max_ = sum_; } } } return max_; } int main() { int n; int i, j; int arr_[MAXSIZE][MAXSIZE]; int max_; while (~scanf("%d", &n)) //多组测试。 相当于 scanf("%d", &n) != EOF { for (i=1; i<=n; i++) { for (j=1; j<=n; j++) { scanf("%d", &arr_[i][j]); } } max_ = MaxMatrix(n, arr_); printf("%d\n", max_); } return 0; }