• 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;
    }
    
  • 相关阅读:
    win8 64下启动Apache失败:443端口被占用的解决方法
    JavaScript初学者应注意的七个细节
    再说SQL Server数据库优化
    2010.Net程序员年终随笔
    基于Siverlight 3.0的超炫图表工具Visifire 最后一个免费版本,你还等什么?
    苦修六年 终成正果 幸福之路 从此开始
    Asp.net中服务端控件事件是如何触发的(笔记)
    我的缓存实例—工作记录
    坚持观点:决不为了用Linq而用Linq!!
    ASP.NET 之 常用类、方法的超级总结,并包含动态的EXCEL导入导出功能,奉上类库源码
  • 原文地址:https://www.cnblogs.com/TangYJHappen/p/12988921.html
Copyright © 2020-2023  润新知