【Java/C】最大子列和Maximum Subsequence Sum

课程:数据结构陈越何钦铭


Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, ..., N​j​​ } 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

时间限制: 200 ms

内存限制: 64 MB

代码:使用课程里讲到的在线处理法

import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int k = in.nextInt();//输入正整数K
		int[] nums = new int[k];
		for (int i=0; i<k; i++) {//输入K个整数
			nums[i]=in.nextInt();
		}
		in.close(); 
		
		MaxSubseqSum maxSum = new MaxSubseqSum();
		int[] result = maxSum.maxSubseqSum2(nums, k);
		for(int i=0;i<result.length;i++){//输出结果,以空格分隔
            System.out.print(result[i]);
            if(i<result.length-1){
                System.out.print(" ");
            }
        }
	}
}

class MaxSubseqSum {
	
	//在线处理法
	public int[] maxSubseqSum2(int[] a, int n){
		
		int thisSum = 0, maxSum = 0, reSum = 0;
        int first = a[0], last = a[n-1];
        int start = 0, end = 0;
        boolean flag = false;
        for(int i=0; i<n; i++){
            thisSum += a[i];
            if(thisSum > maxSum){
                maxSum = thisSum;
                end = i;
            }else if(thisSum < 0){
                thisSum = 0;
            }
            if(a[i] == 0){
                flag = true;
            }
        }
        
        for(int j=end; j>=0; j--){
            reSum += a[j];
            if(reSum == maxSum){
                start = j;
                if(a[j] >= 0) {//因为要求最大子列和不唯一时,输出的first和last值的下标要最小,故当最大子列和前面有0时,要把这些0加进来
                	continue;
                }
            }
        }

        if(maxSum > 0){
            first = a[start];
            last = a[end];
        }else if(maxSum == 0){//maxSum为0存在两种情况:一种是所有整数都为负数,此时first和last分别为输入数列的首尾值;另一种是负数和0的组合,此时无论0的位置和数量,first和last都是0,所以在上面遍历时增加flag变量以记录是否存在0
            if(flag){
                first = 0;
                last = 0;
            }
        }
        return (new int[] {maxSum,first,last});
	}
}

结果:

 最后一个测试用例的运行超时问题没有能够解决,期待更好的解法。


更新

用C语言将该算法实现了一下,运算效率高很多,最后一例顺利通过。

#include<stdio.h>

int main(){
	int n;
	int thisSum = 0, maxSum = 0, reSum = 0;
	int start = 0, end = 0;//最大子列和首尾数字的下标 
	int first, last;//最大子列和的首尾数字 
	int flag = 0;//用来判断输入数字都是负数或零的情况 
	
	scanf("%d",&n);
	int nums[n];
	for( int i = 0; i < n; i++ ){
		scanf("%d", &nums[i]);
		thisSum += nums[i];
		if( thisSum > maxSum ){
			maxSum = thisSum;
			end = i;
		}
		else if( thisSum < 0){
			thisSum = 0;
		}
		if( nums[i] == 0 ){
			flag = 1;
		}
	}
	
	for( int j = end; j >= 0; j-- ){
		reSum += nums[j];
		if( reSum == maxSum ){
			start = j;
			if( nums[j-1] == 0 ){
				continue;
			}
		}
	}
	
	first = nums[start];
	last = nums[end]; 
	
	if( maxSum == 0 ){
		if( flag ){//输入数字是负数和零的组合 
			first = last = 0;
		}else{//输入数字都是负数 
			first = nums[0];
			last = nums[n-1];
		}
	}
	
	printf("%d %d %d", maxSum, first, last);	
    
    return 0;
}

发布了37 篇原创文章 · 获赞 47 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/u013378642/article/details/104542554