Cod sursa(job #2652999)

Utilizator teofilotopeniTeofil teofilotopeni Data 26 septembrie 2020 16:24:24
Problema Subsecventa de suma maxima Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.24 kb
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream in("FileIn.txt");
    ofstream out("ssm.out");
    int n, i, x;
    in >> n;
    in >> x;
    int sumaMax = x, firstMax = 1, lastMax = 1, sumaAct = x, firstAct = 1;
    if (x < 0)
    {
        sumaAct = 0;
        firstAct = 2;
    }
    for (i = 2; i <= n; i++)
    {
        in >> x;
        if (sumaMax < 0)
        {
            sumaAct = x;
            if (sumaMax < sumaAct)
            {
                sumaMax = sumaAct;
                firstMax = firstAct;
                lastMax = i;
            }
            sumaAct = 0;
        }
        if (sumaAct + x >= 0)
        {
            if (sumaMax < sumaAct)
            {
                sumaMax = sumaAct;
                firstMax = firstAct;
                lastMax = i;
            }
            sumaAct += x;
            if (sumaMax < sumaAct)
            {
                sumaMax = sumaAct;
                firstMax = firstAct;
                lastMax = i;
            }
        }
        else
        {
            sumaAct = 0;
            firstAct = i + 1;
        }
        cout << sumaMax << " " << firstMax << " " << lastMax;
    }
    return 0;
}