Cod sursa(job #1657651)

Utilizator danutbodbodnariuc danut danutbod Data 20 martie 2016 17:38:43
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.57 kb
#include <fstream>
#include <queue>
using namespace std;
ifstream fi("deque.in");
ofstream fo("deque.out");
int n, k, x;
deque <pair <int, int> > deq;
long long smini;
int main()
{
    fi>> n >> k;
    for (int i = 1; i <= n; i++) {
        fi >> x;
        while (!deq.empty() && deq.front().first <= i - k)
            deq.pop_front();
        while (!deq.empty() && deq.back().second >= x)
            deq.pop_back();
        deq.push_back(make_pair(i,x));
        if (i >= k)
            smini += deq.front().second;
    }

   fo<<smini;
   return 0;
}