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;
    }
    
  • 相关阅读:
    Pycharm破解
    Web UI绕过登录的实现
    使用Docker安装Jenkins服务
    Appium 基于控件左滑操作
    Pytest执行用例报Hint: make sure your test modules/packages have valid Python names.
    Selenium文件上传
    获取Android手机日志
    Linux机器间ssh免密登录
    JMeter中使用Put请求方式请求接口
    python发送post请求上传文件,无法解析上传的文件
  • 原文地址:https://www.cnblogs.com/TangYJHappen/p/12988921.html
Copyright © 2011-2022 走看看