标签:des style blog color java os strong io
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 0
——————————————————————————————————————————
dp[i]表示以i为结尾的最大连续序列和,则 dp[ i ] = max ( dp[ i - 1 ] + a[i], a[i] )
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #define M 10001 using namespace std; int a[M],dp[M],n; int main() { while(scanf("%d",&n),n){ for(int i=0;i<n;++i){ scanf("%d",&a[i]); dp[i]=a[i]; } int f=0,l=0,x=0,maxx=dp[0];//f,x,l用来记录下标 for(int i=1;i<n;++i){ if((dp[i-1]+a[i])>a[i]){ dp[i]=dp[i-1]+a[i]; if(dp[i]>maxx){ maxx=dp[i]; f=x,l=i; //改变下标 } } else{ x=i; //如果dp[i]=a[i],则记录点i if(dp[i]>maxx){ maxx=dp[i]; f=l=i; //改变下标 } } //cout<<maxx<<" "<<f<<" "<<l<<endl; } if(maxx<0){ cout<<0<<" "<<a[0]<<" "<<a[n-1]<<endl; } else cout<<maxx<<" "<<a[f]<<" "<<a[l]<<endl; } return 0; }
HDU 1231——最大连续子序列(DP),布布扣,bubuko.com
标签:des style blog color java os strong io
原文地址:http://blog.csdn.net/u014141559/article/details/38370163