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;
    }
    
  • 相关阅读:
    Android之SQLite分页读取
    android 对sqlite数据库的增删改查
    Android如何导入已有的外部数据库(在raw下自己导入db文件)
    Android自定义对话框(Dialog)位置,大小
    android UI进阶之实现listview中checkbox的多选与记录
    Android开发教程 数据存储 SQLite
    Android 中的ORM框架
    android错误信息大整理
    C#中导出Execl
    关于用户退出,点击浏览器后退仍可回到原来页面解决二
  • 原文地址:https://www.cnblogs.com/TangYJHappen/p/12988921.html
Copyright © 2011-2022 走看看