Cod sursa(job #1937632)

Utilizator andreigasparoviciAndrei Gasparovici andreigasparovici Data 24 martie 2017 08:28:31
Problema Secventa 2 Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.09 kb
#include <cstdio>
#include <iostream>
#define MAXN 50005
using namespace std;

class InputParser
{
    char *b;
    int bs, bi;
    bool isdigit(char c){ return c>='0' && c<='9';}
    bool issgn(char c){ return c=='-' || c=='+';}
public:
    InputParser(FILE *f)
    {
        fseek(f,0,SEEK_END);
        bs = ftell(f);
        b = new char[bs+1];
        fseek(f,0,SEEK_SET);
        fread(b,1,bs,f);
        bi = 0;
    }
    InputParser operator>>(int &x)
    {
        x = 0;
        int sgn = 1;
        while(!isdigit(b[bi]) && !issgn(b[bi])) ++bi;

        if(issgn(b[bi]))
        {
            if(b[bi]=='-')
                sgn = -1;
            else sgn=1;
        }

        while(!isdigit(b[bi])) ++bi;

        while(isdigit(b[bi]))
            x = x * 10 + b[bi++] - '0';

        x*=sgn;
        return *this;
    }
    InputParser operator>>(long long &x)
    {
        x = 0;
        long long sgn = 1;
        while(!isdigit(b[bi]) && !issgn(b[bi])) ++bi;

        if(issgn(b[bi]))
        {
            if(b[bi]=='-')
                sgn = -1;
            else sgn=1;
        }

        while(!isdigit(b[bi])) ++bi;

        while(isdigit(b[bi]))
            x = x * 10 + b[bi++] - '0';

        x*=sgn;
        return *this;
    }
};

class OutParser {
private:
    FILE *fout;
    char *buff;
    int sp;

    void write_ch(char ch) {
        if (sp == 50000) {
            fwrite(buff, 1, 50000, fout);
            sp = 0;
            buff[sp++] = ch;
        } else {
            buff[sp++] = ch;
        }
    }


public:
    OutParser(const char* name) {
        fout = fopen(name, "w");
        buff = new char[50000]();
        sp = 0;
    }
    ~OutParser() {
        fwrite(buff, 1, sp, fout);
        fclose(fout);
    }

    OutParser& operator << (int vu32) {
        if (vu32 <= 9) {
            write_ch(vu32 + '0');
        } else {
            (*this) << (vu32 / 10);
            write_ch(vu32 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (long long vu64) {
        if (vu64 <= 9) {
            write_ch(vu64 + '0');
        } else {
            (*this) << (vu64 / 10);
            write_ch(vu64 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (char ch) {
        write_ch(ch);
        return *this;
    }
    OutParser& operator << (const char *ch) {
        while (*ch) {
            write_ch(*ch);
            ++ch;
        }
        return *this;
    }
};


int k, n;
long long int s[MAXN], m[MAXN], poz[MAXN], inc, sf, smax = -(1LL<<60);
int main()
{
    InputParser in(fopen("secv2.in","r"));

    in>>n;
    in>>k;
    for(int i=1;i<=n;i++)
    {
        long long a;
        in>>a;
        s[i] = s[i-1] + a;
        if(s[i]>m[i-1])
        {
            m[i] = m[i-1];
            poz[i] = poz[i-1];
        }
        else
        {
            m[i] = s[i];
            poz[i] = i;
        }
    }

    for(int i=k;i<=n;i++)
    {
        if(s[i] - m[i-k] > smax)
        {
            smax = s[i] - m[i-k];
            inc = poz[i-k]+1;
            sf = i;
        }
    }

    OutParser out("secv2.out");
    out<<inc<<' '<<sf<<' '<<smax;
    return 0;
}