51nod 1050【DP】

思路:
就是先正常的dp一下求一个最大连续子串,然后特殊情况就是sum-最小连续子串。。
比一比谁大谁小就好了

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

typedef long long LL;

const int N=5e4+10;
const LL INF=5e13+10;

LL a[N];

int main()
{
    int n;
    LL ans1,ans2,sum;
    sum=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        sum+=a[i];
    }
    LL temp1=a[1];
    LL temp2=a[1];
    ans2=INF;
    ans1=-INF;
    for(int i=2;i<=n;i++)
    {
        if(temp1>0)
            temp1+=a[i];
        else
            temp1=a[i];
        if(temp1>ans1)
            ans1=temp1;

        if(temp2<0)
            temp2+=a[i];
        else
            temp2=a[i];
        if(temp2<ans2)
            ans2=temp2;
    }
    //printf("%lld %lld\n",ans1,ans2);
    printf("%lld\n",max(ans1,sum-ans2));
    return 0;
}
posted @ 2016-08-31 23:40  see_you_later  阅读(97)  评论(0编辑  收藏  举报