最大子列和问题

http://www.patest.cn/contests/mooc-ds/01-1

给定K个整数组成的序列{ N1, N2, ..., NK },“连续子列”被定义为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j <= K。“最大子列和”则被定义为所有连续子列元素的和中最大者。例如给定序列{ -2, 11, -4, 13, -5, -2 },其连续子列{ 11, -4, 13 }有最大的和20。现要求你编写程序,计算给定整数序列的最大子列和。

输入格式:

输入第1行给出正整数 K (<= 100000);第2行给出K个整数,其间以空格分隔。

输出格式:

在一行中输出最大子列和。如果序列中所有整数皆为负数,则输出0。

输入样例:
6
-2 11 -4 13 -5 -2
输出样例:
20
O(n*3)解法:
#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)解法:
#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;
}


 
 
分治算法 O(nlogn)解法:
#include <stdio.h>  
#include <vector>  
#include <iostream>  
using namespace std;  
long long max(long 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[left] > 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;  
}  



线性算法 O(n)复杂度:
 
 
#include <stdio.h>
#define MAXN 100001
long long maxSubseqSum(int a[], int n){
	long long thisSum = 0, maxSum = 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;
}




 

你可能感兴趣的:(最大子列和问题)