Most important sub-sections and problems in Dynamic Planning

Source: Internet
Author: User

Problem description:
Given a sequence of A1, A2,..., and an consisting of n integers (including negative integers), calculate the maximum value of the subsegment and.

When All integers are negative, the maximum sub-segment is defined as 0.
According to this definition, the optimal value is:
 
For example, when (A1, A2, A3, A4, A5, A6) = (-2, 11,-4,-5,-2,

The maximum sub-segment is:

11 + (-4) + 13 = 20
1. The maximum sub-segment and the simple problemAlgorithm:

Code:

# Include <iostream> <br/> using namespace STD; <br/> int maxsum (int A [], int N, Int & besti, Int & bestj) {<br/> int sum = 0; <br/> int I, j, k; <br/> for (I = 1; I <= N; I ++) <br/> for (j = I; j <= N; j ++) <br/>{< br/> int thissum = 0; <br/> for (k = I; k <= J; k ++) thissum + = A [k]; <br/> If (thissum> sum) {<br/> sum = thissum; <br/> besti = I; <br/> bestj = J; <br/>}< br/> return sum; <br/>}< br/> int main () {<br/> int N, A [100], M, I, j, maxsum; <br/> cout <"Enter the number of elements in the integer sequence N:" <Endl; <br/> CIN> N; <br/> cout <"Enter the value of each element in the sequence a [I] (total" <n <) "<Endl; <br/> // For (M = 1; m <= N; I ++) <br/> // CIN> A [m]; <br/> for (m = 0; m <n; m ++) <br/> CIN> A [m]; <br/> int B [100]; <br/> for (m = 0; m <n; m ++) <br/> B [M + 1] = A [m]; <br/> maxsum = maxsum (B, n, I, j); <br/> cout <"the maximum sub-segments of an integer sequence and are: "<maxsum <Endl; <br/> cout <" besti = "<I <Endl; <br/> cout <"bestj =" <j <Endl; <br/> system ("pause"); <br/>}

The time complexity of this algorithm: O (N3 ).

This algorithm can be improved to reduce the time complexity to O (n2 ).

Code:

# Include <iostream> <br/> using namespace STD; <br/> int maxsum (int A [], int N, Int & besti, Int & bestj) {<br/> int sum = 0; <br/> int I, j, k; <br/> for (I = 1; I <= N; I ++) {<br/> int thissum = 0; <br/> for (j = I; j <= N; j ++) <br/>{< br/> thissum + = A [J]; <br/> If (thissum> sum) {<br/> sum = thissum; <br/> besti = I; <br/> bestj = J; <br/>}< br/> return sum; <br/>}< br/> int main () {<br/> int n, a [100], M, I, j, maxsum; <br/> cout <"Enter the number of elements in the integer sequence N:" <Endl; <br/> CIN> N; <br/> cout <"Enter the value of each element in the sequence a [I] (total" <n <")" <Endl; <br/> // For (M = 1; m <= N; I ++) <br/>/CIN> A [m]; <br/> for (m = 0; m <n; m ++) <br/> CIN> A [m]; <br/> int B [100]; <br/> for (m = 0; m <n; m ++) <br/> B [M + 1] = A [m]; <br/> maxsum = maxsum (B, n, I, j); <br/> cout <"the maximum sub-segments of an integer sequence and are: "<maxsum <Endl; <br/> cout <" besti = "<I <Endl; <br/> cout <"bestj =" <j <Endl; <br/> system ("pause"); <br/>}< br/>

 

2. Maximum sub-section and issue division and Control Law:

Code:

// Maximum sub-segment sum and divide the algorithm. T (n) = O (nlog (n )).

# Include <iostream> <br/> using namespace STD; <br/> int maxsubsum (int A [], int left, int right) {<br/> int sum = 0; <br/> If (Left = right) sum = A [left]> 0? A [left]: 0; <br/> else {<br/> int center = (left + right)/2; <br/> int leftsum = maxsubsum (, left, center); <br/> int rightsum = maxsubsum (A, center + 1, right); <br/> int S1 = 0; <br/> int lefts = 0; <br/> for (INT I = center; I >= left; I --) {<br/> lefts + = A [I]; <br/> If (lefts> S1) S1 = lefts; <br/>}< br/> int S2 = 0; <br/> int rights = 0; <br/> for (INT I = center + 1; I <= right; I ++) {<br/> rights + = A [I]; <br/> If (rights> S2) S2 = rights; <br/>}< br/> sum = S1 + S2; <br/> If (sum <leftsum) sum = leftsum; <br/> If (sum <rightsum) sum = rightsum; <br/>}< br/> return sum; <br/>}< br/> int main () {<br/> int N, A [100], M, maxsum; <br/> cout <"Enter the number of elements in the integer sequence N:" <Endl; <br/> CIN> N; <br/> cout <"Enter the value of each element in the sequence a [I] (total" <n <")" <Endl; <br/> for (m = 0; m <n; m ++) <br/> CIN> A [m]; <br/> int B [100]; <br/> for (m = 0; m <n; m ++) <br/> B [M + 1] = A [m]; <br/> maxsum = maxsubsum (B, 1, n); <br/> cout <"maximum sub-segment of an integer sequence and" <maxsum <Endl; <br/> system ("pause"); <br/>}< br/>

 

3. Dynamic Planning Algorithm for maximum sub-segments and problems:

Code:

// Maximum child segment sum, dynamic planning, T (n) = O (n ).

# include <iostream> <br/> using namespace STD; <br/> int maxsum (int n, int A []) {<br/> int sum = 0; <br/> int B = 0; <br/> for (INT I = 1; I <= N; I ++) {<br/> If (B> 0) B + = A [I]; <br/> else B = A [I]; <br/> If (B> sum) sum = B; <br/>}< br/> return sum; <br/>}< br/> int main () {<br/> int n, a [100], M, maxsum; <br/> cout <"Enter the number of elements in the integer sequence N:" <Endl; <br/> CIN> N; <br/> cout <"Enter the value of each element in the sequence a [I] (total" <n <) "<Endl; <br/> for (m = 0; m <n; m ++) <br/> CIN> A [m]; <br/> int B [100]; <br/> for (m = 0; m <n; m ++) <br/> B [M + 1] = A [m]; <br/> maxsum = maxsum (n, B ); <br/> cout <"maximum sub-segment of the integer sequence and" <maxsum <Endl; <br/> system ("pause "); <br/>}< br/>

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.