Http://www.patest.cn/contests/mooc-ds/01-1
Given the sequence of K-integers {N1, N2, ..., NK}, "contiguous sub columns" are defined as {Ni, ni+1, ..., Nj}, where 1 <= i <= J <= K. Maximum sub columns and is defined as the largest of all contiguous child column elements. For example, given the sequence {-2, 11,-4, 13,-5,-2}, its contiguous sub columns {11,-4, 13} have the largest and 20. You are asked to write a program that calculates the maximum number of columns for a given integer sequence.
Input format:
The input line 1th gives a positive integer k (<= 100000), and line 2nd gives K integers, which are separated by spaces.
Output format:
Outputs the largest columns in a row. If all integers in the sequence are negative, the output is 0. Input Sample:
6
-2 11-4 13-5-2
Output Sample:
20
O (n*3) Solution:
#include <stdio.h>
#define MAXN 100001
Long Long maxSubseqSum1 (int a[], int n) {
long long maxsum = 0;
for (int i = 0; i < n; ++i) {for
(int j = i; j < n; ++j) {
long long thissum = 0;
for (int k = i; k <= J; ++k) {
thissum = a[k];
if (Thissum > Maxsum)
maxsum = thissum;
}
}} return maxsum;
}
int main () {
int k, A[MAXN];
scanf ("%d", &k);
for (int i = 0; i < K; ++i)
scanf ("%d", &a[i]);
printf ("%lld\n", MaxSubseqSum1 (A, k));
return 0;
}
O (n*2) Solution:
#include <stdio.h>
#define MAXN 100001
Long Long maxSubseqSum2 (int a[], int n) {
long long maxsum = 0;
for (int i = 0; i < n; ++i) {
long long thissum = 0;
for (int j = i; j <= N; ++j) {
thissum = a[j];
if (Thissum > Maxsum)
maxsum = thissum;
if (Thissum < 0)
thissum = 0;
}
}
return maxsum;
}
int main () {
int k, A[MAXN];
scanf ("%d", &k);
for (int i = 0; i < K; ++i)
scanf ("%d", &a[i]);
printf ("%lld\n", maxSubseqSum2 (A, k));
return 0;
}
Divide-and-conquer algorithm O (NLOGN) Solution:
#include <stdio.h> #include <vector> #include <iostream> using namespace std;
Long Long Max (long A, long long B, long long C) {if (b > a) a = B;
if (a > C) return A;
else return C; Long Long maxsubseqsum (const vector<int> &a, int left, int right) {if (left = right) {if (a[l
EFT] > 0) return a[left];
else return 0;
int mid = (left + right)/2;
Long Long maxleftsum = Maxsubseqsum (A, left, mid);
Long Long maxrightsum = Maxsubseqsum (A, mid+1, right);
Long Long maxleftbordersum = 0, leftbordersum = 0;
for (int i = mid; I >= left;-i) {leftbordersum + = A[i];
if (Leftbordersum > maxleftbordersum) maxleftbordersum = leftbordersum;
Long Long maxrightbordersum = 0, rightbordersum = 0; for (int i = mid+1 i <= right; ++i) {Rightbordersum + + a[i];
if (Rightbordersum > maxrightbordersum) maxrightbordersum = rightbordersum;
Return Max (Maxleftsum, Maxrightsum, maxleftbordersum+maxrightbordersum);
int main () {vector<int> A;
int k, x;
scanf ("%d", &k);
for (int i = 0; i < K; ++i) {cin >> x;
A.push_back (x);
printf ("%lld\n", Maxsubseqsum (A, 0, a.size ()-1));
return 0; }
Linear algorithm O (n) Complexity:
#include <stdio.h>
#define MAXN 100001
Long Long maxsubseqsum (int a[], int n) {
long long thissum = 0, MA xsum = 0;
for (int i = 0; i < n; ++i) {
thissum + = A[i];
if (Thissum > Maxsum)
maxsum = thissum;
else if (Thissum < 0)
thissum = 0;
}
return maxsum;
}
int main () {
int k, A[MAXN];
scanf ("%d", &k);
for (int i = 0; i < K; ++i)
scanf ("%d", &a[i]);
printf ("%lld\n", Maxsubseqsum (A, k));
return 0;
}