Cod sursa(job #456666)

Utilizator alexandru92alexandru alexandru92 Data 16 mai 2010 13:45:11
Problema Subsecventa de suma maxima Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.12 kb
/* 
 * File:   main.cpp
 * Author: virtualdemon
 *
 * Created on May 16, 2010, 1:34 PM
 */
#include <cstdlib>
#include <fstream>
#define SIZE 9392

/*
 * 
 */
using namespace std;
ifstream in;
int idx;
char file[SIZE];
inline void read( int& x )
{
    int sign=1;
    while( file[idx] < '0' || file[idx] > '9' )
    {
        if( '-' == file[idx] )
            sign=-1;
        if( ++idx == SIZE )
        {
            idx=0;
            in.read( file, SIZE );
        }
    }
    for( x=0; file[idx] >= '0' && file[idx] <= '9'; )
    {
        x=x*10+file[idx]-'0';
        if( ++idx == SIZE )
        {
            idx=0;
            in.read( file, SIZE );
        }
    }
    x*=sign;
}
int main( void )
{
    int N, x, i, min, S, Smax, start, mstart, end;
    in.open( "ssm.in" );
    read(N); read(min);
    S=Smax=min;
    start=mstart=end=1;
    for( i=2; i <= N; ++i )
    {
        read(x);
        S+=x;
        if( S-min > Smax )
            Smax=S-min, start=mstart, end=i;
        if( S < min )
            min=S, mstart=i+1;
    }
    ofstream out( "ssm.out" );
    out<<Smax<<' '<<start<<' '<<end<<'\n';
    return EXIT_SUCCESS;
}