Cod sursa(job #1053001)

Utilizator cioionutFMI Ionut Ciocoiu cioionut Data 12 decembrie 2013 00:30:41
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include<iostream>
#include<fstream>
#include<deque>
#include<vector>
using namespace std;
int main()
{
    ifstream f("deque.in");
    ofstream g("deque.out");
    long long n,k,i,j,x,y;
    f>>n>>k;
    i=1;j=1;
    deque <long long> q,p;
    //for(i=0;i<n;i++) {f>>x;v.push_back(x);}
    x=0;
    for(i=0;i<n;i++)
    {
        f>>y;
        if(!q.empty()&&q.front()<=(i-k)) {q.pop_front();p.pop_front();}
        if(!q.empty()) while(!q.empty()&&p.back()>=y) {q.pop_back();p.pop_back();}
        q.push_back(i);p.push_back(y);
        if(i>=k-1) {
       if(q.front()==0&&j) {x+=p.front();j=0;}
           else if(q.front()!=0) x+=p.front();
        }
    }
          //if(!q.empty())  cout<<v[q.front()]<<" ";
        g<<x;
        f.close();
        g.close();
            return 0;
}