Cod sursa(job #672320)

Utilizator harababurelPuscas Sergiu harababurel Data 1 februarie 2012 20:57:53
Problema Subsecventa de suma maxima Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <iostream>
#include <fstream>
#include <limits.h>
using namespace std;
ifstream f("ssm.in");
ofstream g("ssm.out");
int n, v[6000005], best[6000005], lung[6000005];

void citire() {
	int i;
	f>>n;
	for(i=1; i<=n; i++) f>>v[i];
}

void rezolva() {
	int i,j;
	best[1]=v[1];
	lung[1]=1;
	
	for(i=2; i<=n; i++) {
		if(best[i-1]>0) {  best[i]=best[i-1]+v[i]; }
		else { best[i]=v[i]; }
	}
	
	int max=-INT_MAX;
	//secventa se termina unde ii maximu, incepe unde ii primul best negativ dinainte maximului
	for(i=1; i<=n; i++) if(best[i]>max) max=best[i];

	
	i=n;
	int start, stop;
	int startfin=1, stopfin=n;
	while(i>=1) {
		if(best[i]==max) {
			stop=i;
			j=i-1;
			while(best[j]>=0 && j>=1) {
				j--;
			}
			start=j+1;
			if(stop-start<stopfin-startfin) { stopfin=stop; startfin=start; }
			if(stop-start==stopfin-startfin && start<startfin) { startfin=start; stopfin=stop; } 
		}
		i--;
	}
	
	
	
	g<<max<<" "<<startfin<<" "<<stopfin<<"\n";
	

	
	
	
	
//	for(i=1; i<=n; i++)	g<<best[i]<<" "; g<<"\n";

	
}


int main() {
	citire();
	rezolva();
	
	f.close();
	g.close();
	return 0;
}