Maximum Subsequence Sum(With C)

来源:互联网 发布:vs2005操作mysql 编辑:程序博客网 时间:2024/05/18 03:16

Perface

This is my first CSDN blog, and I decided to write this blog entirely because of a blog called “Even though you don’t have a reader, you should write your own blog“Then I began my first blog with a homework in my data structure course.

Task Description

Given a sequence of K integers { N1,N2,N3,...,NK}. A continuous subsequence is defined to be { NiNi+1Ni+2,....Nj}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

PAY ATTENTIONS The output is the elements not the indices!!!

My Solution

`#include<stdio.h>int main(){    int ThisSum,MaxSum,i,j,begin,best_begin,end;    int N;    int A[10000];    scanf("%d",&N);    for(j=0;j<N;j++){        scanf("%d",&A[j]);    }       ThisSum=0;    MaxSum=best_begin=-1;/*pay attention to the initialization of MaxSum*/    begin=end=0;    for(j=0;j<N;j++){        ThisSum+=A[j];              if(ThisSum>MaxSum){            MaxSum=ThisSum;            end=j;            best_begin=begin;        }        else if(ThisSum<0)        {            ThisSum=0;            begin=j+1;        }    }    if(best_begin==-1){        MaxSum=0;        printf("%d %d %d",MaxSum,A[0],A[N-1]);    }    else{    printf("%d %d %d",MaxSum,A[best_begin],A[end]);    }    return 0;}

Comments

It should be clear why the time bound is correct, but it takes a little thought to see why the algorithm actually works. This is left to the reader. An extra advantage of this algorithm is that it makes only one pass through the data, and once a[i] is read and processed, it does not need to be remembered. Thus, if the array is on a disk or tape, it can be read sequentially, and there is no need to store any part of it in main memory. Furthermore, at any point in time, the algorithm can correctly give an answer to the subsequence problem for the data it has already read (the other algorithms do not share this property). Algorithms that can do this are called on-line algorithms. An on-line algorithm that requires only constant space and runs in linear time is just about as good as possible.

0 0
原创粉丝点击