hdu1231-最大连续子序列

http://acm.hdu.edu.cn/showproblem.php?pid=1231

dp入门题

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<iomanip>

using namespace std;

#define MAX 10005
int main()
{
	int dp[ MAX ] ;
	int n ;
	while( scanf( "%d" , &n ) != EOF , n ) 
	{
		int sum = 0 ;
		int temp = 0 , temp1 = 0 , temp2 = 0 	;
		int begin ;
		int end ;
		int Maxnum =  -99999;
		for( int i = 0 ; i < n ; ++i )
		{
			scanf( "%d" , &dp[ i ] ) ;
			sum += dp[ i ] ;
			if( sum < 0 )
			{
				temp1 = i + 1 ;
				sum = 0 ;
			} 
			else
			{
				if( Maxnum < sum )
				{
					Maxnum = sum ;
					begin = temp1 ;
					end = i ; 
				}
			}	
		}	
		if( Maxnum < 0 )
		{
			printf( "0 %d %d\n" , dp[ begin ] , dp[ n - 1 ] ) ;
		}
		else
			printf( "%d %d %d\n" , Maxnum , dp[ begin ] ,dp[ end ] ) ;
	}
	return 0 ;
}


 

 

posted @ 2013-07-26 19:10  坚固66  阅读(120)  评论(0编辑  收藏  举报