标签:des style blog http color io os java ar
6 -2 11 -4 13 -5 -2 10 -10 1 2 3 4 -5 -23 3 7 -21 6 5 -8 3 2 5 0 1 10 3 -1 -5 -2 3 -1 0 -2 0
20 11 13 10 1 4 10 3 5 10 10 10 0 -1 -2 0 0 0Huge input, scanf is recommended.HintHint
/****************************************
*****************************************
* Author:Tree *
*From :http://blog.csdn.net/lttree *
* Title : 最大连续子序列 *
*Source: hdu 1231 *
* Hint : dp *
*****************************************
****************************************/
#include <stdio.h>
int a[10001],sum[10001],pre[10001];
int main()
{
int n,i;
int Max,Max_i;
// isnegtive来推断是否全部数都小于0
bool isnegtive;
while( scanf("%d",&n)!=EOF && n)
{
isnegtive=false;
for(i=0;i<n;++i)
{
scanf("%d",&a[i]);
if( a[i]>=0 ) isnegtive=true;
}
// 假设全部数都小于0,后面不用算,直接输出
if( !isnegtive )
{
printf("0 %d %d\n",a[0],a[n-1]);
continue;
}
// 计算最大序列和
sum[0]=pre[0]=a[0];
for( i=1;i<n;++i )
{
if( sum[i-1]+a[i]>a[i] )
{
sum[i]=sum[i-1]+a[i];
pre[i]=pre[i-1];
}
else
sum[i]=pre[i]=a[i];
}
// 寻找最大子序列和,存下下标
Max=-999999;
for( i=0;i<n;++i )
{
if( sum[i]>Max )
{
Max=sum[i];
Max_i=i;
}
}
printf("%d %d %d\n",Max,pre[Max_i],a[Max_i]);
}
return 0;
}
标签:des style blog http color io os java ar
原文地址:http://www.cnblogs.com/lcchuguo/p/3991355.html