51nod Tutorial Max Sub-segments and issues (dynamic planning)

Source: Internet
Author: User


Give an array of integers a (positive and negative numbers have), how to find a continuous sub-array (can not take one, then the result is 0), making the and maximum.

For example: -2,11,-4,13,-5,-2, and the largest subkey: 11,-4,13. and for 20.

Give an array of integers a (positive and negative numbers have), how to find a continuous sub-array (can not take one, then the result is 0), making the and maximum.

For example: -2,11,-4,13,-5,-2, and the largest subkey: 11,-4,13. and for 20.



The first reaction you see in this question is what algorithm to use.

(1) enumeration. Yes, enumerations are omnipotent. Enumerate what. The position of the sub-array. Well enumerate an opening position I, an end position j>=i, and then seek a[i. J] The number of the and, find the biggest is OK. Okay, TIME's complicated.
(1.1) enumeration I,o (n)
(1.2) enumeration J,o (n)
(1.3) Summation a[i. J],o (N)
Presumably this is a calculation method: 1 2 3 4 5 6 7 8 9 for (int i = 1;  I <= N;  i + +) {for (int j = i;  J <= N;          J + +) {int sum = 0;  for (int k = i;  K <= J;                        K + +) sum + = a [K];      max = max (max, sum); }} So it is O (n^3), the complexity is too high. Try it down a bit. (2) is still an enumeration. Can be computed at the same time as the enumeration.
(2.1) enumeration I,o (n)
(2. 2) Enumerate J,o (n), here we find a[i. J] 's and not a[i. J–1] and add a[j]. So we're here when J adds 1 to add a[j] to the previous results. Right. So we effortlessly reduce complexity and get a faster algorithm with a new time complexity of O (n^2).
That's about it. Code: 1 2 3 4 5 6 7 8 9 for (int i = 1;  I <= N;           i + +) {int sum = 0;  for (int j = i;  J <= N;          J + +) {sum + = a [j];      max = max (max, sum); }} is not the limit. Far more than that.
(3) Divide and conquer.
We cut the array from the middle, the largest sub-segments of the original array and either the largest sub-segments and (min) of the two sub-arrays, or the maximal sub-segments and (Hopewell) that span the center boundary point. So how to calculate the maximal sub-segment of the crossing point of the center. is still an enumeration. From the center point to the left to find out where to get the largest, and then from the center point to the right to check where to get the largest sub-segment, add up on it. We can see that the original problem is difficult because we do not know where the sub-array starts, where it ends, there is no "focus", with the central position of the "focus", we could easily find the maximum sub-segments and by the loop linear time.
Then the algorithm becomes
(3.1) Splitting the numerator array for the maximal sub-segments and sum1 of the array of approximately half the length, sum2
Time complexity of T (N/2)
(3.2) To find the maximum and the maximum and sum3 time complexity O (n) from the center point to each side respectively.
So the overall time complexity is T (n) = 2 * t (N/2) + O (n) = O (Nlogn), and it's a big step, isn't it.
Can you optimize it. Think again, don't give up.
We need a "focal point" in the solution (3) To achieve the time complexity of the sub-Problem of O (n), and in the solution (2) easily with the previous and add a new element to get the current and, then "before and" is so important. If the previous and is negative. It's obviously useless. We're going to have to start again from the current element.
Think again, if I want to choose A[j], then "before and" must be the largest and is positive. Otherwise, I'll change the "before and" to better, or I directly from the a[j], not better.
Dynamic planning. We record Dp[i] represents the largest and most of all the sub-segments ending in a[i]. Let's see what we just thought, I can not take a[i–1], if take a[i–1] then must be taken to a[i–1] end of the sub-paragraph and the largest one, so is dp[i–1]. What if you don't take dp[i–1]? Then I'll just take a[i] and be all alone. Note that the definition of dp[i] must be taken a[i]. Then I'll either take a[i–1] or not take a[i-1]. That kind of situation is good for dp[i]. Obviously take the biggest. So we have dp[i] = max (dp[i–1] + a[i], a[i]) in fact it and dp[i] = max (dp[i–1], 0) + A[i] is the same, meaning to say before the largest and is positive I will, otherwise I will not. What is the initial value. The initial value is dp[1] = a[1], because there is no previous choice.
So what's the result. We have to take the largest sub-paragraph and must end with some a[i]. So the result is Max (Dp[i]).
In this way, our time complexity is O (n), and the spatial complexity is O (n)-because we want to record the array of DP.
is the algorithm optimal? It's like. can also be optimized. We notice that dp[i] = max (dp[i-1], 0) + a[i], see it is only related to dp[i–1], why should we record it all down? To find the maximum value for all Dp[i]. No, the maximum value we can also ask for a comparison.
We define Endmax to represent the maximum number of sub-segments at the end of the current element and, when adding a[i], we have endmax ' = max (Endmax, 0) + a[i], and then by the way the maximum is recorded.

The pseudo code is as follows; (array subscript starting from 1) 1 2 3 4 5

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.