Cod sursa(job #2064494)

Utilizator MihalachiRazvanMihalachi Razvan MihalachiRazvan Data 12 noiembrie 2017 14:10:25
Problema Deque Scor 5
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <iostream>
#include <fstream>
#include <deque>
using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");
std::deque<long int> deq,deqp;
long int n,k,x,y,s=0;
int main()
{
    fin>>n>>k;
      for(long int i=1;i<=n;i++)
    {
        fin>>x;
        if(i>=k+1)
        {
         s=s+deq.front();
         if(i-deqp.front()==k)
         {deq.pop_front();
          deqp.pop_front();}}
        if(i==1)
        {deq.push_back(x);
         deqp.push_back(i);}
        else
        {
          while(deq.empty()!=1&&x<deq.back())
             {
             deq.pop_back();
             deqp.pop_back();}
            deq.push_back(x);
            deqp.push_back(i);
        }
    }
    if(x==deq.front())
        s=s+x;
    fout<<s;
    return 0;
}