Cod sursa(job #358718)

Utilizator nitica_isabelaNitica Isabela nitica_isabela Data 24 octombrie 2009 11:02:11
Problema Subsecventa de suma maxima Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.73 kb
#include<fstream>

using namespace std ;

int n, a[6000001];

void Citire()
{
ifstream f("ssm.in");

f>>n ;

for(int	i=1 ; i<=n ; i++)
	f>>a[i] ;

f.close() ;
}

void Rez()
{
int i, pozmax, lgmax, summax, suma, lg, poz;

summax = a[1]; pozmax = 1 ; lgmax = 1 ;
if (a[1] < 0)
{
	suma = 0 ; poz = 2 ; lg = 0 ;
}
else
{
	suma = a[1] ; poz = 1 ; lg = 1 ;
}

 for( i=2; i<=n; i++)	
 {
	suma += a[i] ; 
	lg++ ;
	if (suma > summax)
	{
		summax = suma ;
		lgmax = lg ;
		pozmax = poz ;
	}		
	if(suma < 0) 
	{
		suma = 0;
		poz = i+1 ;
		lg = 0 ;
	}
 }
 ofstream f("ssm.out") ;
 f<<summax<<" "<<pozmax<<" "<<(pozmax + lgmax - 1)<<"\n" ;
 f.close();
}

int main()
{
	Citire();
	Rez();
	return 0 ;
}