Maximum subsequence and (continuous): Maximum Sequence

Source: Internet
Author: User

Maximum subsequence and (continuous): Maximum Sequence

// Maximum subsequence and (continuous): // http://my.oschina.net/itblog/blog/267860#include <iostream> using namespace std; int MaxSum (int * a, int n) {int sum = 0; int max = 0; // the first element of the largest subsequence cannot be a negative number, // It is impossible to include subsequences with the sum of 0 or negative numbers as the prefix. // This avoids multiple considerations for the same element for (int I = 0; I <n; I ++) {sum + = a [I]; if (sum> max) max = sum; else if (sum <0) sum = 0;} return sum ;} int main () {int a [] = {-1,-2,-3,-4}; // test case cout where all values are negative <MaxSum (a, 4) <endl; int B [10] = {1 ,- 2, 3, 10,-4, 7, 2,-5}; cout <MaxSum (B, 8) <endl; system ("pause "); return 0;}/* For example, array: 1,-2, 3, 10,-4, 7, 2, -5 the maximum subsequence is 13. One is the violent enumeration O (n ^ 3). Two for loops determine the boundary, and the third for loop traverses and sums up and compares. For (I = 0, I --- n) for (j = I, j --- n) for (k = I, k --- n) sum + = s [k] A traversal O (n ^ 2): In the second for loop, j moves one side and adds and then compares. For (I = 0, I --- n) for (j = I, j --- n) sum + = s [j] one is to use DP for consideration, the largest subsequence is either in the left half, or in the right half, or across the Left and Right O (nlogn ). One is linear, such as O (n )*/



// Divide and conquer method: // Let's look at recursion and binary # include <iostream> using namespace std; // calculate the maximum number of three int max3 (int I, int j, int k) {if (I> = j & I> = k) return I; return max3 (j, k, I);} int maxsequence2 (int a [], int l, int u) {if (l> u) return 0; if (l = u) return a [l]; int m = (l + u)/2; // calculate the left half part of the largest continuous subsequence across the Left and Right int lmax = a [m], lsum = 0; // This cycle is the maximum value of this sequence, add all elements for (int I = m; I> = l; I --) {lsum + = a [I]; if (lsum> lmax) lmax = lsum ;} // calculate the cross Int rmax = a [m + 1], rsum = 0; for (int I = m + 1; I <= u; I ++) {rsum + = a [I]; if (rsum> rmax) rmax = rsum;} // if the maximum subsequence spans the left and right half sides, that is, the sum of lmax on the left half side and rmax on the right half side. Return max3 (lmax + rmax, maxsequence2 (a, l, m), maxsequence2 (a, m + 1, u);} int main () {// int a [] = {-1,-2,-3,-4}; // test case where all values are negative. // cout <MaxSum (a, 4) <endl; int a [10] = {1,-2, 3, 10,-4, 7, 2,-5}; cout <maxsequence2 (a, 0, 8) <endl; system ("pause"); return 0 ;}
 

 

 

 

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.