HDU 1231 最大连续子序列

http://acm.hdu.edu.cn/showproblem.php?pid=1231

最大连续子序列,水

View Code
#include <stdio.h>

#include <stdlib.h>

int cmp(const void*a,const void*b)

{

    return *(int*)b-*(int*)a;

}

int a[110000];

int main()

{

    int n,i;

    int max,now;

    int start,end;

    int temp;

    int p,q;

    while(scanf("%d",&n),n)

    {

        scanf("%d",a+0);

        max=now=start=end=temp=a[0];

        for(i=1;i<n;i++)

        {

            scanf("%d",a+i);

            if(now+a[i]<a[i])

            {

                now=a[i];

                temp=a[i];

            }

            else

                now+=a[i];

            if(now>max){

                max=now;

                start=temp;

                end=a[i];

            }    

        }

        p=a[0];

        q=a[n-1];

        qsort(a,n,4,cmp);

        if(a[0]<0){

            max=0;

            start=p;

            end=q;

        }

        printf("%d %d %d\n",max,start,end);

    }

    return 0;

}

 

你可能感兴趣的:(HDU)