• Maximum Subsequence Sum


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

    代码实现:
    #include <stdio.h>
    #include <stdlib.h>
    int MaxSum, first, last;
    void MaxSubseqSum(int List[], int N,int *TmpMaxSum, int *Tmpfirst, int *Tmplast ) {
    	int i, Sum, LocFst,flag;
    	*TmpMaxSum = -1; 
        *Tmpfirst = *Tmplast = 0;
    	Sum = flag = 0;
        LocFst = List[0];
    	for (i = 0; i < N;i++) {
    		Sum += List[i];
            if(List[i]==0) flag=1;   //用于标记数列中是否有0
            if (Sum < 0) { 
    			Sum = 0;
                if(List[i+1]>0) {
                    LocFst = List[i+1];
                    }
    		}
            if (Sum > *TmpMaxSum) {
    			*TmpMaxSum = Sum;
    			*Tmplast = List[i];
    			*Tmpfirst = LocFst;
    		}
    	}
    	if (*TmpMaxSum == 0&&flag==0) {   
                *Tmpfirst = List[0];
                *Tmplast = List[N-1];
            }else if(*TmpMaxSum == 0&&flag==1){
                for(i = 0; i < N;i++){
                    if(List[i]==0){
    			        *Tmpfirst = List[i];
                        *Tmplast = List[i];
                        break;
                    }
                }
            }
    }
    int main(){
        int N;
    	scanf("%d", &N);   
    	int *arr;
    	arr = (int *)malloc(N * sizeof(int));
    	for (int j = 0; j < N; j++) {
    		scanf("%d", &arr[j]);
    	}
    	 MaxSubseqSum(arr, N, &MaxSum, &first, &last);
    	 printf("%d %d %d",MaxSum, first, last);
        return 0;
    }
    
  • 相关阅读:
    Maven2-profile多环境配置
    Maven-setting.xml详解
    Maven-通过命令操作maven项目
    Maven-eclipse运行maven命令
    Eclipse-导入maven项目
    Maven-搭建maven web项目
    Maven-搭建普通maven项目
    Maven-pom.xml详解
    Maven-生命周期
    Maven-常用命令
  • 原文地址:https://www.cnblogs.com/TangYJHappen/p/12988921.html
Copyright © 2020-2023  润新知