Cod sursa(job #1961955)

Utilizator shantih1Alex S Hill shantih1 Data 11 aprilie 2017 14:45:34
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <iostream>
#include <fstream>
#include <deque>

using namespace std;

long long n, i, k, x, s;
deque <int> dq, pq;

int main()
{
    ifstream fin("deque.in");
    ofstream fout("deque.out");

    fin >> n >> k;
    for (i = 1; i <= n; i++)
    {
        fin >> x;
        if (!dq.empty() && pq.front() <= i-k)
        {
            pq.pop_front();
            dq.pop_front();
        }
        while (!dq.empty() && dq.back() >= x)
        {
            pq.pop_back();
            dq.pop_back();
        }

        pq.push_back(i);
        dq.push_back(x);
        if (i >= k) s += dq.front();
    }
    fout << s << "\n";
}