Cod sursa(job #2632575)

Utilizator stefan.popescuPopescu Stefan stefan.popescu Data 3 iulie 2020 21:20:25
Problema Secventa 3 Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.15 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <deque>
#include <iomanip>
using namespace std;
ifstream in ("secv3.in");
ofstream out("secv3.out");
struct tl
{
    int p1, p2;
    tl(int i=0, int j=0) {
        p1=i; p2=j;
    }
    friend tl operator + (const tl ths, const tl that) {
        return {ths.p1+that.p1, ths.p2+that.p2};
    }
    friend tl operator - (const tl ths, const tl that) {
        return {ths.p1-that.p1, ths.p2-that.p2};
    }
    void operator +=(const tl that) {
        p1+=that.p1; p2+=that.p2;
    }
    void operator -=(const tl that) {
        p1-=that.p1; p2-=that.p2;
    }

    operator double() {
        return (double)p1/p2;
    }

    friend bool operator >=(tl ths, tl that){
        return (double) ths >=(double) that;
    }



    friend ostream & operator << (ostream & out, tl val)
    {
        out<<fixed<<setprecision(2)<<(double) val;
        return out;
    }
};

int n, l1, l2, stact, dract;
vector <tl> a;
tl act, maxi;
inline tl getInterval(int st, int dr)
{
    if(dr<st)
        return {0, 0};
    return a[dr]-a[st-1];
}
int main()
{
    in>>n>>l1>>l2;
    a.resize(n+1);
    for(int i=1; i<=n; i++)
        in>>a[i].p1;
    for(int i=1; i<=n; i++)
        in>>a[i].p2;
    for(int i=2; i<=n; i++)
        a[i]+=a[i-1];

    stact=1; dract=l1; act=a[l1];

    maxi=act;

    for(int i=l1+1; i<=l2; i++)
    {
        //cout<<"q";
        if(getInterval(1, i)>=act)
        {
            act=getInterval(1, i);
            maxi=act;
            dract=i;
        }
    }
    for(int i=2; i<=n-l1+1; i++)
    {
        act-=getInterval(i-1, i-1);
        stact++;
        if(act>=maxi)
            maxi=act;

        if(dract-stact+1<l1)
        {
            dract++; act+=getInterval(dract, dract);
            if(act>=maxi)
                maxi=act;
        }

        if((act+getInterval(dract+1, min(i+l2-1, n)))>=act)
        {
            act=(act+getInterval(dract+1, min(i+l2-1, n)  ));
            dract=min(i+l2-1, n);
            if(act>=maxi)
                maxi=act;
        }
    }
    out<<maxi;
    return 0;
}