你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

01-复杂度2 Maximum Subsequence Sum (25 分)

2021/11/26 22:03:11

Given a sequence of K integers { N1​, N2​, ..., NK​ }. A continuous subsequence is defined to be { Ni​, Ni+1​, ..., Nj​ } where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤10000). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21

结尾无空行

Sample Output:

10 1 4

结尾无空行

答案:

#include <stdio.h>
int MaximumSubSum (int N, int a[],int *start_index, int *end_index,int *no_positive); 
//此函数返回最大子列和,同时修改目标子列下标还有数组的正负情况 。 

int main () {
    int result,i,length,start_index,end_index,no_positive;
    scanf("%d",&length);
    int array[length];
    for (i=0;i<length;i++) 
        scanf("%d",&array[i]);
    result=MaximumSubSum (length, array,&start_index, &end_index,&no_positive);
    switch (no_positive) { 
	//都是正数则正常,不都是正数再分情况 。 
        case 0:printf("%d %d %d",result,array[start_index],array[end_index]); break; //都是正数 。 
        case 1:{
        	int is_there_zero=0; 
        	for (i=0;i<length;i++) {
        		if (array[i]==0) {
        			is_there_zero=1;
        			break;
        		}
			}
			if (is_there_zero==1) {
			// 都是负数输出头尾;如果是负数和零输出第一个0,由于求最小的ij,此时ij都是第一个0的角标 。 
				printf("%d %d %d",result,array[i],array[i]);
			} else {
				printf("%d %d %d",result,array[0],array[length-1]);
			}
			break;
		}
    }
    return 0; 
}


int MaximumSubSum (int N, int a[],int *start_index, int *end_index,int *no_positive) {
    int i,ThisSum=0, MaxSum=0, temp_index=0;
    *start_index=0;
	*end_index=0;
	*no_positive=1;
	int previous_Maxsum=0;
    for (i=0; i<N; i++) {
        ThisSum+=a[i];
        if (ThisSum>MaxSum) {
            *start_index=temp_index;//当以目前的开头找到了更大的子列和,就替换掉开头。 
            *end_index=i;//末尾必然是当前的角标 。 
            MaxSum=ThisSum;//比最大的大则替换。 
            *no_positive=0;//只要此情况出现一次就代表有正数,即不是没有正数 。 
        } else if (ThisSum<0) {
		    temp_index=i+1;
			/*若前面一部分和是负的,则让下一个作为开头开始找,但是并不能保证找到更大的,
			因此需要等到更大的被找到的一瞬间,这个临时的开头才是真正最优解的开头。 */ 
			ThisSum=0; 
        } 
    }
    return MaxSum;
}