Cod sursa(job #1121687)

Utilizator catalincraciunCraciun Catalin catalincraciun Data 25 februarie 2014 13:40:17
Problema Secventa 2 Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.28 kb
/// Craciun Catalin
///  Secv2
#include <fstream>
#include <iostream>

#define NMax 51000

using namespace std;

ifstream f("secv2.in");
ofstream g("secv2.out");

unsigned int n,k;
int A[NMax];
long long best;
unsigned int st, dr;

void afisare()
{
    if (k==n)
    {
        long s=0;
        for (unsigned int i=1; i<=n;i++)
            s+=A[i];

        g<<1<<' '<<n<<' '<<s<<'\n';
    }
    else
        g<<st<<' '<<dr<<' '<<best<<'\n';

    g.close();
}

void pd()
{
    unsigned int auxSt;
    long long suma=0;
    unsigned int length=0;
    bool found=false;

    for (unsigned int i=1;i<=n;i++)
    {
        if (suma<0)
        {
            auxSt=i;
            suma=A[i];
        }
        else
            suma+=A[i];

        if (i-auxSt+1>=k)
            if (!found)
            {
                best=suma;
                st=auxSt;
                dr=i;

                found=true;
            }
            else if (suma>best)
            {
                best=suma;
                st=auxSt;
                dr=i;
            }
    }
}

void citire()
{
    f>>n>>k;
    for (unsigned int i=1;i<=n;i++)
        f>>A[i];
    f.close();
}

int main()
{
    citire();
    pd();
    afisare();

    return 0;
}