zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    js数组基础
    JavaScript原型链
    js之promise讲解
    ajax登录验证-js
    js事件委托
    js中的回调函数的理解和使用方法
    js闭包的理解
    JavaScript是如何实现继承的(六种方式)
    js创建对象的几种常用方式小结
    canvas绘图详解-08-样式填充
  • 原文地址:https://www.cnblogs.com/TangYJHappen/p/12988921.html
Copyright © 2011-2022 走看看