Cod sursa(job #1889240)

Utilizator cipri321Marin Ciprian cipri321 Data 22 februarie 2017 17:18:30
Problema Secventa 2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <cstdio>

using namespace std;
FILE *fi,*fo;
int n,x,i,sc,smax,st,dr,aux,k;
int main()
{
    fi=fopen("secventa2.in","r");
    fo=fopen("secventa2.out","w");
    fscanf(fi,"%d",&n);
    fscanf(fi,"%d",&k);
    fscanf(fi,"%d",&x);
    sc=x;
    smax=x;
    st=1;
    dr=1;
    aux=1;
    if(k==n)
        st=1;
    dr=n;
    smax=x;
    for(i=2; i<=n; i++)
    {
        fscanf(fi,"%d",&x);
        if(k==n)
            smax+=x;
        else
        {
            if(sc+x<x)
            {
                sc=x;
                aux=i;
            }
            else
                sc+=x;
            if(sc>smax && i-st+1>=k)
            {
                smax=sc;
                dr=i;
                st=aux;
            }
        }
    }
    fprintf(fo,"%d %d %d",st,dr,smax);
    fclose(fi);
    fclose(fo);
    return 0;
}