hdu 1003 Max Sum【DP】 0MS 248K

//dp[i] = max(dp[i-1]+a[i],dp[i]) = a[i]+dp[i-1]<0?0:dp[i-1];
//max=a[i]+max<0?0:max;
#include<iostream>
using namespace std;
inline void read(int &d)
{
    char ch;
    int flag=1;
    while(ch=getchar(),ch==' '||ch=='\n');
    ch=='-'?flag=-1,d=0:d=ch-'0';
    while(ch=getchar(),ch>='0'&&ch<='9')    d=d*10+ch-'0';
    d*=flag;
}
int main()
{
    int a,t,n,cases=1;
    read(t);
    while(t--)
    {
        read(n);
        int sum=0,ans=-2000,tmp=1,head,rear;
        for(int i=1;i<=n;i++)
        {
            read(a);
            sum+=a;
            if(sum>ans) head=tmp,rear=i,ans=sum;
            if(sum<0)   sum=0,tmp=i+1;
        }
        //cout<<"Case "<<cases++<<":"<<endl;
        //cout<<ans<<' '<<head<<' '<<rear<<endl;
        printf("Case %d:\n%d %d %d\n",cases++,ans,head,rear);
        if(t)putchar('\n');
        //if(t)cout<<endl;
    }
    return 0;
}


你可能感兴趣的:(hdu 1003 Max Sum【DP】 0MS 248K)