Cod sursa(job #1526038)

Utilizator dnprxDan Pracsiu dnprx Data 15 noiembrie 2015 20:54:04
Problema Buline Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <bits/stdc++.h>

using namespace std;

int a[400009], n, smax, smin, stmax, drmax, stmin, drmin, S;

void Ssmax()
{
    int i, s, st;
    smax = s = a[1];
    stmax = drmax = 1;
    st = 1;
    if (s < 0) {s = 0; st = 2;}

    for (i = 2; i <= n; i++)
    {
        s += a[i];
        if (s > smax)
        {
            smax = s; stmax = st; drmax = i;
        }
        if (s < 0)
        {
            s = 0; st = i + 1;
        }
    }
}

void Ssmin()
{
    int i, s, st;
    smin = s = a[1];
    stmin = drmin = 1;
    st = 1;
    if (s > 0) {s = 0; st = 2;}

    for (i = 2; i <= n; i++)
    {
        s += a[i];
        if (s < smin)
        {
            smin = s; stmin = st; drmin = i;
        }
        if (s > 0)
        {
            s = 0; st = i + 1;
        }
    }
}

void Citire()
{
    int i, op, x;
    ifstream fin("buline.in");
    fin >> n;
    for (i = 1; i <= n; ++i)
    {
        fin >> x >> op;
        if (op == 0) a[i] = -x;
        else a[i] = x;
        S += a[i];
    }
    fin.close();
}

void Afisare()
{
    S = S - smin;
    ofstream fout("buline.out");
    if (S < smax)
        fout << smax << " " << stmax << " " << (drmax-drmin+1) << "\n";
    else
    {
        fout << S << " " << (drmin + 1) << " " << (n - (drmin-stmin+1)) << "\n";
    }
    fout.close();
}

int main()
{
    Citire();
    Ssmax();
    Ssmin();
    Afisare();
    return 0;
}