Cod sursa(job #2124358)

Utilizator MDiana15Diana M MDiana15 Data 7 februarie 2018 09:53:55
Problema Secventa 2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.63 kb
#include <fstream>
#include <queue>
#include <iostream>
using namespace std;
ifstream f("secv2.in");
ofstream g("secv2.out");
queue <int>q;
int x,n,s[50001],i,max1,st,dr,k,mx,ss;
int main()
{
   f>>n>>k;
   for(i=1;i<=n;i++)
   {
    f>>x;
    s[i]=s[i-1]+x;
   }
   q.push(1);
   st=1;
   dr=k;
   mx=s[k];
   for(i=k+1;i<=n;i++)
   {
    ss=s[i]-s[i-k];
    while(!q.empty()&&s[i]-s[q.front()-1]<ss)
    {
        q.pop();
    }
    q.push(i-k+1);
    ss=s[i]-s[q.front()-1];
    if(ss>mx)
    {
        mx=ss;
        st=q.front();
        dr=i;
    }
   }
   g<<st<<" "<<dr<<" "<<mx;
    return 0;
}