Cod sursa(job #1829472)

Utilizator chiricaandrei23Chirica Andrei chiricaandrei23 Data 15 decembrie 2016 00:03:56
Problema Subsecventa de suma maxima Scor 5
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <iostream>
#include <fstream>
using namespace std;
int main(){
    ifstream f("ssm.in");
    ofstream g("ssm.out");

    int n, i, x;
    int max_ending_here = 0;
    int max_so_far = 0;
    int max_starting_at = 0;
    int current_max_pos = 0;
    int max_ends_at = 0;
    int v[600000];

    f >> n;

    for(i = 1; i <= n; ++i){
        f >> x;
        max_ending_here = max(0, max_ending_here + x);
        if (max_so_far > max_ending_here){
            max_starting_at = current_max_pos;
            max_ends_at = i;
        }
        max_so_far = max(max_so_far, max_ending_here);
        if (max_ending_here == 0){
            current_max_pos = i;
        }
    }
    g << max_so_far << " " << current_max_pos + 1 << " " << max_ends_at + 1;
    f.close();
    g.close();
    return 0;
}