ACM: recursion and sub-governance, maximum continuous sum, O (N3), O (n2), O (nlogn), O (n) algorithms.

Source: Internet
Author: User
Tags cmath

Question: Find a continuous array, maximum continuous sum.

(1) O (N3) algorithm:

Using the method of exhaustion, the efficiency of this method is the worst.

The Code is as follows:

#include <iostream>#include <cstdlib>#include <ctime>#include <cmath>using namespace std;const int MAXN = 1000;int A[MAXN], n;int maxsum(int *A, int n) {int beat = A[0];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];}if(sum > best) best = sum;}}return best;}int main() {cin >> n;srand(time(NULL));for(int i = 1; i <= n; ++i) {A[i] = rand();}for(int i = 1; i <= n; ++i) cout << A[i] << endl; int maxSumAns = maxsum(A, n);cout << maxSumAns << endl;return 0;}

(2) O (n2) algorithm:

Analysis: Suppose Si = A1 + A2 +... + AI, then ai + AI + 1 +... + AJ = Sj-Si-1. This formula indicates that the sum of consecutive subsequences is equal to the difference between two prefixes.

With this idea, we can remove the innermost loop in the O (N3) algorithm!

The Code is as follows:

#include <iostream>#include <cstdlib>#include <ctime>#include <cmath>using namespace std;const int MAXN = 1000;int A[MAXN], S[MAXN], n;int maxsum(int *A, int *S, int n) {S[0] = 0;int best = A[1];for(int i = 1; i <= n; ++i) S[i] = S[i-1] + A[i];for(int i = 1; i <= n; ++i) {for(int j = i; j <= n; ++j) {best = max(best, S[j] - S[i-1]);}}return best;}int main() {cin >> n;srand(time(NULL));for(int i = 1; i <= n; ++i) {A[i] = rand();}for(int i = 1; i <= n; ++i) cout << A[i] << endl; int maxSumAns = maxsum(A, S, n);cout << maxSumAns << endl;return 0;}



(3) O (nlogn) algorithm:

Analysis: Divide and conquer:

(1) division problem: divide the problematic instances into sub-problems.

(2) recursive solution: recursion solves subproblems.

(3) Merge problem: Solve the merge subproblem to obtain the original problem.

This method is used in this example:

(1) division problem: divide the sequence into two halves with equal numbers of elements;

(2) recursive solution: Find the optimal sequence that is completely located in the left half or completely located in the right half;

(3) Merge problem: Find the maximum continuous sequence with the left half of the starting point and the right half of the end point, and compare it with the optimal solution of the subproblem.

The solution here is a little special in the process of merging the problem: Since the starting point is in the left half and the ending point is in the right half, we can artificially divide the sequence into two parts and then solve it independently: first find the best starting point, and then find the best ending point.


The Code is as follows:

# Include <iostream> # include <cstdlib> # include <ctime> # include <cmath> using namespace STD; const int maxn = 1000; int A [maxn], N; int maxsum (int * a, int X, int y) {// returns an array in the left-closed and right-open interval [x, y) returns the maximum continuity and if (Y-x = 1) return a [X]; // returns int M = x + (Y-x)/2 directly; // divide the first step into [x, m) and [M, Y ). Int maxans = max (maxsum (A, X, m), maxsum (a, m, y); // the second part of sub-governance, recursive solution, the maximum continuity on the left and the maximum continuity on the right and the larger two are put into the variable maxans int L = a M-1], u = 0; For (INT I = s-1; i> = x; -- I) L = max (L, U + = A [I]); // divide and conquer the third part, merge (1) -Maximum continuity from the demarcation point to the left and lint r = A [m], V = 0; For (INT I = m; I <Y; ++ I) R = max (R, V + = A [I]); // the third part of sub-division, merge (2)-maximum continuity from the demarcation point to the right and rreturn max (maxans, L + r); // compare the solution of sub-Problems with L and R} int main () {CIN> N; srand (Time (null )); for (INT I = 1; I <= N; ++ I) {A [I] = rand ();} int maxsumans = maxsum (A, 1, n + 1); For (INT I = 1; I <= N; ++ I) cout <A [I] <Endl; cout <maxsumans <Endl; return 0 ;}

(4) O (n) algorithm:

After slightly improving the O (n2) algorithm, we can get the following most efficient algorithm!

Analysis: When J is determined, "S [J]-s [I-1] Max" is equivalent to "s [I-1] min", so you only need to scan the array once, maintain "minimum s encountered currently !!!

The Code is as follows:

# Include <iostream> # include <cstdlib> # include <ctime> # include <cmath> using namespace STD; const int maxn = 1000; int A [maxn], s [maxn], n; int maxsum (int * a, int * s, int N) {s [0] = 0; For (INT I = 1; I <= N; + I) s [I] = s [I-1] + A [I]; int mins = s [0], Maxs =-100000; for (INT I = 1; I <= N; ++ I) {If (s [I]-mins> Maxs) Maxs = s [I]-mins; if (s [I] <mins) Mins = s [I]; // maintain the "minimum s encountered currently"} return Maxs;} int main () {CIN> N; srand (Time (null); For (INT I = 1; I <= N; ++ I) {A [I] = rand () ;}for (INT I = 1; I <= N; ++ I) cout <A [I] <Endl; int maxsumans = maxsum (A, S, N); cout <maxsumans <Endl; return 0 ;}



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.