Pku acm 1050 to the max Dynamic Planning Problem Solving report (16)

Source: Internet
Author: User

Http://acm.pku.edu.cn/JudgeOnline/problem? Id = 1050

The meaning of the question is very simple. Find its sub-matrix in a matrix to maximize the sum of the sub-Matrix Values. In fact, it is the promotion of the largest sub-segment and problem in two-dimensional space. Let's talk about the one-dimensional situation first: It has arrays A0, A1... An. Find the consecutive sub-segments to maximize the sum of them. Assume that for sub-segments: 9 2-16 2 temp [I], it indicates the maximum sub-segment and in the sub-segment ending with AI. If temp [I] is known, the method for obtaining temp [I + 1] is:

If temp [I]> 0 temp [I + 1] = temp [I] + AI (add AI to the previous child segment ), otherwise, temp [I + 1] = AI (without the preceding sub-segment), that is, the state transition equation:

Temp [I] = (temp [I-1]> 0? Temp [I-1]: 0) + Buf [I];

For the example temp: 9 11-5 2, then the largest sub-segment in temp [] is the largest of the one-dimensional sequence. Evaluate the functions of the largest subsegment and in one dimension:

Int getmax (INT Buf [100], int N)

{

Int temp [101], max = N * (-127 );

Memset (temp, 0, 4 * (n + 1 ));

For (INT I = 1; I <= N; I ++)

{

Temp [I] = (temp [I-1]> 0? Temp [I-1]: 0) + Buf [I];

If (max <temp [I])

Max = temp [I];

}

Return Max;

}

The following is an example of two-dimensional scaling:

0-2-7 0

9 2-6 2

-4 1-4 7

-1 8 0-2

We use I j to indicate the start and end rows respectively, and traverse all possibilities:

For (I = 1; I <= N; I ++)

For (j = I; j <= N; j ++ ){}

In one of the cases, I = 2 J = 4, which is equivalent to selecting 2 3 4 and 3 rows, so that the combination of those columns can obtain the maximum value, because it is always 2 3 4 rows, so we can "bundle" these three lines and convert them to 4 ( 9-4-1 ), 11 (8 + 2 + 1),-10 (-6-4 + 0), 7 (7 + 2-2), and OK, the problem is successfully converted to a one-dimensional problem!

Code with detailed comments can be obtained at http://download.csdn.net/user/china8848/

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.