Maximum child arrays and

Source: Internet
Author: User

Maximum child arrays and

Hoj2558, given a matrix, returns the sum of the largest child matrices.

Ideas (Dynamic Planning ):

1. Calculate the part and matrix while reading the Matrix

2. The upper and lower boundary of the enumeration matrix. After the upper and lower boundary of the row is fixed,

According to the Section and matrixO (1)Returns the sum of the elements of the same column in time and converts them to a one-dimensional array.

3. In the case of a one-dimensional array, the idea of finding the maximum sub-array and the sum is:

You can calculate the largest sum starting with the current element A [I ].Start,

AddStartCompare it with the real largest and largest value represented by A [I + 1: n], and save it as the true largest and largest of A [I: n.

4. Maximum value obtained during the output traversal ProcessMAXYou can.

CppCode:

# Include
 
  
# Define SIZE 102 # define INF 1000 using namespace std; int maxx (int a, int B) {return a> B? A: B;} int main () {int n, I, j, k, a, c, tmp, MAX, start, all; int num [SIZE]; int mat [SIZE] [SIZE]; while (cin> n) {// calculate the part and matrix for (j = 0; j <= n; j ++) mat [0] [j] = mat [j] [0] = 0; for (I = 1; I <= n; I ++) {for (j = 1; j <= n; j ++) {cin> mat [I] [j]; mat [I] [j] + = mat [I-1] [j]; // accumulate parts and} // start to process MAX =-INF; for (a = 1; a <= n; a ++) {for (c = a; c <= n; c ++) {// enumerate upper and lower boundary start = mat [c] [n]-mat [A-1] [n]; all = start; // assign a value to the last element of the array for (k = n-1; k> = 1; k --) {// calculate tmp = mat [c] [k]-mat [A-1] [k] From the back; // calculate the current element value start = maxx (tmp + start, tmp) based on the partial part; // compare all = maxx (start, all) Starting with tmp ); // compare the total number} if (all> MAX) MAX = all ;}} cout <
  
   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.