Cod sursa(job #689916)

Utilizator gener.omerGener Omer gener.omer Data 24 februarie 2012 22:59:58
Problema Subsecventa de suma maxima Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include <cstdio>

using namespace std;

#define NMAX 6000005
#define MIN (- 1 << 29)

int A[NMAX], N;

void citire(){
	FILE * f = fopen("ssm.in", "rt");
	fscanf(f, "%d", &N);
	for(int i = 0; i < N; ++i)
		fscanf(f, "%d", &A[i]);
	fclose(f);
}

int main(){
	citire();
	int smax = MIN, beginMax, endMax;
	int currentS = 0, currentBegin = 0;
	for(int i = 0; i < N; ++i){
		if(currentS + A[i] < 0){
			if(currentBegin <= i - 1 && currentS > smax){
				smax = currentS;
				beginMax = currentBegin+1, endMax = i;
			}
			currentBegin = i + 1;
			currentS = 0;
		}
		else{
			currentS += A[i];
			if(currentS > smax){
				smax = currentS;
				beginMax = currentBegin+1;
				endMax = i+1;
			}
		}
	}
	if(smax == MIN){
		for(int i = 0; i < N; ++i)
			if(A[i] > smax){
				smax = A[i];
				beginMax = i+1;
				endMax = i+1;
			}
	}
	
	FILE * f = fopen("ssm.out", "wt");
	fprintf(f, "%d %d %d", smax, beginMax, endMax);
	fclose(f);
	
	return 0;
}