The beauty of programming: 2.14 -- calculate the maximum value of the sum of the array sub-Arrays

Source: Internet
Author: User

Problem:
1. A one-dimensional array composed of N integer elements, and the maximum values of the elements and in all its sub-arrays.
2. if the beginning and end of the array are adjacent, that is, the sub-array A [I],..., A [n-1], A [0],..., A [j] exists, and the total elements and maximum values of all its sub-arrays are obtained.

1. solution:
We can use the idea of dynamic planning to calculate the maximum sum of sub-arrays in O (n) time.
The problem of dynamic planning is often very flexible, so I don't know how to write the questions. In fact, it has a clear analysis method, so it is easier to solve the problem of dynamic planning according to this method. For more information, see IOI2000 Zhang Chen's paper "dynamic planning features and applications".
Phase: In all child arrays ending with element k, select the element and the largest child array, k = 1, 2... n.
State: the largest and ending child arrays with element k are the child arrays that contain the largest and ending with an element k-1 or only the element k, that is, these two optional states.
The reason for selecting such a stage and status is that this selection method has no aftereffect, that is, phase k + 1 (subarray ending with element k + 1) it will only be associated with stage k (sub-array ending with element k), but not with other stages. After obtaining the largest child array ending with each element, the maximum value is the largest child array in all child arrays.
[Cpp]
# Include <iostream>
# Include <algorithm>
Using namespace std;
 
# Define maxn1003
Int A [MAXN];
 
// Dynamic planning, time complexity O (n)
Int Tail [MAXN];
 
Int main ()
{
Int n, I, j, k;
Cin> n;
For (I = 1; I <= n; I ++)
Cin> A [I];
// Calculate the maximum value of the sum of sub-arrays ending with k, that is, the number of sub-arrays containing k
Tail [1] = A [1];
For (k = 2; k <= n; k ++) // k stages
Tail [k] = max (A [k], Tail [k-1] + A [k]); // only two States are available
// The maximum value of the n sub-arrays must end with a certain number.
// It is the largest subarray.
Int All = Tail [1];
For (I = 2; I <= n; I ++)
All = max (All, Tail [I]);
Cout <All;
}
Although the time complexity of this standard dynamic planning method has been optimized, it still occupies O (n) space, which is inevitable for general dynamic planning problems, however, this problem is simple and can be further optimized. We put the operation to take the maximum value of All in the calculation cycle of Tail, as follows:
Tail [1] = A [1];
All = Tail [1];
For (k = 2; k <= n; k ++) // k stages
{
Tail [k] = max (A [k], Tail [k-1] + A [k]); // only two States are available
All = max (All, Tail [k]);
}
Because the loop body only cares about the current Tail [k] and the previous Tail [k-1], you can save the previous calculated Tail [1], Tail [2]... the data for Tail [K-2] is as follows:
Tail = A [1];
All = Tail;
For (k = 2; k <= n; k ++) // k stages
{
Tail = max (A [k], Tail + A [k]); // there are only two statuses
All = max (All, Tail );
}
The final optimized code is the final result in the book.

2. solution:
There are two problems:
(1) The largest and subarrays do not span from A [n] to A [1] (for example, question 1)
(2) The largest and subarrays span from A [n] to A [1].
For Case (2), the largest and sub-arrays contain two parts: the largest and sub-arrays starting with A [1, and the largest and sub-arrays ending with A [n], and the two sub-arrays do not allow overlap, then the combination of the two sub-arrays is the solution of case (2.
[Cpp]
# Include <iostream>
# Include <algorithm>
Using namespace std;
 
# Define maxn1003
Int A [MAXN];
 
Int main ()
{
Int n, I, j, k;
Cin> n;
For (I = 1; I <= n; I ++)
Cin> A [I];
// And the largest subarray does not span A [n] And A [1]
Int Tail = A [1];
Int All = Tail;
For (k = 2; k <= n; k ++) // k stages
{
Tail = max (A [k], Tail + A [k]); // there are only two statuses
All = max (All, Tail );
}
// And the largest sub-array spans A [n] And A [1]
Int Start = A [1];
For (I = 2; I <= n & Start + A [I]> Start; I ++)
Start + = A [I];
Tail = A [n];
For (j = n-1; j> = 1 & Tail + A [j]> Tail; j --)
Tail + = A [j];
If (I <j & Start + Tail> All)
All = Start + Tail;
Cout <All;
}

Author: linyunzju

 


Related Article

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.