Cod sursa(job #526405)

Utilizator feelshiftFeelshift feelshift Data 28 ianuarie 2011 11:46:00
Problema Subsecventa de suma maxima Scor 95
Compilator cpp Status done
Runda Arhiva educationala Marime 1.12 kb
// http://infoarena.ro/problema/ssm
#include <fstream>
using namespace std;

#define INFINITY 0x3f3f3f3f
#define maxLenght 6000001

int lenght,bestSum,leftPosition,rightPosition;
int array[maxLenght],sum[maxLenght],best[maxLenght];

void read();
void getMaxSubsequence();
void findLeftPosition();
void write();

int main() {
	read();
	getMaxSubsequence();
	findLeftPosition();
	write();

	return (0);
}

void read() {
	ifstream in("ssm.in");

	in >> lenght;

	for(int i=1;i<=lenght;i++)
		in >> array[i];

	in.close();
}

void getMaxSubsequence() {
	bestSum = array[1];

	for(int i=1;i<=lenght;++i) {
		best[i] = array[i];

		if(best[i] < best[i-1] + array[i])
			best[i] = best[i-1] + array[i];

		if(bestSum < best[i]) {
			bestSum = best[i];
			rightPosition = i;
		}
	}
}

void findLeftPosition() {
	int tmp = 0;

	for(int i=rightPosition;i>=1;i--) {
		tmp = tmp + array[i];

		if(tmp == bestSum) {
			leftPosition = i;
			break;
		}
	}
}

void write() {
	ofstream out("ssm.out");

	out << bestSum << " " << leftPosition << " " << rightPosition << "\n";

	out.close();
}