Cod sursa(job #1540244)

Utilizator justsomedudePalade Thomas-Emanuel justsomedude Data 2 decembrie 2015 15:28:18
Problema Buline Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.89 kb
#include<iostream>
#include<fstream>
#define nmax 200005

using namespace std;

ifstream fin ("buline.in");
ofstream fout ("buline.out");

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

void Citire()
{
   int i, nr, semn;

   fin >> n; S = 0;

   for (i=1; i<=n; i++)
   {
     fin >> nr >> semn;
     if (semn == 0) nr = -nr;
     S += nr;
     a[i] = nr;
   }
}

void Ssmax()     /// imi ramane in stmax, drmax si smax tot ce imi trebuie
{
    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()      /// imi ramane in stmin, drmin si smin tot ce imi trebuie
{
    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 Afisare()
{
    S = S - smin;
    ofstream fout("buline.out");
    if (S < smax)
        fout << smax << " " << stmax << " " << (drmax-stmax+1) << "\n";
    else if (S > smax)
    {
        fout << S << " " << (drmin + 1) << " " << (n - (drmin-stmin+1)) << "\n";
    }
    else /// S == smax
    {
        fout << smax << " ";
        if (stmax < drmin + 1)
            fout << stmax << " " << (drmax-stmax+1) << "\n";
        else
            fout << (drmin + 1) << " " << (n - (drmin-stmin+1)) << "\n";
    }
    fout.close();
}

int main ()
{
  Citire();
  Ssmax();
  Ssmin();
  Afisare();
  fout.close();
  fin.close();
  return 0;
}