hdu 1231最大连续子序列
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main( )
{
int sum,A[10010],i,j,k,m,n,N,x,y,t;
while(scanf("%d",&N)!=EOF,N)
{
for(i=0;i<N;i++)
scanf("%d",&A[i]);
sum=t=m=n=x=y=A[0];
for(i=1;i<N;i++)
{
if(sum>0)
sum+=A[i],m=A[i];
else
sum=A[i],m=n=A[i];
if(sum>t)
{
t=sum;
x=n;
y=m;
}
}
if(t<0)
t=0,x=A[0],y=A[N-1];
printf("%d %d %d\n",t,x,y);
}
//system("pause");
return 0;
}
关键是每一次相加后,与之前的做比较,然后如果比序列大就保存和与起点,终点,,非常得妙。。这题还可以用最差的方法做,我刚开始时,不了解动态规划就这样做的,可惜,测试数据全过了,就是wrong answer.....悲剧ing....
posted on 2011-04-18 02:50 more think, more gains 阅读(284) 评论(0) 收藏 举报
浙公网安备 33010602011771号