Time
Limit: 2000/1000 MS (Java/Others) Memory Limit:
65536/32768 K (Java/Others)
Total Submission(s):
16819 Accepted Submission(s):
7387
#include<iostream>
using namespace std;
int main()
{
int
n,i,s[10001],a[10001],f[10001],max,st,end;
//f[i]为以i为结尾最长连续序列,s[i]为其开始点
bool
flag;
while(cin>>n&&n)
{
flag=0;
for(i=1; i<=n;
i++)
{
cin>>a[i];
if(a[i]>=0)
flag=1;
}
if(flag==0)
{
cout<<"0 "<<a[1]<<"
"<<a[n]<<endl;
continue;
}
f[1]=a[1];
s[1]=1;
for(i=2; i<=n;
i++)
{
if(f[i-1]+a[i]>=a[i])
{
f[i]=f[i-1]+a[i];
s[i]=s[i-1];
}
else
{
f[i]=a[i];
s[i]=i;
}
}
max=f[1];
st=1;
end=1;
for(i=2; i<=n;
i++)
{
if(f[i]>max)
{
max=f[i];
st=s[i];
end=i;
}
}
cout<<max<<"
"<<a[st]<<" "<<a[end]<<endl;
}
return 0;
}
原文:http://www.cnblogs.com/lxm940130740/p/3560469.html