hdu 1003 max sum #最大字段和

来源:互联网 发布:js去掉字符串最后几个 编辑:程序博客网 时间:2024/06/11 12:06
/*最大字段和trip:全为负数的情况*/#include <stdio.h>const int N = 100001;const int MININF = -100000000;int main(){    int a[N],p,maxsum,t,cas = 0;    scanf("%d",&t);    while(t--)    {        int n;        scanf("%d",&n);        int i;        for(i = 0; i < n; ++i)            scanf("%d" , a + i);        int lef = 0,rig = 0,tlef = 0,trig = 0;        maxsum = 0;        p = MININF;        for(i = 0; i < n && a[i] < 0; ++i)            if(p < a[i])            {                p = a[i];                lef = i;            }        if(i == n)        {            if(cas)                printf("\n");            printf("Case %d:\n%d %d %d\n",++cas,p,lef+1,lef+1);            continue;        }        p = 0;        for(i = 0; i < n ; ++i)        {            if(p < 0)            {                trig = tlef = i;                p = a[i];                if(p > maxsum)                {                    maxsum = p;                    lef = tlef;                    rig = trig;                }            }            else            {                p += a[i];                trig = i;                if(p > maxsum)                {                    maxsum = p;                    lef = tlef;                    rig = trig;                }            }        }        if(cas)            printf("\n");        printf("Case %d:\n%d %d %d\n",++cas,maxsum,++lef, ++rig);    }    return 0;}/*65 6 -1 5 4 -77 0 6 -1 1 -6 7 -57 6 -1 5 4 -7 1007 0 6 -1 1 -6 7 -5*/