Cod sursa(job #2400895)

Utilizator mihnea.anghelMihnea Anghel mihnea.anghel Data 9 aprilie 2019 11:16:32
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.5 kb
#include <fstream>
#include <deque>
#define val first
#define poz second

using namespace std;
ifstream f("deque.in");
ofstream g("deque.out");
deque < pair <int, int> > deq;
int n,k,x,i;
long long s;

int main()
{
    f>>n>>k;
    for(i=1;i<=n;i++){
        f>>x;
        while ( deq.size() && x < deq.back().val ) deq.pop_back();
        deq.push_back(make_pair(x,i));
        if(deq.front().poz==i-k) deq.pop_front();
        if(i>=k) s+=deq.front().val;
    }
    g<<s;
    return 0;
}