Cod sursa(job #1754964)

Utilizator meriniucrMeriniuc Razvan- Dumitru meriniucr Data 9 septembrie 2016 01:12:25
Problema Deque Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include <fstream>
#include <deque>

using ll = long long;

std::deque <ll> q;
std::deque <ll> firstK;

int main()
{
    ll n;
    ll index;
    ll k;
    ll x;
    ll sum;

    std::ifstream mama("deque.in");
    std::ofstream tata("deque.out");

    mama >> n;
    mama >> k;

    for (index = 0; index < k; ++index)
    {
        mama >> x;

        while ( (not q.empty()) and
                (q.back() > x) )
        {
            q.pop_back();
        }

        q.push_back(x);
        firstK.push_back(x);
    }

    sum = q.front();

    for (; index < n; ++index)
    {
        mama >> x;

        while ( (not q.empty()) and
                (q.back() > x) )
        {
            q.pop_back();
        }

        q.push_back(x);

        if (firstK.front() == q.front())
        {
            q.pop_front();
        }

        sum += q.front();
        firstK.pop_front();
        firstK.push_back(x);
    }

    tata << sum;
    return 0;
}