Pku acm 1050 to the Max [Dynamic Planning]

Source: Internet
Author: User

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 [I + 1] method 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, therefore, we can bind these three lines to 4 (9-4-1), 11 (8 + 2 + 1 ), -10 (-6-4 + 0), 7 (7 + 2-2) Maximum child segment and, OK, the problem is successfully converted to one-dimensional situation!

// Acm pku 1050 to the max by suqiang @ neuq & jlu // For more detailed Problem Solving reports please visit my blog: http://blog.csdn.net/china8848#include <stdio. h> # include <memory. h> // calculate the maximum sub-segment of the one-dimensional array and 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;} int main (void) {int N, num [101] [101], I, J, K, Max, temp [101]; scanf ("% d", & N ); for (I = 1; I <= N; I ++) for (j = 1; j <= N; j ++) scanf ("% d ", & num [I] [J]); max =-127 * n; for (I = 1; I <= N; I ++) {memset (temp, 0, 4 * (n + 1); For (j = I; j <= N; j ++) {for (k = 1; k <= N; k ++) {temp [k] + = num [J] [k];} // convert two-dimensional data to one-dimensional int this_max = getmax (temp, n); If (this_max> MAX) max = this_max ;}} printf ("% d/N", max); return 1 ;} 

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.