Cod sursa(job #1636491)

Utilizator QuzuroPrioteasa Andrei Quzuro Data 7 martie 2016 10:21:13
Problema Secventa 2 Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <fstream>
using namespace std;
ifstream fin("secv2.in");
ofstream fout("secv2.out");
int n,x,st,maxim,i,dr,smax,drmax,stmax,dif,k,temp;
long long s;
int main()
{
    fin>>n>>k;
    s=0;
    temp=0;
    maxim=-2000000000;
    for(i=1;i<=n;++i)
    {
        fin>>x;
        if(x>maxim)
            maxim=x;
        s=s+x;
        if(s>smax && i-temp>=k)
        {
            smax=s;
            dr=i;
            drmax=i;
            stmax=st;
            dif=drmax-stmax;
        }
        if(s==smax)
        {
            if(i-st<=dif)
                drmax=i;
        }
        if(s<0)
        {
            temp=i;
            s=0;
            st=i+1;
        }
    }
    if(smax<=0)
        smax=maxim;

    fout<<stmax<<" "<<drmax<<" "<<smax;
    return 0;
}