Cod sursa(job #2074240)

Utilizator MoldooooooooMoldoveanu Stefan Moldoooooooo Data 24 noiembrie 2017 11:54:28
Problema Buline Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <iostream>
#include <fstream>
#include <climits>
using namespace std;
ifstream f("buline.in");
ofstream g("buline.out");
int main()
{
    long int N, i, b, x, Smax, p, l=1, vf=0, mx, B, lmax=1, vf2=0, pmax;
    f>>N;
    long int V[N], S[N], MAX[N], MAXPOS[N], P[N];
    S[0]=0;
    for(i=1; i<=N; i++)
    {
        f>>x>>b;
        if(b){S[i]=S[i-1]+x;
              V[i]=x;}
              else{S[i]=S[i-1]-x;
                   V[i]=-x;}
    }
    MAX[1]=S[1];
    MAXPOS[++vf2]=1;
    for(i=2; i<=N; i++)
        if(S[i]>MAX[i-1]) {MAX[i]=S[i];
                           MAXPOS[++vf2]=i;}
                else MAX[i]=MAX[i-1];
    //for(i=1; i<=N; i++)g<<MAX[i]<<endl;
   // for(i=1; i<=vf2; i++)
    //    g<<MAX[i]<<" "<<MAXPOS[i]<<endl;
    vf2=1;
    for(i=1; i<=N; i++)if(V[i]>0)P[++vf]=i;
    vf=0; Smax=0; mx=0; lmax=INT_MIN;
    for(i=1; i<=N; i++)
    {
        if(Smax+V[i]>mx){mx=Smax+V[i];
                    lmax=l+1;
                    pmax=p;}
        if(Smax+V[i]<0){i=P[++vf];
                        Smax=V[i];
                        l=1; p=i;}
                  else {Smax+=V[i];
                        l++;}
    }
    g<<mx<<" "<<lmax<<" "<<pmax;
}