Cod sursa(job #2064548)

Utilizator MihalachiRazvanMihalachi Razvan MihalachiRazvan Data 12 noiembrie 2017 14:59:35
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <fstream>
#include <deque>
using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");
std::deque<long long int> deq;
std::deque<int> deqp;
long long int n,k,x,y,s=0;
int main()
{
    fin>>n>>k;
      for(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(n-deqp.front()<=k)
        s=s+deq.front();
    fout<<s;
    return 0;
}