Cod sursa(job #1769173)

Utilizator vladboss2323Ciorica Vlad vladboss2323 Data 1 octombrie 2016 23:41:44
Problema Secventa 2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <iostream>
#include <fstream>
using namespace std;

int a[6000001];
int main()
{
    ifstream in("secv2.in");
    ofstream out("secv2.out");
    int i,sc=0,smax,x,y,n,xc=1,lung=1,k,b=-10000000;
    in>>n>>k;
    for(i=1; i<=n; i++)
        in>>a[i];
    if(n==k)
    {
        for(i=1; i<=n; i++)
              sc=sc+a[i];
        out<<1<<" "<<n<<" "<<sc;
    }
    else
    {
        sc = smax = a[1];
        for (i=2; i<=n; i++)
        {
            if(sc<0)
                if (sc < b)
                {
                    sc=0;
                    xc=i;
                    lung=0;
                }
                else if (sc < 0 )
                {
                    sc=0;
                    xc=i;
                    lung=0;
                }

            sc =sc + a[i];
            lung++;
            if(sc>0)
                if (sc > smax && lung>=k)
                {
                    smax = sc;
                    x = xc;
                    y=i;
                }
            if(sc<0)
                if (sc < smax && lung>=k)
                {
                    smax = sc;
                    x = xc;
                    y=i;
                }
        }
          out<<x<<" "<<y<<" "<<smax;
    }
    return 0;
}