Pagini recente » Cod sursa (job #1711668) | Cod sursa (job #112949) | Cod sursa (job #2896222) | Cod sursa (job #1171829) | Cod sursa (job #443232)
Cod sursa(job #443232)
/*
* File: main.cpp
* Author: VirtualDemon
*
* Created on April 16, 2010, 3:18 PM
*/
#include <cstdlib>
#include <fstream>
#define SIZE 8219
/*
*
*/
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 )
{
in.read( file, SIZE );
idx=0;
}
}
x=0;
while( file[idx] >= '0' && file[idx] <= '9' )
{
x=x*10+file[idx]-'0';
if( ++idx == SIZE )
{
in.read( file, SIZE );
idx=0;
}
}
x*=sign;
}
int main(int argc, char** argv)
{
int N, i, j=1, x, maxx, S, start=1, Smax, mstart=1, mend=1;
in.open( "ssm.in" );
read(N);
read(x);
for( maxx=S=Smax=x, i=2; i <= N ; ++i )
{
read(x);
if( maxx < x )
maxx=x, j=i;
if( S < 0 )
{
S=0;
start=i;
}
S+=x;
if( S > Smax )
{
Smax=S;
mend=i;
mstart=start;
}
else if( S == Smax && ( i-start+1 ) < ( mend-mstart+1 ) )
{
mend=i;
mstart=start;
}
}
ofstream out( "ssm.out" );
if( Smax > maxx )
out<<Smax<<' '<<mstart<<' '<<mend<<'\n';
else out<<maxx<<' '<<j<<' '<<j<<'\n';
return EXIT_SUCCESS;
}